기존의 UserBean에서 로그인에 필요한 정보로 만들고
추가로 로그인 상태를 추가
//UserBean의 객체를 loginUserBean 이름으로 세션에 등록함
@Component("loginUserBean")
@SessionScope
public class LoginUserBean {
private int user_idx;
private String user_name;
@Size(min=4, max=20)
@Pattern(regexp = "[a-zA-Z0-9]*")
private String user_id;
@Size(min=4, max=20)
@Pattern(regexp = "[a-zA-Z0-9]*")
private String user_pw;
private boolean userLogin; // 로그인상태
public LoginUserBean() {
this.userLogin = false; //처음로그인상태는 false
}
//get(),set() 생략
}
<!--추가-->
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<form:form action="${root }user/login_pro" modelAttribute="loginBean">
<div class="form-group">
<form:label path="user_id">아이디</form:label>
<form:input path="user_id" class="form-control" />
<form:errors path="user_id" style="color:coral"/>
</div>
<div class="form-group">
<form:label path="user_pw">비밀번호</form:label>
<form:input path="user_pw" showPassword="true" class="form-control" />
<form:errors path="user_pw" style="color:coral" />
</div>
<div class="form-group text-right">
<form:button class="btn btn-primary">로그인</form:button>
<a href="${root }user/join" class="btn btn-danger">회원가입</a>
</div>
</form:form>
@GetMapping("/login")
public String login(@ModelAttribute("loginBean") LoginUserBean loginBean){
return "user/login";
}
//유효성검사
@PostMapping("/login_pro")
public String login_pro(@ModelAttribute("loginBean") LoginUserBean loginBean, BindingResult result) {
if(result.hasErrors()) {
return "user/login";
}
return "user/login_success";
}
유효성체크
@Select("select user_idx, user_name " +
"from user_table " +
"where user_id=#{user_id} and user_pw=#{user_pw}")
LoginUserBean getLoginUserInfo(LoginUserBean loginBean);
public void getLoginUserInfo(LoginUserBean loginBean) {
LoginUserBean tempLoginBean = userMapper.getLoginUserInfo(loginBean);
if(tempLoginBean != null) {
loginUserBean.setUser_idx(tempLoginBean.getUser_idx());
loginUserBean.setUser_name(tempLoginBean.getUser_name());
loginUserBean.setUserLogin(true); //로그인상태 true
}
}
@Resource(name = "loginUserBean")
private LoginUserBean loginUserBean;
/*생략*/
//유효성검사
@PostMapping("/login_pro")
public String login_pro(@Valid @ModelAttribute("loginBean") LoginUserBean loginBean,
BindingResult result) {
if(result.hasErrors()) {
return "user/login";
}
//유효성테스트 완료후 id pw로 현재 로그인 유저정보를 DB에서 꺼내와 세션에 로그인객체에 저장한다.
userService.getLoginUserInfo(loginBean);
if(loginUserBean.isUserLogin() == true) {
return "user/login_success";
} else {
return "user/login_fail";
}
}
로그인 실패했을시, 로그인 성공했을 경우 페이지
@GetMapping("/login")
public String login(@ModelAttribute("loginBean") LoginUserBean loginBean, Model model,
@RequestParam(value="fail",defaultValue="false") boolean fail) {
model.addAttribute("fail", fail);
return "user/login";
}
<c:if test="${fail == true }">
<div class="alert alert-danger">
<h3>로그인 실패</h3>
<p>아이디 비밀번호를 확인해주세요</p>
</div>
</c:if>
<script>
alert('로그인에 실패하엿습니다')
location.href = '${root}user/login?fail=?'
</script>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var='root' value="${pageContext.request.contextPath }/"/>
<script>
alert('로그인 되었습니다')
location.href = '${root}'
</script>