Spring Security条件default-target-url

我注意到有几个问题询问这个话题。 我查看了它们,我无法将它们应用到我特定的Spring设置中。 我想根据用户的角色将我的登录重定向配置为有条件的。 这是我到目前为止:

      

我认为这个问题可能与我想要做的事情在同一条线上。 有人知道我怎么能申请吗?

编辑1

        

编辑2

目前我没有类public class Test implements AuthenticationSuccessHandler {} ,如本例所示。

我测试了代码并且它有效,其中没有火箭科学

 public class MySuccessHandler implements AuthenticationSuccessHandler { @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { Set roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities()); if (roles.contains("ROLE_ADMIN")){ response.sendRedirect("/Admin.html"); return; } response.sendRedirect("/User.html"); } } 

安全上下文中的更改:

    

更新如果要使用default-target-url方法,它将同样有效,但会在用户首次访问登录页面时触发:

 @Controller public class WelcomeController { @RequestMapping(value = "/welcome.htm") protected View welcome() { Set roles = AuthorityUtils .authorityListToSet(SecurityContextHolder.getContext() .getAuthentication().getAuthorities()); if (roles.contains("ROLE_ADMIN")) { return new RedirectView("Admin.htm"); } return new RedirectView("User.htm"); } }