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
'Spring' 카테고리의 다른 글
| [Spring] javax.servlet.ServletException: No adapter for handler 해결방법 (0) | 2021.11.10 |
|---|---|
| [JAVA] java.lang.Integer cannot be cast to java.lang.String 형변환 오류 해결하기 (0) | 2021.10.29 |
| [EgovFramework] context-syslogaop.xml aop Error (0) | 2021.10.12 |
| 로그인 양방향 암호화(RSA) 단방향 암호화(SHA 256) 사용하기 (0) | 2021.09.03 |
| [Spring Error] Can not find the tag library descriptor for (0) | 2021.08.31 |