여러개의 세션에 동시에 접속을 하려면 SSO(single sign on : 단일 인증으로 복수의 세션을 사용한다.)를 사용해야한다.
오늘은 로그인에 대해서 배웠고 이 중 가장 중요한 개념은 session에 대한 것이다.
session은 tomcat 서버 안에 존재하는 임시저장 공간이다. 로그인을 하게되면 해당 서비스에 처음 접속했을때 부여받았던 session_id에 대한 저장공간에 구별할 수 있는 키 값이 저장되게 된다.
하나의 서버를 이용한다는 말을 다른말로바꾸면 하나의 서비스를 여러사람이 접속해서 사용한다는 말이다. 이 구별을 사용자마다 부여하는 고유 session_id로 한다.
로그인의 과정에서 가장 중요한 부분은 Controller에 따라 다른다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>로그인 페이지</title>
<script type="text/javascript"
src="resources/script/jquery/jquery-1.12.4.min.js">
</script>
<script type="text/javascript">
$(document).ready(function () {
$("#loginBtn").on("click", function () {
if($.trim($("#mId").val()) == ""){
alert("아이디를 입력해 주세요.");
$("#mId").focus();
}else if($.trim($("#mPw").val()) == ""){
alert("비밀번호를 입력해 주세요.");
$("#mPw").focus();
}else{
$("#loginForm").submit();
}
});
});
</script>
</head>
<body>
<form action="testLogins" id ="loginForm" method="post">
<!--로그인 아이디와 비밀번호를 받아서 데이터 베이스로 보낼 것 -->
아이디 <input type="text" id="mId" name="mId"><br>
비밀번호<input type="password" id="mPw" name="mPw"><br>
<input type="button" value="로그인" id="loginBtn">
</form>
</body>
</html>
package com.spring.sample.web.test.controller;
import java.util.HashMap;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.spring.sample.web.test.service.ITestLService;
@Controller
public class TestLController {
@Autowired
public ITestLService iTestLService;
// 처음으로 testLogin을 받아 오게 되면 testLogin페이지가 그려지기 전에 컨트롤러 먼저 실행이된다.
@RequestMapping(value="/testLogin")
public ModelAndView testLogin(ModelAndView mav,
HttpSession session) {
// 로그인을 하기 위해선 세션 가방이 필요하다. 해당 서비스에 입장할 때 개인 가방처럼 session_id가 주어진다.
if(session.getAttribute("sMNo") != null) {
//로그인이 되면 받은 id pw를 데이터와 비교하고 해당 이름과 아이디를 가져온다. 그걸ㄴ sMNo의 형식으로 session안에 저장해 놓게 된다.
// 그렇게 session 안에 있는 sMNo의 값이 빈값이 아니면 로그인이 되어 있다는 뜻이기 때문에 testO를 다시 찾아가게 한다.
mav.setViewName("redirect:testO");
}else {
//session안에 sMNo에 값이 없을 때는 로그인 페이지로 이동하게 한다.
mav.setViewName("test/testLogin");
}
return mav;
};
@RequestMapping(value="/testLogins") // 로그인 시간의 기본설정값은 30분이다.
public ModelAndView testLogins(
//HttpServletRequest req,
HttpSession session, //요청한 사용자 개개인에게 개별적인 session을 제공하기 때문에 전제조건으로 요청한 사용자가 있어야한다. 따라서 @Request 안에 묶여져 있다
// 세션을 제공할 때 고유sessionId를 제공해준다.(primary key 와 같다)
ModelAndView mav,
@RequestParam HashMap<String, String> params) throws Throwable {
HashMap<String,String> data
= iTestLService.getM(params);
if(data != null) {
//data에 담겨있는 반환 값은
//HttpSession session = req.getSession();
session.setAttribute("sMNo", data.get("M_NO")); // session의 값을 가져오는 키값에는 s를 앞에 붙이는 것이 통상적이다.
session.setAttribute("sMNm", data.get("M_NM")); // session의 값을 가져오는 키값에는 s를 앞에 붙이는 것이 통상적이다.
System.out.println(session.getAttribute("sMNm")); // 보관이 잘 됬는지 확인하는 작업
mav.addObject("res", "success");
} else {
mav.addObject("res", "failed");
}
mav.setViewName("test/testLogins");
return mav;
}
@RequestMapping(value="/testO")
public ModelAndView testO(ModelAndView mav) {
mav.setViewName("test/testO");
return mav;
}
@RequestMapping(value="/testLogout")
public ModelAndView testLogout(HttpSession session, ModelAndView mav) {
session.invalidate(); //session 초기화
mav.setViewName("redirect:testLogin");
return mav;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript"
src="resources/script/jquery/jquery-1.12.4.min.js">
</script>
<script type="text/javascript">
$(document).on("click", function () {
$("#logoutBtn").on("click", function () {
location.href = "testLogout";
});
});
</script>
</head>
<body>
<c:choose>
<c:when test="${empty sMNo}"> <!-- !empty는 비어있지 않다면 이라는 뜻이다.
이프문으로 sMNo의 값이 있을때와 없을때를 구분하여 띄어준다. -->
<!-- 비로그인 -->
<input type="button" value="로그인" id="loginBtn">
</c:when>
<c:otherwise>
<!-- 로그인 -->
${sMNm}님 어서오세요.<br>
<input type="button" value="로그아웃" id="logoutBtn">
</c:otherwise>
</c:choose>
</body>
</html>
'about. What I learned > about.Gudi' 카테고리의 다른 글
비동기 방식 (AJAX 사용하기) (0) | 2021.06.15 |
---|---|
한줄 게시판 만들기 (0) | 2021.06.10 |
SPRING (0) | 2021.06.07 |
SPRING - 글쓰기, 수정 흐름도 (0) | 2021.06.06 |
spring 흐름대로 복습하기 (0) | 2021.06.01 |