|
1 | 1 | package org.baeldung.security; |
2 | 2 |
|
3 | 3 | import java.io.IOException; |
| 4 | +import java.util.Collection; |
4 | 5 |
|
5 | 6 | import javax.servlet.ServletException; |
6 | 7 | import javax.servlet.http.HttpServletRequest; |
|
10 | 11 | import org.apache.commons.logging.Log; |
11 | 12 | import org.apache.commons.logging.LogFactory; |
12 | 13 | import org.springframework.security.core.Authentication; |
| 14 | +import org.springframework.security.core.GrantedAuthority; |
13 | 15 | import org.springframework.security.web.DefaultRedirectStrategy; |
14 | 16 | import org.springframework.security.web.RedirectStrategy; |
15 | 17 | import org.springframework.security.web.WebAttributes; |
16 | 18 | import org.springframework.security.web.authentication.AuthenticationSuccessHandler; |
17 | | -import org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper; |
18 | 19 |
|
19 | 20 | public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler { |
20 | 21 | protected final Log logger = LogFactory.getLog(this.getClass()); |
@@ -47,9 +48,20 @@ protected void handle(final HttpServletRequest request, final HttpServletRespons |
47 | 48 | protected String determineTargetUrl(final HttpServletRequest requestRaw, final HttpServletResponse response) { |
48 | 49 | // Check for the parameter and use that if available |
49 | 50 |
|
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 | + |
53 | 65 | if (isUser) { |
54 | 66 | return "/homepage.html"; |
55 | 67 | } else if (isAdmin) { |
|
0 commit comments