【问题标题】:How to disable spring security for certain resource paths如何禁用某些资源路径的弹簧安全性
【发布时间】:2017-12-02 13:58:31
【问题描述】:

我在 Spring Boot 应用程序中实现 Spring Security 以执行 JWT 验证,其中我有一个过滤器、一个 AuthenticationManager 和一个 AuthenticationProvider。我想要做的是我想禁用某些资源路径的安全性(使它们基本上不安全)。

我在 securityConfig 类(从 WebSecuirtyConfigurerAdapater 扩展)中尝试的内容如下:

protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.addFilterBefore(buildJwtTokenAuthenticationProcessingFilter(),
                UsernamePasswordAuthenticationFilter.class);
        httpSecurity.authorizeRequests().antMatchers("/**").permitAll();
        httpSecurity.csrf().disable();
        httpSecurity.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    } 

我现在要做的是让我的所有资源路径都不安全,

但上面的代码不起作用,并且我的 CustomAuthenticationProvider(从 AuthenticationProvider 扩展)中的 authenticate 方法每次都会执行

无论在每个请求上是否使用 permitAll,都将执行身份验证。我也尝试过 anyRequest 来代替 antMatchers:

httpSecurity.authorizeRequests().anyRequest().permitAll();

任何帮助将不胜感激。

【问题讨论】:

    标签: spring spring-boot spring-security


    【解决方案1】:

    在您的类中覆盖以下扩展 WebSecuirtyConfigurerAdapater 的方法:

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/unsecurePage");
    }
    

    【讨论】:

      【解决方案2】:

      尝试更新您的代码以允许对特定路径的请求,如下所示

      protected void configure(HttpSecurity httpSecurity) throws Exception {
      
      httpSecurity.addFilterBefore(buildJwtTokenAuthenticationProcessingFilter(),
                      UsernamePasswordAuthenticationFilter.class);
              httpSecurity.authorizeRequests().antMatchers("/").permitAll().and()
                  .authorizeRequests().antMatchers("/exemptedPaths/").permitAll();
      
      httpSecurity.csrf().disable(); 
       httpSecurity.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
      
      } 
      

      【讨论】:

        猜你喜欢
        • 2018-01-21
        • 2015-08-02
        • 2019-03-03
        • 2014-11-22
        • 2013-01-05
        • 1970-01-01
        • 1970-01-01
        • 2012-07-27
        • 2020-05-19
        相关资源
        最近更新 更多