Skip to content

Commit 984a0bf

Browse files
author
eugenp
committed
finishing up redirect work
1 parent 3a1897d commit 984a0bf

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.baeldung.security;
22

33
import java.io.IOException;
4+
import java.util.Collection;
45

56
import javax.servlet.ServletException;
67
import javax.servlet.http.HttpServletRequest;
@@ -10,11 +11,11 @@
1011
import org.apache.commons.logging.Log;
1112
import org.apache.commons.logging.LogFactory;
1213
import org.springframework.security.core.Authentication;
14+
import org.springframework.security.core.GrantedAuthority;
1315
import org.springframework.security.web.DefaultRedirectStrategy;
1416
import org.springframework.security.web.RedirectStrategy;
1517
import org.springframework.security.web.WebAttributes;
1618
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
17-
import org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper;
1819

1920
public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
2021
protected final Log logger = LogFactory.getLog(this.getClass());
@@ -47,9 +48,20 @@ protected void handle(final HttpServletRequest request, final HttpServletRespons
4748
protected String determineTargetUrl(final HttpServletRequest requestRaw, final HttpServletResponse response) {
4849
// Check for the parameter and use that if available
4950

50-
final SecurityContextHolderAwareRequestWrapper req = (SecurityContextHolderAwareRequestWrapper) requestRaw;
51-
final boolean isUser = req.isUserInRole("ROLE_USER");
52-
final boolean isAdmin = req.isUserInRole("ROLE_ADMIN");
51+
boolean isUser = false;
52+
boolean isAdmin = false;
53+
final Authentication authentication = org.springframework.security.core.context.SecurityContextHolder.getContext().getAuthentication();
54+
final Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
55+
for (final GrantedAuthority grantedAuthority : authorities) {
56+
if (grantedAuthority.getAuthority().equals("ROLE_USER")) {
57+
isUser = true;
58+
break;
59+
} else if (grantedAuthority.getAuthority().equals("ROLE_ADMIN")) {
60+
isAdmin = true;
61+
break;
62+
}
63+
}
64+
5365
if (isUser) {
5466
return "/homepage.html";
5567
} else if (isAdmin) {

0 commit comments

Comments
 (0)