Javascript

자바 스프링 회원가입 페이지 [기능 구현]

최종군 2024. 10. 11. 14:36

 

 

 <h2>회원가입</h2>
            <br>
            <%-- 상대 경로 요청 시 action="insert" 로 작성 --%>
            <form action="<%= contextPath %>/member/insert" method="post">

                <div class="form-group">
                    <label for="userId">* ID </label>
                    <input type="text" class="form-control mb-3" name="userId" id="userId" placeholder="Enter ID.." required>

                    <label for="userPwd">* Password </label>
                    <input type="password" class="form-control mb-3" name="userPwd" id="userPwd" placeholder="Enter Password.." required>

                    <label for="checkPwd">* Password Check </label>
                    <input type="password" class="form-control mb-3" id="checkPwd" placeholder="Enter Password.." required>
                    
                    <label for="userName">* Name </label>
                    <input type="text" class="form-control mb-3" name="userName" id="userName" placeholder="Enter Name.." required>

                    <label for="email"> &nbsp; Email </label>
                    <input type="email" class="form-control mb-3" name="email" id="email" placeholder="Enter Email..">  
                    
                    <label for="age"> &nbsp; Age </label>
                    <input type="number" class="form-control mb-3" name="age" id="age" placeholder="Enter Age..">  
                    
                    <label for="phone"> &nbsp; Phone </label>
                    <input type="tel" class="form-control mb-3" name="phone" id="phone" placeholder="Enter Phone(-제외)..">  
                    
                    <label for="address"> &nbsp; Address </label>
                    <input type="text" class="form-control mb-3" name="address" id="address" placeholder="Enter Address..">

                    <label for=""> &nbsp; Gender</label> &nbsp;&nbsp;
                    <input type="radio" class="mb-3" name="gender" id="Male" value="M">
                    <label for="Male">남자</label> &nbsp;&nbsp;
                    <input type="radio" class="mb-3" name="gender" id="Female" value="F">
                    <label for="Female">여자</label><br>

                </div>
                <br>
                <div class="btns"  align="center">
                    <button type="submit" class="btn btn-primary">회원가입</button>
                    <button type="reset" class="btn btn-danger">초기화</button>
                </div>
            </form>
        </div>
        <br><br>
    </div>

 

@RequestMapping("/insert")
public String insertMember(Member m
, HttpSession session
, Model model) {


// => 한글 인코딩 처리 --> web.xml 파일에 필터를 등록하여 처리
// => 숫자 타입의 데이터(나이,age)가 값이 없을 경우 400 에러 발생
// --> DB 처리 시 자동 형변환 되므로 정수형->문자열 형변환하여 처리

 

---비밀번호 암호화 처리하기 : 암호화 등록 방법 ---

* Spring-security 라이브러리 추가 (pom.xml 등록)

<!-- spring-security 관련 라이브러리 -->
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-core -->
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-core</artifactId>
    <version>5.8.11</version>
</dependency>

 

* BCryptPasswordEncoder 클래스를 빈으로 등록 (xml 파일 사용)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-5.8.xsd">
<bean class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"
id="bCryptPasswordEncoder"/>
</beans>

appServlet 폴더 하위 경로로 생생 : spring-security-xml; 

 

* 추가한 파일을 서버 구동 시 pre-loading 할 수 있도록 web.xml 파일에 설정

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/root-context.xml 
/WEB-INF/spring/spring-security.xml
</param-value>
</context-param>

-------------------------------------------------------------------------------------------------------------

 


m.setUserPwd(bCrypt.encode(m.getUserPwd()) );
// => Member 객체에 비밀번호 평문을 암호문으로 변경

int result = mService.insertMember(m);

 

DML => int 형으로 처리한다.

 
if (result > 0) {

회원가입 성공 => 성공메시지 + 메인페이지 url 재요청
session.setAttribute("alertMsg", "회원가입에 성공했습니다.");
return "redirect:/";
} else { // 회원가입 실패 => 에러메시지 + 에러페이지 포워딩
model.addAttribute("errorMsg", "회원가입에 실패했습니다.");
return "common/errorPage";
}
}