단순 코드 기록/Spring

Spring_Intercept_for_login

일일일코_장민기 2024. 2. 14. 12:07
728x90
Controller

 

package com.app.test;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class TestController {

	@RequestMapping(value =  {"/main", "/home", "main2"}, method = RequestMethod.GET)	
	public String main(LoginDTO loginDTO, HttpSession session) {
		
		//main:	session 로그인 정보가 있으면 main.jsp
		//로그인 정보가 없는 경우, loginForm.jsp
		//main2는 인터셉터 처리가 되지 않았기 때문에 그냥 이동됨
		
		System.out.println("Controller " + loginDTO);
		return "main";
	}

	@RequestMapping(value =  "/loginForm", method = RequestMethod.GET)	//그냥 주소 처리
	public String loginForm() {
		System.out.println("Controller - loginForm");
		return "loginForm";
	}

	@RequestMapping(value =  "/login", method = RequestMethod.GET)
	public ModelAndView login(String userid, String passwd, HttpSession session) {

		ModelAndView mav = new ModelAndView();
		session.setAttribute("login", userid);		//인터셉터의 prehandle에서 확인
		
		mav.addObject("userid", userid);
		mav.addObject("passwd", passwd);
		mav.setViewName("logined");					//로그인 jsp로 이동
		
		System.out.println("Controller - login");
		return mav;
	}
	
	
	
	
}

 

Intercept

 

package com.app.test;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class MyHandlerInterceptor extends HandlerInterceptorAdapter{

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
		//main과 home만 동작 -> 특정 주소로 한정하지 않으면 루프 돌 수 있음
		
		System.out.println("preHandle");
		
		HttpSession session = request.getSession();
		//servelt-context.xml에서 main과 home만 처리하도록 설정
		if(session.getAttribute("login")== null){
			System.out.println("intercept 세션 정보 없음");
			response.sendRedirect("loginForm");
		}
		return true;	//true면 계속 진행, false면 진행 금지
	}
	
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception{
		System.out.println("postHandle");		
	}
	
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception{
		System.out.println("afterCompletion");		
	}
	
}

 

Servlet-context.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

	<resources mapping="/resources/**" location="/resources/" />

	<beans:bean id="xxx" class="com.app.test.TestController"></beans:bean>
	<beans:bean id="myHandlerInterceptor" class="com.app.test.MyHandlerInterceptor"></beans:bean>

	<interceptors>		<!-- 특정 요청 처리 한정으로 인터셉터 처리 -->
		<interceptor>
			<mapping path="/main"/>
			<beans:ref bean="myHandlerInterceptor"/>
		</interceptor>
		<interceptor>
			<mapping path="/home"/>
			<beans:ref bean="myHandlerInterceptor"/>
		</interceptor>
	</interceptors>



	<annotation-driven />
	
	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>
	
	
	
</beans:beans>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

'단순 코드 기록 > Spring' 카테고리의 다른 글

Spring_ajax&JSON(@RequestBody)  (0) 2024.02.14
Spring_보안폴더에서 외부파일 사용+xml주소처리  (0) 2024.02.14
Spring_Intercept  (0) 2024.02.14
Spring_Redirect_Forwarding  (0) 2024.02.14
Spring_URLMAPPING  (0) 2024.02.14