Skip to content

Commit 6458f2f

Browse files
author
eugenp
committed
work on custom success handler
1 parent dc2323f commit 6458f2f

1 file changed

Lines changed: 21 additions & 35 deletions

File tree

‎spring-security-mvc-custom/src/main/java/org/baeldung/security/MySimpleUrlAuthenticationSuccessHandler.java‎

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.io.IOException;
44
import java.util.Collection;
55

6-
import javax.servlet.ServletException;
76
import javax.servlet.http.HttpServletRequest;
87
import javax.servlet.http.HttpServletResponse;
98
import javax.servlet.http.HttpSession;
@@ -12,7 +11,6 @@
1211
import org.apache.commons.logging.LogFactory;
1312
import org.springframework.security.core.Authentication;
1413
import org.springframework.security.core.GrantedAuthority;
15-
import org.springframework.security.core.context.SecurityContextHolder;
1614
import org.springframework.security.web.DefaultRedirectStrategy;
1715
import org.springframework.security.web.RedirectStrategy;
1816
import org.springframework.security.web.WebAttributes;
@@ -27,13 +25,18 @@ protected MySimpleUrlAuthenticationSuccessHandler() {
2725
super();
2826
}
2927

30-
/**
31-
* Invokes the configured {@code RedirectStrategy} with the URL returned by the {@code determineTargetUrl} method.
32-
* <p>
33-
* The redirect will not be performed if the response has already been committed.
34-
*/
35-
protected void handle(final HttpServletRequest request, final HttpServletResponse response, final Authentication authentication) throws IOException, ServletException {
36-
final String targetUrl = determineTargetUrl(request, response);
28+
// API
29+
30+
@Override
31+
public void onAuthenticationSuccess(final HttpServletRequest request, final HttpServletResponse response, final Authentication authentication) throws IOException {
32+
handle(request, response, authentication);
33+
clearAuthenticationAttributes(request);
34+
}
35+
36+
// IMPL
37+
38+
protected void handle(final HttpServletRequest request, final HttpServletResponse response, final Authentication authentication) throws IOException {
39+
final String targetUrl = determineTargetUrl(authentication);
3740

3841
if (response.isCommitted()) {
3942
logger.debug("Response has already been committed. Unable to redirect to " + targetUrl);
@@ -43,13 +46,9 @@ protected void handle(final HttpServletRequest request, final HttpServletRespons
4346
redirectStrategy.sendRedirect(request, response, targetUrl);
4447
}
4548

46-
/**
47-
* Builds the target URL according to the logic defined in the main class Javadoc.
48-
*/
49-
protected String determineTargetUrl(final HttpServletRequest requestRaw, final HttpServletResponse response) {
49+
protected String determineTargetUrl(final Authentication authentication) {
5050
boolean isUser = false;
5151
boolean isAdmin = false;
52-
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
5352
final Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
5453
for (final GrantedAuthority grantedAuthority : authorities) {
5554
if (grantedAuthority.getAuthority().equals("ROLE_USER")) {
@@ -70,27 +69,6 @@ protected String determineTargetUrl(final HttpServletRequest requestRaw, final H
7069
}
7170
}
7271

73-
/**
74-
* Allows overriding of the behavior when redirecting to a target URL.
75-
*/
76-
public void setRedirectStrategy(final RedirectStrategy redirectStrategy) {
77-
this.redirectStrategy = redirectStrategy;
78-
}
79-
80-
protected RedirectStrategy getRedirectStrategy() {
81-
return redirectStrategy;
82-
}
83-
84-
/**
85-
* Calls the parent class {@code handle()} method to forward or redirect to the target URL, and
86-
* then calls {@code clearAuthenticationAttributes()} to remove any leftover session data.
87-
*/
88-
@Override
89-
public void onAuthenticationSuccess(final HttpServletRequest request, final HttpServletResponse response, final Authentication authentication) throws IOException, ServletException {
90-
handle(request, response, authentication);
91-
clearAuthenticationAttributes(request);
92-
}
93-
9472
/**
9573
* Removes temporary authentication-related data which may have been stored in the session
9674
* during the authentication process.
@@ -105,4 +83,12 @@ protected final void clearAuthenticationAttributes(final HttpServletRequest requ
10583
session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
10684
}
10785

86+
public void setRedirectStrategy(final RedirectStrategy redirectStrategy) {
87+
this.redirectStrategy = redirectStrategy;
88+
}
89+
90+
protected RedirectStrategy getRedirectStrategy() {
91+
return redirectStrategy;
92+
}
93+
10894
}

0 commit comments

Comments
 (0)