로메오의 블로그

[Spring boot] HttpSession 세션 처리 - Interceptor 본문

Backend/Spring

[Spring boot] HttpSession 세션 처리 - Interceptor

romeoh 2019. 10. 15. 14:30
반응형

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

 

 

Spring Boot / Oracle / Mybatis 차례

반응형
Comments