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();
}
}
์ฐธ๊ณ :