在 spring-boot 中删除其中一个 Web API 上的 Auth Filter

Posted

技术标签:

【中文标题】在 spring-boot 中删除其中一个 Web API 上的 Auth Filter【英文标题】:Remove Auth Filter on one of the Web API's in spring-boot 【发布时间】:2017-01-21 21:54:24 【问题描述】:

我的项目有 2 个 API。第一个需要身份验证,第二个不需要。

我成功地为第一个 API /auth/uploadFile 添加了一个基于令牌的身份验证过滤器

这是 SecurityConfig 类中的代码 sn-p,它扩展 WebSecurityConfigurerAdapter

    @Override
    protected void configure(HttpSecurity http) throws Exception 

        http.addFilterBefore(tokenAuthenticationFilter, BasicAuthenticationFilter.class).authorizeRequests()
                .antMatchers("/auth/uploadFile/").permitAll().anyRequest()
                .authenticated().and().csrf().disable();
    

还没有将我的第二个 API /noauth/uploadFile 添加到 antMatchers() 但是当我制作时它仍然进入自定义 tokenAuthenticationFilter对它的 POST 调用。

当我调用我的第二个 API /noauth/uploadFile 时,我如何避免输入我的自定义过滤器 tokenAuthenticationFilter,即我的过滤器不应该应用于第二个 API?

【问题讨论】:

【参考方案1】:

您可以在 SecurityConfig 类中覆盖/添加以下方法。

public void configure(WebSecurity web) throws Exception 
    web
        .ignoring()
        .antMatchers("/noauth/**");

【讨论】:

以上是关于在 spring-boot 中删除其中一个 Web API 上的 Auth Filter的主要内容,如果未能解决你的问题,请参考以下文章