web developer๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป

[EgovFramework] Spring Interceptor session AJAX ์ฒ˜๋ฆฌ ๋ณธ๋ฌธ

Spring

[EgovFramework] Spring Interceptor session AJAX ์ฒ˜๋ฆฌ

natrue 2021. 10. 18. 18:04
728x90

egov-com-interceptor.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
	
    <beans profile="session">  
	    <mvc:interceptors>
	        <mvc:interceptor>
	            <mvc:mapping path="/**/*.do" />
	            <mvc:exclude-mapping path="/loginView.do" />
				<mvc:exclude-mapping path="/logOut.do" />
				<mvc:exclude-mapping path="/actionLogin.do" />
	            <bean class="egovframework.com.cmm.interceptor.LoginSessionInterceptor">
	            </bean>
	        </mvc:interceptor>
	    </mvc:interceptors> 
	</beans>
</beans>

LoginSessionInterceptor.java

preHandle() - ์„ธ์…˜ ๋ฐ ๋กœ๊ทธ์ธ ์ฒดํฌ

 request -> preHandle -> controller -> postHandle -> afterCompletion -> view
 ์„ธ์…˜๋งŒ๋ฃŒ ์‹œ ๋กœ๊ทธ์ธํŽ˜์ด์ง€๋กœ ์ด๋™ํ•œ๋‹ค.
 @return true(์š”์ฒญํ•œ controller ํ˜ธ์ถœ), false(๋กœ๊ทธ์ธํŽ˜์ด์ง€๋กœ ์ด๋™)

์„ธ์…˜ ์œ ํšจํ•˜๋‹ค๋ฉด ๋ฌธ์ œ ์—†์ด Controller๋กœ ํ†ต๊ณผ์‹œ์ผœ๋„ ๋˜๊ธฐ ๋•Œ๋ฌธ์— return true๋ฅผ ํ•ด์ฃผ๊ณ  ์œ ํšจํ•˜์ง€ ์•Š๋‹ค๋ฉด login ํŽ˜์ด์ง€๋กœ redirect ์‹œํ‚จ๋‹ค.

public class LoginSessionInterceptor extends HandlerInterceptorAdapter {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		boolean result = false;
		String webRoot = request.getContextPath();
		try {
			if(request.getSession().getAttribute("LoginResultVO") == null){
				if(isAjaxRequest(request)){
					response.sendError(400);
					result =  false;
				}else{
					response.sendRedirect(webRoot + "/loginView.do");  
					result =  false;
				}
			}else{
				result =  true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
			result =  false;
		}
		return result;
	}
	private boolean isAjaxRequest(HttpServletRequest req) {
		String header = req.getHeader("AJAX");
		if ("true".equals(header)){
			return true;
		}else{
			return false;
		}
	}
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modeAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex){
	}
}

ajax.js

ajax : function(type, url, param, dataType, callback) {
  $.ajax({
    type: type,
    url: url,
    data: param,
    dataType : dataType,
    beforeSend : function(xmlHttpRequest){
	      console.log("ajax xmlHttpRequest check");
	      xmlHttpRequest.setRequestHeader("AJAX","true");
	},
    success: function(data, textStatus, xhr) {
    	return callback(data);
    },
    error: function(xhr, status, error) {
      if(status==400){
        var offset = location.href.indexOf(location.host)+location.host.length;
        var ctxPath = location.href.substring(offset,location.href.indexOf('/',offset+1));
        location.href = ctxPath+"/loginView.do";
      }
     	return callback(data);
    }
  });
},


//๋˜๋Š” 

error: function(xhr, status, err) {
  if (xhr.status == 400) {
  	window.location.reload();
	}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

์ฐธ๊ณ  : 

 

Spring Interceptor ํ™œ์šฉ ์„ธ์…˜ ์„ค์ • ์ธํ„ฐ์…‰ํ„ฐ ์„ธ์…˜์„ค์ • ajax, ํŽ˜์ด์ง€์—ฐ๊ฒฐ ๊ตฌ๋ถ„

Spring Interceptor ํ™œ์šฉ ์„ธ์…˜ ์„ค์ • ์ธํ„ฐ์…‰ํ„ฐ ์„ธ์…˜์„ค์ • ajax, ํŽ˜์ด์ง€์—ฐ๊ฒฐ ๊ตฌ๋ถ„ ์ด์ „ ํฌ์ŠคํŒ…์—์„œ AOP๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„ธ์…˜์„ ์ฒดํฌ ํ–ˆ์—ˆ๋Š”๋ฐ.. ์ด๊ฒƒ์— ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ์ฃ . ์•„๋ฌด๋ฆฌ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ๋ฅผ ํ•ด๋„ ํŽ˜์ด์ง€๊ฐ€ ๋„˜์–ด

aljjabaegi.tistory.com