【问题标题】:Spring Security with Java Configuration: How to handle BadCredentialsException from a custom provider带有 Java 配置的 Spring Security:如何处理来自自定义提供程序的 BadCredentialsException
【发布时间】:2014-03-13 20:24:00
【问题描述】:

我需要使用 url 中的令牌 id(或者可能在请求标头中 - 但这暂时不重要)来验证一些休息服务。我正在尝试使用 java 配置来设置它,并以此 post 为指导。我的问题是我不知道如何处理提供者身份验证失败时抛出的“BadCredentialsException”。这是我的安全配置:

public static class SecurityConfigForRS extends
        WebSecurityConfigurerAdapter {

    @Autowired
    TokenAuthenticationProvider tokenAuthenticationProvider;

    @Override
    protected void configure(AuthenticationManagerBuilder auth)
            throws Exception {
        auth.authenticationProvider(tokenAuthenticationProvider);
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean()
            throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);

        http.regexMatcher("^/rest.*")
                .addFilterBefore(
                        new TokenAuthenticationFilter(
                                authenticationManagerBean()),
                        AbstractPreAuthenticatedProcessingFilter.class)
                .and().csrf().disable();

    }
}

现在我跳过其他实现 - 如果有帮助,我会在稍后发布它们。

当令牌丢失或无效时,TokenAuthernticationProvider 会抛出 BadCredentialsException。我需要抓住这个并发回401-Unauthorized。可以这样做吗?

【问题讨论】:

    标签: spring-security spring-java-config


    【解决方案1】:

    我创建的第一个过滤器是 GenericFilterBean 的子类,它不支持身份验证失败处理程序或成功处理程序。但是 AbstractAuthenticationProcessingFilter 支持成功和失败处理程序。我的过滤器就这么简单:

    public class TokenAuthenticationProcessingFilter extends
        AbstractAuthenticationProcessingFilter {
    
    public TokenAuthenticationProcessingFilter(
            RequestMatcher requiresAuthenticationRequestMatcher) {
        super(requiresAuthenticationRequestMatcher);
    }
    
    @Override
    public Authentication attemptAuthentication(HttpServletRequest request,
            HttpServletResponse response) throws AuthenticationException,
            IOException, ServletException {
        Authentication auth = new TokenAuthentication("-1");
        try {
            Map<String, String[]> params = request.getParameterMap();
            if (!params.isEmpty() && params.containsKey("auth_token")) {
                String token = params.get("auth_token")[0];
                if (token != null) {
                    auth = new TokenAuthentication(token);
                }
            }
            return this.getAuthenticationManager().authenticate(auth);
        } catch (AuthenticationException ae) {
            unsuccessfulAuthentication(request, response, ae);
        }
        return auth;
    }}
    

    而我的 http 安全性是:

        public static class SecurityConfigForRS extends
            WebSecurityConfigurerAdapter {
    
        @Autowired
        TokenAuthenticationProvider tokenAuthenticationProvider;
    
        @Override
        protected void configure(AuthenticationManagerBuilder auth)
                throws Exception {
            auth.authenticationProvider(tokenAuthenticationProvider);
        }
    
        @Bean
        @Override
        public AuthenticationManager authenticationManagerBean()
                throws Exception {
            return super.authenticationManagerBean();
        }
    
        @Bean
        protected AbstractAuthenticationProcessingFilter getTokenAuthFilter()
                throws Exception {
            TokenAuthenticationProcessingFilter tapf = new TokenAuthenticationProcessingFilter(
                    new RegexRequestMatcher("^/rest.*", null));
            tapf.setAuthenticationManager(authenticationManagerBean());
            return tapf;
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            super.configure(http);
    
            http.regexMatcher("^/rest.*")
                    .addFilterAfter(getTokenAuthFilter(),
                            BasicAuthenticationFilter.class).csrf().disable();
    
        }
    }
    

    过滤器链顺序确实很重要!我把它放在 BasicAuthenticationFilter 之后,它工作正常。当然可能有更好的解决方案,但目前可行!

    【讨论】:

      猜你喜欢
      • 2015-07-10
      • 2016-08-26
      • 2015-08-16
      • 2014-05-01
      • 2016-03-26
      • 1970-01-01
      • 1970-01-01
      • 2011-02-09
      • 1970-01-01
      相关资源
      最近更新 更多