【问题标题】:Spring Boot Security not ignoring certian url via WebSecuritySpring Boot Security 不会通过 Web Security 忽略某些 url
【发布时间】:2016-09-10 06:23:51
【问题描述】:

我正在使用带有 Spring Security 的 Spring boot 1.3.2。 我有以下 configure(HttpSecurity http) 方法来强制身份验证

    protected void configure(HttpSecurity http) throws Exception {

    RequestMatcher csrfRequestMatcher = new RequestMatcher() {
          private AntPathRequestMatcher[] requestMatchers = {
              new AntPathRequestMatcher("/iams/w/*")
          };
          @Override
          public boolean matches(HttpServletRequest request) {
            for (AntPathRequestMatcher rm : requestMatchers) {
              if (rm.matches(request)) { return true; }
            }
            return false;
          } // method matches

        };      


    http
        .csrf()
        .requireCsrfProtectionMatcher(csrfRequestMatcher)
        .and()
        .authorizeRequests()
            .anyRequest().authenticated()
            .and()
        .requestCache()
            .requestCache(new NullRequestCache())
            .and()
        .httpBasic();
}

我有以下 configure(WebSecurity web) 方法来忽略下面的一些 url;

    public void configure(WebSecurity web) throws Exception {

    web.ignoring().antMatchers(
            "/myapp/docs/**",
            "/myapp/docs/*",
            "/myapp/docs/index.html",
            "/resources/**", 
            "/static/**");

}

但是对 http://127.0.0.1:9000/myapp/docs/index.html 的 http 请求仍然需要用户名/密码(身份验证)并返回 "status":401,"error":"Unauthorized"... 实际上,WebSecurity 上的任何忽略 url 都不起作用,因为它还需要身份验证。如果我提供身份验证,那么它可以工作。我怎样才能在这里简单地忽略一些网址(例如 "/myapp/docs/**" )。我在 SecurityConfig 类中有以下定义

@EnableWebSecurity

@EnableGlobalMethodSecurity(prePostEnabled = true) 公共类 SecurityConfig 扩展 WebSecurityConfigurerAdapter {

我错过了什么?请指教。

【问题讨论】:

    标签: spring-security spring-boot restful-authentication


    【解决方案1】:

    使用尽可能简单的一组模式来保持不安全可能会更容易,然后简单地说其他一切都是安全的。

    这可能更接近你想要的:

    public static final String[] NOT_SECURED = {"/iams/docs/**","/static/**"};
    
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers(NOT_SECURED);
    }
    
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
          .authorizeRequests()
          .antMatchers(NOT_SECURED).permitAll()
          .anyRequest().authenticated()
          .and()
          .httpBasic()
          .and()
          .requestCache()
          .requestCache(new NullRequestCache())
          .and()
          .csrf().disable();
    }
    

    【讨论】:

      【解决方案2】:

      您的代码中有错误顺序。

      http
          .csrf()
          .requireCsrfProtectionMatcher(csrfRequestMatcher)
          .and()
          .authorizeRequests()
              .anyRequest().authenticated()
              .and()
          .requestCache()
              .requestCache(new NullRequestCache())
              .and()
          .httpBasic();
      

      因此,任何请求都需要经过身份验证。可以直接使用antMatchers

      http
              .authorizeRequests()
              .antMatchers("/iams/w/*")
              .authenticated()
              .and()
              .httpBasic()
              .and()
              .requestCache()
              .requestCache(new NullRequestCache())
              .csrf().disable()
      

      希望对你有帮助。

      【讨论】:

      • 感谢您的回复,但根据您的建议,我的“/iams/w/*”根本不受保护。我可以访问所有这些网址; "/iams/docs/**" 、 "/iams/w/*" 和 "/iams/api/*" 没有基本身份验证。以下是根据您的建议进行的设置。在这里,我想用用户名/密码保护“/iams/w”和“/iams/api/”,但让每个人都可以在没有用户名/密码的情况下访问“/iams/docs/*”。这是基于 Spring Boot RESTful 的实现,但希望公开一些像 docs 这样的 url,以便所有人都可以访问它,而不是 api 调用。
      【解决方案3】:

      感谢您的回复,但根据您的建议,我的“/iams/w/*”根本不受保护。我可以访问所有这些网址;没有基本身份验证的“/iams/docs/**”、“/iams/w/”和“/iams/api/”。以下是根据您的建议进行的设置。在这里,我想用用户名/密码保护“/iams/w”和“/iams/api/”,但让每个人都可以在没有用户名/密码的情况下访问“/iams/docs/*”。这是基于 Spring Boot Restful 的实现,但希望公开一些像 docs 这样的 url,以便所有人而不是 api 调用都可以访问它。

          public void configure(WebSecurity web) throws Exception {
      
          web.ignoring().antMatchers(
                  "/iams/docs/**",
                  "/iams/docs/*",
                  "/iams/docs/index.html",
                  "/static/**");
      
      }
      
      
      @Override
      protected void configure(HttpSecurity http) throws Exception {
      
          http
          .authorizeRequests()
          .antMatchers("/iams/api/**","/iams/api/v1/*")
          .authenticated()
          .and()
          .httpBasic()
          .and()
          .requestCache()
          .requestCache(new NullRequestCache())
          .and()
          .csrf().disable();
      }
      

      【讨论】:

        猜你喜欢
        • 2020-11-04
        • 2018-06-21
        • 2017-04-28
        • 2020-07-15
        • 2013-08-23
        • 1970-01-01
        • 2016-09-14
        • 1970-01-01
        • 2015-12-18
        相关资源
        最近更新 更多