【问题标题】:Spring Boot serving static content blocked by securitySpring Boot 提供被安全阻止的静态内容
【发布时间】:2019-05-05 23:58:14
【问题描述】:

我启动了 Spring Boot + Angular 应用程序,现在我想将整个东西部署为一个 jar。所以我创建了 maven 配置,其中 Angular 应用程序被构建,然后被复制到 /target/classes/resources

但是对 root (localhost:8080) 的每个请求都会被安全性阻止。当我禁用它时,我可以看到该页面,这意味着整个事情都已正确部署,但不知何故 spring 不允许我看到它。这是我的简单安全配置,我希望静态资源不受保护,而任何其他请求都需要身份验证:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
                .anyRequest().authenticated()
                .and().httpBasic();
    }
}

编辑: 我的问题的一个最小示例在这里: https://gitlab.com/jnowacki/security-issue-demo

编辑 2: 我尝试了这篇文章中的所有内容: Serving static web resources in Spring Boot & Spring Security application 我在概念上做错了吗?将静态内容与 Spring Boot 应用程序一起提供是错误的吗?

【问题讨论】:

  • @dur 我用最小的例子添加了一个回购链接。我需要在没有任何身份验证的情况下提供索引,以及身份验证后的任何其他请求。我知道我可以允许所有人,然后在例如之后确保某些路径/api 但这不是重点。
  • 你的问题不清楚。如果您想允许所有 URL /,那么您必须将该 URL 添加到您的配置中。
  • @dur 当我按如下方式配置它时:http.authorizeRequests() .antMatchers("/").permitAll() .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll() .anyRequest().authenticated() .and().httpBasic(); index.html 是允许的,但对 css 的请求要求提供凭据。

标签: java spring angular spring-boot spring-security


【解决方案1】:

添加这个额外的覆盖:

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

AUTH_WHITELIST 将包含要忽略的路径。例如:

private static final String[] SWAGGER_AUTH_WHITELIST = {
        // -- swagger ui
        "/v2/api-docs",
        "/swagger-resources",
        "/swagger-resources/**",
        "/swagger-ui.html",
        "/resources/**"
};

【讨论】:

  • 你能说得更具体点吗? 2.1 的发行说明中没有提到类似的事情:github.com/spring-projects/spring-boot/wiki/…
  • 我添加了最小示例的链接,我不认为它特定于 2.1,它只是我有这个版本,它在那里不起作用。
【解决方案2】:

试试下面。

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

参考spring-securitys-antmatcher

【讨论】:

    【解决方案3】:

    使用该方法可以让spring security忽略静态资源

    //this method allows static resources to be neglected by spring security
        @Override
        public void configure(WebSecurity web) throws Exception {
            web
                .ignoring()
                .antMatchers("/resources/**", "/static/**");
        }
    

    【讨论】:

      【解决方案4】:

      根据StaticResourceLocationdocs:

      .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()
      

      将允许访问 CSS、JS、ICO、图像,不允许访问 html。

      要允许index.html,您可以使用以下配置:

       http.authorizeRequests()
                      .antMatchers("/index.html", "/").permitAll()
                      .anyRequest().authenticated()
                      .and().httpBasic();
      

      【讨论】:

        【解决方案5】:

        // 扩展 WebSecrityConfiguratesAdapeter

        @Configuration
        public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
        
            @Overide
            public void configure(WebSecurity web) throws Exception { 
                 web.ignoring()
                    .antMatchers(HttpMethod.OPTIONS, ALL_ESCAPE)
                    .antMatchers("*.js")
                    .antMatchers(")
                    .antMatchers(BOWER_COMPONENTS)
                    .antMatchers(I18N)
                    .antMatchers(CONTENT);           
              }
        }
        

        【讨论】:

          【解决方案6】:

          你必须匹配真实路径,或者例子:

          .pathMatchers("/assets/**", "/static/**")
                      .permitAll()ere
          

          【讨论】:

            猜你喜欢
            • 2016-07-14
            • 2019-03-30
            • 1970-01-01
            • 2015-06-21
            • 2019-09-20
            • 1970-01-01
            • 2015-05-24
            • 2020-08-07
            相关资源
            最近更新 更多