java - Spring Security 4中的方法defineTargetUrl未被调用

标签 java spring authentication spring-security

我的 Spring MVC 4 项目中有一个 CustomLoginSucessHandler 来管理用户登录时的操作。

这工作正常。在同一个类中,我有方法 defineTargetUrl 来根据用户的 ROLE 重定向用户。

这是代码:

@Override
    protected String determineTargetUrl(HttpServletRequest request, HttpServletResponse response){
        final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        final String userName = authentication.getName();       
        log.debug("TARGET URL METHOD!");
         List<Authority> authorityList = authorityService.getAllAuthoritiesByUserName(userName);

         for(Authority authority: authorityList){
             switch (authority.getAuthority()){

             case "ROLE_ADMIN": 
                 return "processFile";

             case "ROLE_USER":
                 return "userPortal";

             case "ROLE_DEMO1":
                 return "processFile";

             case "ROLE_DEMO2":
                 return "processFile";                      
            }            

         }
        return "403";   



    }

看到我有一个log.debug("TARGET URL METHOD")

这个日志永远不会被调用,当然页面也不会被重定向,它会转到默认的登陆页面,即 processFile.html

我很困惑为什么我的 onAuthenticationSuccess 工作正常时没有调用第二个方法。他们在同一个类(class)。

以下是我如何创建 CustomLoginSucessHandler 实例的代码:

public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Autowired
    private DataSource dataSource;

    @Autowired
    private CustomLoginSucessHandler customLoginSucessHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().authenticated().and().formLogin().loginPage("/login.html")
                .loginProcessingUrl("/login").permitAll().and().logout().logoutSuccessUrl("/")
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout")).permitAll().and().exceptionHandling()
                .accessDeniedPage("/403.html");

        http.csrf().requireCsrfProtectionMatcher(new CsrfRequestMatcher());
        http.formLogin().successHandler(customLoginSucessHandler);  

    }

谢谢。

最佳答案

您试图覆盖错误的函数,这是问题的根本原因。在您提供的摘录中,您有一个似乎覆盖另一个函数:

@Override
protected String determineTargetUrl(HttpServletRequest request, HttpServletResponse response){

但事实上它并没有覆盖任何东西。如果你检查AuthenticationSuccessHandler的javadoc ,您将看到它只提供一个函数:onAuthenticationSuccess,您将其报告为“正在运行”。它可以工作,但它是一个重写函数,并且它确实作为标准登录过程的一部分被调用。如果您密切关注此示例:

CustomLoginSuccessHandler example (可能您已经遵循了此操作)

您将看到 defineTargetUrl 函数未被重写,而是由实现显式调用:

protected void handle(HttpServletRequest request, 
  HttpServletResponse response, Authentication authentication) throws IOException {
    String targetUrl = determineTargetUrl(authentication);

哪个句柄方法也被调用:

@Override
public void onAuthenticationSuccess(HttpServletRequest request, 
  HttpServletResponse response, Authentication authentication) throws IOException {
    handle(request, response, authentication);
    clearAuthenticationAttributes(request);
}

关于java - Spring Security 4中的方法defineTargetUrl未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34620026/

相关文章:

java - Slick2D:StateBasedGame 并创建菜单

java - Spring 集成: How to read the unconsumed messages in the queue?

java - Max session 1 不适用于 spring boot

java - 使用 Jetty Websocket 客户端进行摘要式身份验证

python - 如何以编程方式对 Django 中的用户进行身份验证?

javascript - 如何制作HTML5视频标签,使用Web API进行不记名认证?

java - 对特定登录客户的只读权限 | Spring MVC

java - 如何使用泛型指定与 Comparable 类型的父类(super class)关系

java - 如何修复此使用泛型的代码中的 "method not applicable to arguments"问题

java - HTTP 请求未委托(delegate)给根 WebApplicationContext 的 Controller