JAVA-877 Update "Redirect after login" article
This commit is contained in:
parent
20b8b8c903
commit
02ce642cbd
@ -2,6 +2,8 @@ package org.baeldung.security;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
@ -47,26 +49,21 @@ public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSu
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected String determineTargetUrl(final Authentication authentication) {
|
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();
|
final Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
|
||||||
for (final GrantedAuthority grantedAuthority : authorities) {
|
for (final GrantedAuthority grantedAuthority : authorities) {
|
||||||
if (grantedAuthority.getAuthority().equals("ROLE_USER")) {
|
|
||||||
isUser = true;
|
String authorityName = grantedAuthority.getAuthority();
|
||||||
break;
|
if(roleTargetUrlMap.containsKey(authorityName)) {
|
||||||
} else if (grantedAuthority.getAuthority().equals("ROLE_ADMIN")) {
|
return roleTargetUrlMap.get(authorityName);
|
||||||
isAdmin = true;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isUser) {
|
throw new IllegalStateException();
|
||||||
return "/homepage.html";
|
|
||||||
} else if (isAdmin) {
|
|
||||||
return "/console.html";
|
|
||||||
} else {
|
|
||||||
throw new IllegalStateException();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -83,12 +80,4 @@ public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSu
|
|||||||
session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
|
session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setRedirectStrategy(final RedirectStrategy redirectStrategy) {
|
|
||||||
this.redirectStrategy = redirectStrategy;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected RedirectStrategy getRedirectStrategy() {
|
|
||||||
return redirectStrategy;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user