OWL ITS + 탐지시스템(인터넷 진흥원)
jhjang
2021-10-14 d680ff9fa4298ad3c0cd12f5f9d87f6c51110480
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package kr.wisestone.owl.config.security.filter;
 
import kr.wisestone.owl.constant.Constants;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.session.SessionInformation;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.security.web.util.UrlUtils;
import org.springframework.util.Assert;
import org.springframework.web.filter.GenericFilterBean;
 
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
 
public class AjaxSessionExpiredFilter extends GenericFilterBean {
    private final Logger log = LoggerFactory.getLogger(AjaxSessionExpiredFilter.class);
 
    private String ajaxHeader;
    private SessionRegistry sessionRegistry;
    private String expiredUrl;
    private LogoutHandler[] handlers = new LogoutHandler[]{new SecurityContextLogoutHandler()};
    private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
 
    public void destroy() {
    }
 
    public AjaxSessionExpiredFilter(SessionRegistry sessionRegistry) {
        Assert.notNull(sessionRegistry, "SessionRegistry required");
        this.sessionRegistry = sessionRegistry;
    }
 
    public AjaxSessionExpiredFilter(SessionRegistry sessionRegistry, String expiredUrl) {
        Assert.notNull(sessionRegistry, "SessionRegistry required");
        Assert.isTrue(expiredUrl == null || UrlUtils.isValidRedirectUrl(expiredUrl), expiredUrl + " isn\'t a valid redirect URL");
        this.sessionRegistry = sessionRegistry;
        this.expiredUrl = expiredUrl;
    }
 
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)res;
        HttpSession session = request.getSession(false);
 
        String uri = request.getRequestURI();
        if (FilenameUtils.isExtension(uri, "html") || uri.contains("/user/getUserSession") || uri.contains("/owl-socket")) {
            chain.doFilter(request, response);
            return;
        }
 
        if(session != null) {
            SessionInformation info = this.sessionRegistry.getSessionInformation(session.getId());
            if(info != null) {
                if(info.isExpired()) {
                    this.doLogoutAndFireErrorMessage(request, response);
                    return;
                }
 
                this.sessionRegistry.refreshLastRequest(info.getSessionId());
            }
        }
 
        chain.doFilter(request, response);
    }
 
    private void doLogoutAndFireErrorMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doLogout(request, response);
        response.sendRedirect(Constants.SESSION_EXPIRE_REDIRECT_URL);
    }
 
    private void doLogout(HttpServletRequest request, HttpServletResponse response) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        LogoutHandler[] var4 = this.handlers;
        int var5 = var4.length;
 
        for(int var6 = 0; var6 < var5; ++var6) {
            LogoutHandler handler = var4[var6];
            handler.logout(request, response, auth);
        }
 
    }
}