게시판 만들기/JSP ➜ Spring
[Spring] 로그인/로그아웃 구현
code-mo
2023. 2. 6. 07:30
728x90
1. JSP 로그인 페이지
1
2
3
4
5
6
7
8
9
10
11
|
<form method="post" action="loginAct">
<h3 style="">로그인 화면</h3>
<div class="from-group">
<input type="text" class="form-control" placeholder="아이디"
name="userID" maxlength="20">
</div>
<input type="password" class="form-control" placeholder="비밀번호"
name="userPassword" maxlength="20">
</div>
<input type="submit" class="btn btn-primary form-control" value="로그인">
</form>
|
cs |
2. 로그인 Controller
로그인하는 사용자의 아디와 비밀번호를 받아와서 비교 후 해당 유저가 있으면 Session에 key, value 형태로
저장하여 메인 페이지로 이동하고 사용자 정보가 잘못되거나 없으면 로그인 페이지를 새로고침
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
//로그인
@RequestMapping(value = "/loginAct", method = RequestMethod.POST)
public String loginAct(HttpServletRequest request, HttpServletResponse response,
HttpSession session,
@RequestParam String userID,
@RequestParam String userPassword) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("userID", userID);
map.put("userPassword", userPassword);
MemberDTO member = memberSvc.selectMemberLogin(map);
if (member != null) {
session.setAttribute("userID", userID);
return "redirect:/main";
}else {
return "redirect:/login";
}
}
|
cs |
3. 로그인 Service
1
2
3
4
|
//로그인
public MemberDTO selectMemberLogin(Map<String, Object> map) {
return session.selectOne("selectMemberLogin", map);
}
|
cs |
4. 로그인 Select 쿼리
1
2
3
4
5
|
<!-- 로그인 -->
<select id="selectMemberLogin" parameterType="java.util.Map" resultType="com.member.MemberDTO">
SELECT * FROM bbs.user
WHERE userID = #{userID} AND userPassword = #{userPassword}
</select>
|
cs |
5. 로그아웃 Controller
로그아웃하면 Session을 비워주고 메인페이지를 새로고침
1
2
3
4
5
6
7
|
//로그아웃
@RequestMapping(value = "/logoutAct", method = RequestMethod.GET)
public String logoutAct(HttpServletRequest request, HttpServletResponse response,
HttpSession session) {
session.invalidate();
return "redirect:/main";
}
|
cs |