Update "Redirect after login" article
@@ -2,6 +2,8 @@ package org.baeldung.security;
import java.io.IOException;
import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -47,27 +49,22 @@ public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSu
}
protected String determineTargetUrl(final Authentication authentication) {
- boolean isUser = false;
- boolean isAdmin = false;
+
+ Map<String, String> roleTargetUrlMap = new HashMap<>();
+ roleTargetUrlMap.put("ROLE_USER", "/homepage.html");
+ roleTargetUrlMap.put("ROLE_ADMIN", "/console.html");
+
final Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
for (final GrantedAuthority grantedAuthority : authorities) {
- if (grantedAuthority.getAuthority().equals("ROLE_USER")) {
- isUser = true;
- break;
- } else if (grantedAuthority.getAuthority().equals("ROLE_ADMIN")) {
- isAdmin = true;
- break;
+
+ String authorityName = grantedAuthority.getAuthority();
+ if(roleTargetUrlMap.containsKey(authorityName)) {
+ return roleTargetUrlMap.get(authorityName);
}
}
- if (isUser) {
- return "/homepage.html";
- } else if (isAdmin) {
- return "/console.html";
- } else {
throw new IllegalStateException();
}
- }
/**
* Removes temporary authentication-related data which may have been stored in the session
@@ -83,12 +80,4 @@ public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSu
session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
}
- public void setRedirectStrategy(final RedirectStrategy redirectStrategy) {
- this.redirectStrategy = redirectStrategy;
- }
-
- protected RedirectStrategy getRedirectStrategy() {
- return redirectStrategy;
- }
-
}
