【问题标题】:Authentication failure redirect with request params not working请求参数不起作用的身份验证失败重定向
【发布时间】:2017-02-11 21:08:25
【问题描述】:

我正在尝试配置我自己的成功和身份验证失败处理程序。在身份验证失败时,我想使用请求参数重定向回我的登录页面,此参数的存在将在我的登录页面上输出错误消息。然而,尽管出错时我被重定向回我的登录页面,但请求参数始终为null

代码如下:

protected void configure(HttpSecurity http) throws Exception {
    http
        .csrf().disable()
        .authorizeRequests()
            .antMatchers("/").permitAll()
            .antMatchers("/login").permitAll()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login.html").permitAll() 
            .usernameParameter("username")
            .passwordParameter("password")                                               
            .loginProcessingUrl("/login")
            .successHandler(successHandler())
            .failureHandler(handleAuthenticationFailure());
}

@Autowired
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    //database checks
}
};
}

/**
 * Authentication success handler defines action when successfully authenticated
 * @return
 */
@Bean
public AuthenticationSuccessHandler successHandler(){
    return new AuthenticationSuccessHandler() {

        @Override
        public void onAuthenticationSuccess(HttpServletRequest httpRequest, HttpServletResponse httpResponse, Authentication authentication)
                throws IOException, ServletException {

            // custom auth success here
            httpResponse.setStatus(HttpServletResponse.SC_OK);
            SavedRequest savedRequest = (SavedRequest) httpRequest.getSession().getAttribute("SPRING_SECURITY_SAVED_REQUEST");
            httpResponse.sendRedirect(savedRequest.getRedirectUrl());
        }
    };
}

@Bean
public AuthenticationFailureHandler handleAuthenticationFailure() {
    return new SimpleUrlAuthenticationFailureHandler() {

        @Override
        public void onAuthenticationFailure(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
                                            AuthenticationException authenticationException) throws IOException, ServletException {

            // custom failure code here
            setDefaultFailureUrl("/login.html?error=fail");
            super.onAuthenticationFailure(httpRequest, httpResponse, authenticationException);
        }
    };
}

【问题讨论】:

  • 如何获取参数?能否请您跟踪网络以查找是否使用error=fail 请求了该url?

标签: spring spring-boot spring-security


【解决方案1】:

试试这个:

@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {

    // .......

    response.sendRedirect("/login.html?error=fail");    
}

更新:

将“/login.html?error=fail”添加到 authorizeRequests() 部分非常重要,否则控制器不会获取错误参数。

.antMatchers("/login").permitAll() 替换为.antMatchers("/login**").permitAll()

【讨论】:

    【解决方案2】:

    参数也有问题(在我的情况下,当登录失败并且一些请求参数被添加到 url 它重定向到没有参数的登录页面时)。

    这解决了我的问题

    .antMatchers("/login**").permitAll()
    

    【讨论】:

      【解决方案3】:

      我是springBoot新手,如果你用的是spring boot 2.1.4.RELEASE,试试这个配置:

      http.csrf().disable()
                  .authorizeRequests()
                  // URLs matching for access rights
                  .antMatchers("/").permitAll()
                  .antMatchers("/login").permitAll()
                  .anyRequest().authenticated()
                  .and()
                  // form login
                  .formLogin()
                  .loginPage("/login")
                  .failureUrl("/login?error=true")
                  .successHandler(sucessHandler)
                  .usernameParameter("email")
                  .passwordParameter("password")
                  .and()
                  // logout
                  .logout()
                  .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                  .logoutSuccessUrl("/").and()
                  .exceptionHandling()
                  .accessDeniedPage("/access-denied");
      

      要使用上面定义的 Spring Security 配置,我们需要将它附加到 Web 应用程序。在这种情况下,我们不需要任何 web.xml:

      public class SpringApplicationInitializer 
      
      
      extends AbstractAnnotationConfigDispatcherServletInitializer {
      
      
      protected Class<?>[] getRootConfigClasses() {
          return new Class[] {SecSecurityConfig.class};
      }}
      

      这意味着您创建了以下将自动实例化的类

      SecSecurityConfig.class :是您执行所有 http.csrf().disable().authorizeRequests()... 配置的类

      来源:https://www.baeldung.com/spring-security-login

      希望对你有帮助:)

      【讨论】:

        猜你喜欢
        • 2016-06-28
        • 2016-04-03
        • 1970-01-01
        • 2016-11-04
        • 2020-02-03
        • 2018-07-02
        • 2010-10-20
        • 2021-11-23
        • 1970-01-01
        相关资源
        最近更新 更多