当前位置: 首页 > 知识库问答 >
问题:

Spring Security with Java Configuration:如何处理来自自定义提供程序的BadCredentialsException

公羊俭
2023-03-14
java prettyprint-override">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();

    }
}

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

缺少令牌或令牌无效时,TokeAuthernticationProvider引发BadCredentialsException。我需要捕获这个并发回401-unauthorized。有可能做到这一点吗?

共有1个答案

齐财
2023-03-14

我创建的第一个过滤器是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之后,它工作得很好。当然,可能有一个更好的解决方案,但目前这是可行的!

 类似资料:
  • 可以通过实现接口或扩展任何现有的实现来创建自己的自定义处理程序。 在下面的示例中,我们通过扩展类创建了自定义处理程序。 为了理解上述与DBUtils相关的概念,让我们编写一个将运行读取查询的示例。 创建一个示例应用程序。 更新在DBUtils入门程序中创建的文件:MainApp.java。 按照下面的说明编译并运行应用程序。 以下是的内容。 以下是文件的内容。 以下是文件的内容。 完成创建源文件后

  • 如果我创建一个提供者并将其绑定到一个类,就像这样 然后

  • 我将为我的网站创建自定义用户提供程序,对于用户来说,没有“用户名”和“密码”这样的概念(实际上有类似于密码的东西,但它的名称不同)。在文档中,用户实体必须实现来自安全包的UserInterface,该安全包具有诸如getUsername、getPassword之类的方法。我能用我自己的领域吗?或者我应该使用名称冲突(例如,getUsername将返回我的唯一字段)来实现我的行为吗?

  • 我正在尝试创建一个自定义的KeyClope提供程序,它将为登录逻辑添加一些内容。我已经读过如何为KeyClope创建提供者(或插件),我正在与之合作的项目中已经有一个提供者(或插件),但我对它们知之甚少。 我需要为用户身份验证/授权添加自定义逻辑:我希望能够检查内部数据库中的一些字段来验证人员帐户。但是我没有找到任何关于类似情况的指南或好文章。有人能给我提供一些关于从什么开始的链接吗?为了实现这样

  • 问题内容: Sun的PKCS11 JCE安全提供程序缺少我们需要的某些功能。 因此,我使用原始资源编写了它的增强版本。 不幸的是,JCE基础结构拒绝新的提供者 “ JCE无法验证提供者”, 因为它没有正确签名。 抛出。 (呼叫) 关于如何签署新提供商以使其与JCE一起工作的任何建议? 问题答案: 该过程在文档“如何实现 提供者”中进行了描述。 它涉及到电子邮件 太阳向Oracle提供一些信息(包括

  • 我们可以通过实现ResultSetHandler接口或扩展ResultSetHandler的任何现有实现来创建我们自己的自定义处理程序。 在下面给出的示例中,我们通过扩展BeanHandler类创建了一个自定义处理程序EmployeeHandler。 要理解与DBUtils相关的上述概念,让我们编写一个运行读取查询的示例。 要编写我们的示例,让我们创建一个示例应用程序。 步 描述 1 更新在 DB