61 lines
2.2 KiB
Java
61 lines
2.2 KiB
Java
package org.baeldung.security;
|
|
|
|
import java.io.IOException;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpSession;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.web.DefaultRedirectStrategy;
|
|
import org.springframework.security.web.RedirectStrategy;
|
|
import org.springframework.security.web.WebAttributes;
|
|
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
|
|
|
public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
|
|
private final Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
|
|
|
|
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
|
|
handle(request, response, authentication);
|
|
HttpSession session = request.getSession(false);
|
|
if (session != null) {
|
|
session.setMaxInactiveInterval(30);
|
|
}
|
|
clearAuthenticationAttributes(request);
|
|
}
|
|
|
|
protected void handle(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
|
|
String targetUrl = determineTargetUrl(authentication);
|
|
|
|
if (response.isCommitted()) {
|
|
logger.debug("Response has already been committed. Unable to redirect to " + targetUrl);
|
|
return;
|
|
}
|
|
|
|
redirectStrategy.sendRedirect(request, response, targetUrl);
|
|
}
|
|
|
|
protected String determineTargetUrl(Authentication authentication) {
|
|
|
|
return "/home.html";
|
|
|
|
}
|
|
|
|
protected void clearAuthenticationAttributes(HttpServletRequest request) {
|
|
HttpSession session = request.getSession(false);
|
|
if (session == null) {
|
|
return;
|
|
}
|
|
session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
|
|
}
|
|
|
|
public void setRedirectStrategy(RedirectStrategy redirectStrategy) {
|
|
this.redirectStrategy = redirectStrategy;
|
|
}
|
|
|
|
protected RedirectStrategy getRedirectStrategy() {
|
|
return redirectStrategy;
|
|
}
|
|
} |