반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- 리눅스
- 맥
- 센토스
- androidstudio
- unittest
- localserver
- MAC
- 개발
- qunit
- avds
- ReactNative
- centos
- MachineLearning
- TensorFlow
- build
- webpack
- jest
- vsCode
- linux
- picker
- Android
- 네트워크
- IOS
- Chrome
- react
- PYTHON
- xcode
- VirtualBox
- node
Archives
- Today
- Total
로메오의 블로그
[Spring boot] HttpSession 세션 처리 - Interceptor 본문
반응형
Spring Boot / Oracle / Mybatis 차례
PersonController.java
package com.gaeyou.firstproject.controller;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gaeyou.firstproject.model.PersonModel;
import com.gaeyou.firstproject.service.PersonService;
@CrossOrigin
@RestController
@RequestMapping("/persons")
public class PersonController {
@Autowired
PersonService personService;
@RequestMapping("/login")
public void doLogin(HttpSession session, Model model) {
personService.doLogin(session, model);
}
@RequestMapping("/logout")
public void doLogout(HttpSession session, Model model) {
personService.doLogout(session, model);
}
@RequestMapping("/all")
public PersonModel getAll(HttpSession session) {
return personService.getPerson(session);
}
}
PersonService.java
package com.gaeyou.firstproject.service;
import javax.servlet.http.HttpSession;
import org.springframework.ui.Model;
import com.gaeyou.firstproject.model.PersonModel;
public interface PersonService {
void doLogin(HttpSession session, Model model);
void doLogout(HttpSession session, Model model);
PersonModel getPerson(HttpSession session);
}
PersonServiceImpl.java
package com.gaeyou.firstproject.service.impl;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import com.gaeyou.firstproject.dao.PersonDao;
import com.gaeyou.firstproject.interceptor.AuthenticationInterceptor;
import com.gaeyou.firstproject.model.PersonModel;
import com.gaeyou.firstproject.service.PersonService;
@Service
public class PersonServiceImpl implements PersonService {
Logger log = LoggerFactory.getLogger(AuthenticationInterceptor.class);
@Autowired
private PersonDao personDao;
@Override
public void doLogin(HttpSession session, Model model) {
PersonModel person = personDao.getPerson();
session.setAttribute("user", person);
}
@Override
public void doLogout(HttpSession session, Model model) {
session.removeAttribute("user");
}
@Override
public PersonModel getPerson(HttpSession session) {
PersonModel person = (PersonModel) session.getAttribute("user");
String id = null;
if (person != null) {
id = person.getId();
log.info("get data ok: " + person);
}
return person;
}
}
PersonDao.java
package com.gaeyou.firstproject.dao;
import com.gaeyou.firstproject.model.PersonModel;
public interface PersonDao {
PersonModel doLogin();
PersonModel getPerson();
}
main.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gaeyou.firstproject.dao.PersonDao">
<select id="doLogin" resultType="com.gaeyou.firstproject.model.PersonModel">
SELECT
'romeoh' AS ID
, '1234' AS PW
FROM DUAL
</select>
<select id="getPerson" resultType="com.gaeyou.firstproject.model.PersonModel">
SELECT
'romeoh' AS ID
, '1234' AS PW
FROM DUAL
</select>
</mapper>
AuthenticationInterceptor.java
package com.gaeyou.firstproject.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.gaeyou.firstproject.model.PersonModel;
@Component
public class AuthenticationInterceptor extends HandlerInterceptorAdapter {
Logger log = LoggerFactory.getLogger(AuthenticationInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// log.info("preHandle");
HttpSession httpSession = request.getSession();
PersonModel person = (PersonModel) httpSession.getAttribute("user");
if (person == null) {
log.info("로그아웃 상태");
throw new Exception("로그인 해주세요.");
}
log.info("로그인 되어 있음");
httpSession.setMaxInactiveInterval(30*60);
return true;
}
@Override
public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
// log.info("postHandle");
super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
// log.info("afterCompletion");
super.afterCompletion(request, response, handler, ex);
}
}
InterceptorConfig.java
package com.gaeyou.firstproject.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Autowired
private HandlerInterceptor interceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(interceptor)
.addPathPatterns("/**")
.excludePathPatterns("/persons/login");
WebMvcConfigurer.super.addInterceptors(registry);
}
}
로그인: http://localhost:8088/persons/login
페이지: http://localhost:8088/persons/all
로그아웃: http://localhost:8088/persons/logout
반응형
'Backend > Spring' 카테고리의 다른 글
[Spring Boot] myBatis Procedure / function 호출 (0) | 2019.12.04 |
---|---|
[Spring boot] 예외처리 - GlobalExceptionHandler - ControllerAdvice (0) | 2019.10.15 |
[Spring boot] mybatis SQL 로그 찍기 - log4jdbc (0) | 2019.10.12 |
[Spring Boot] 프로젝트 구조 정리하기 (0) | 2019.09.21 |
[Spring Boot] Spring Boot - My Batis 연동 - Oracle (12) | 2019.09.19 |
Comments