根据用户的角色,我需要在记录后重定向到不同的页面。我为此使用了类CustomSuccess Handler:
@Component
public class CustomSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
@Override
protected void handle(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
String url = determineTargetUrl(authentication);
if(response.isCommitted()){
return;
}
redirectStrategy.sendRedirect(request, response, url);
}
protected String determineTargetUrl(Authentication authentication){
Collection<? extends GrantedAuthority> authorities =
authentication.getAuthorities();
List<String> roles = new ArrayList<>();
for(GrantedAuthority authority: authorities){
roles.add(authority.getAuthority());
}
if(roles.contains("ROLE_ADMIN")){
return "/admin/adminPage";
}else if(roles.contains("ROLE_USER")){
return "/personal/profile";
}else return "/login";
}
}
我需要SavedRequestAwareAuthenticationSuccessHandler来重定向到targetUrl(如果存在)。
@Bean
public SavedRequestAwareAuthenticationSuccessHandler
savedRequestAwareAuthenticationSuccessHandler() {
SavedRequestAwareAuthenticationSuccessHandler auth
= new SavedRequestAwareAuthenticationSuccessHandler();
auth.setTargetUrlParameter("targetUrl");
return auth;
}
方法http.successHandler(...)会覆盖successHandler的先前值。 如何在我的应用程序中使用这两个类?请帮助)