| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.student.security.filter; |
| 17 | |
|
| 18 | |
import java.io.IOException; |
| 19 | |
|
| 20 | |
import javax.servlet.FilterChain; |
| 21 | |
import javax.servlet.ServletException; |
| 22 | |
import javax.servlet.ServletRequest; |
| 23 | |
import javax.servlet.ServletResponse; |
| 24 | |
import javax.servlet.http.HttpServletRequest; |
| 25 | |
import javax.servlet.http.HttpServletResponse; |
| 26 | |
|
| 27 | |
import org.springframework.security.core.Authentication; |
| 28 | |
import org.springframework.security.core.context.SecurityContextHolder; |
| 29 | |
import org.springframework.security.web.DefaultRedirectStrategy; |
| 30 | |
import org.springframework.security.web.authentication.logout.LogoutHandler; |
| 31 | |
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler; |
| 32 | |
import org.springframework.security.web.util.UrlUtils; |
| 33 | |
import org.springframework.util.Assert; |
| 34 | |
import org.springframework.util.StringUtils; |
| 35 | |
import org.springframework.web.filter.GenericFilterBean; |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
public class KSLogoutFilter extends GenericFilterBean { |
| 44 | |
|
| 45 | 0 | private String filterProcessesUrl = "/j_spring_security_logout"; |
| 46 | 0 | private String logoutSuccessUrl = "/"; |
| 47 | |
private LogoutHandler[] handlers; |
| 48 | 0 | String invalidateSession = "true"; |
| 49 | |
|
| 50 | 0 | public KSLogoutFilter(){ |
| 51 | 0 | handlers = new SecurityContextLogoutHandler[1]; |
| 52 | 0 | SecurityContextLogoutHandler sclh = new SecurityContextLogoutHandler(); |
| 53 | 0 | if ("true".equals(invalidateSession)) { |
| 54 | 0 | sclh.setInvalidateHttpSession(true); |
| 55 | |
} else { |
| 56 | 0 | sclh.setInvalidateHttpSession(false); |
| 57 | |
} |
| 58 | 0 | handlers[0] = sclh; |
| 59 | 0 | } |
| 60 | |
|
| 61 | 0 | public KSLogoutFilter(String logoutSuccessUrl, LogoutHandler[] handlers) { |
| 62 | 0 | Assert.notEmpty(handlers, "LogoutHandlers are required"); |
| 63 | 0 | this.logoutSuccessUrl = logoutSuccessUrl; |
| 64 | 0 | Assert.isTrue(UrlUtils.isValidRedirectUrl(logoutSuccessUrl), logoutSuccessUrl + " isn't a valid redirect URL"); |
| 65 | 0 | this.handlers = handlers; |
| 66 | 0 | } |
| 67 | |
|
| 68 | |
public void doFilter(ServletRequest request, ServletResponse response, |
| 69 | |
FilterChain chain) throws IOException, ServletException { |
| 70 | 0 | if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) { |
| 71 | 0 | doFilterHttp((HttpServletRequest) request, |
| 72 | |
(HttpServletResponse) response, chain); |
| 73 | |
} else { |
| 74 | |
|
| 75 | |
} |
| 76 | 0 | } |
| 77 | |
|
| 78 | |
public void doFilterHttp(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, |
| 79 | |
ServletException { |
| 80 | |
|
| 81 | 0 | if (requiresLogout(request, response)) { |
| 82 | 0 | Authentication auth = SecurityContextHolder.getContext().getAuthentication(); |
| 83 | |
|
| 84 | 0 | if (logger.isDebugEnabled()) { |
| 85 | 0 | logger.debug("Logging out user '" + auth + "' and redirecting to logout page"); |
| 86 | |
} |
| 87 | |
|
| 88 | 0 | for (int i = 0; i < handlers.length; i++) { |
| 89 | 0 | handlers[i].logout(request, response, auth); |
| 90 | |
} |
| 91 | |
|
| 92 | 0 | String targetUrl = determineTargetUrl(request, response); |
| 93 | |
|
| 94 | 0 | sendRedirect(request, response, targetUrl); |
| 95 | |
|
| 96 | 0 | return; |
| 97 | |
} |
| 98 | |
|
| 99 | 0 | chain.doFilter(request, response); |
| 100 | 0 | } |
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
protected boolean requiresLogout(HttpServletRequest request, HttpServletResponse response) { |
| 111 | 0 | String uri = request.getRequestURI(); |
| 112 | 0 | int pathParamIndex = uri.indexOf(';'); |
| 113 | |
|
| 114 | 0 | if (pathParamIndex > 0) { |
| 115 | |
|
| 116 | 0 | uri = uri.substring(0, pathParamIndex); |
| 117 | |
} |
| 118 | |
|
| 119 | 0 | int queryParamIndex = uri.indexOf('?'); |
| 120 | |
|
| 121 | 0 | if (queryParamIndex > 0) { |
| 122 | |
|
| 123 | 0 | uri = uri.substring(0, queryParamIndex); |
| 124 | |
} |
| 125 | |
|
| 126 | 0 | if ("".equals(request.getContextPath())) { |
| 127 | 0 | return uri.endsWith(filterProcessesUrl); |
| 128 | |
} |
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | 0 | return uri.endsWith(filterProcessesUrl); |
| 135 | |
} |
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
protected String determineTargetUrl(HttpServletRequest request, HttpServletResponse response) { |
| 147 | 0 | String targetUrl = request.getParameter("logoutSuccessUrl"); |
| 148 | |
|
| 149 | 0 | if(!StringUtils.hasLength(targetUrl)) { |
| 150 | 0 | targetUrl = getLogoutSuccessUrl(); |
| 151 | |
} |
| 152 | |
|
| 153 | 0 | if (!StringUtils.hasLength(targetUrl)) { |
| 154 | 0 | targetUrl = request.getHeader("Referer"); |
| 155 | |
} |
| 156 | |
|
| 157 | 0 | if (!StringUtils.hasLength(targetUrl)) { |
| 158 | 0 | targetUrl = "/"; |
| 159 | |
} |
| 160 | |
|
| 161 | 0 | return targetUrl; |
| 162 | |
} |
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
protected void sendRedirect(HttpServletRequest request, HttpServletResponse response, String url) |
| 174 | |
throws IOException { |
| 175 | |
|
| 176 | 0 | new DefaultRedirectStrategy().sendRedirect(request, response, url); |
| 177 | |
|
| 178 | 0 | } |
| 179 | |
|
| 180 | |
public void setFilterProcessesUrl(String filterProcessesUrl) { |
| 181 | 0 | Assert.hasText(filterProcessesUrl, "FilterProcessesUrl required"); |
| 182 | 0 | Assert.isTrue(UrlUtils.isValidRedirectUrl(filterProcessesUrl), filterProcessesUrl + " isn't a valid redirect URL"); |
| 183 | 0 | this.filterProcessesUrl = filterProcessesUrl; |
| 184 | 0 | } |
| 185 | |
|
| 186 | |
protected String getLogoutSuccessUrl() { |
| 187 | 0 | return logoutSuccessUrl; |
| 188 | |
} |
| 189 | |
|
| 190 | |
protected String getFilterProcessesUrl() { |
| 191 | 0 | return filterProcessesUrl; |
| 192 | |
} |
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
} |