Spring Boot服务静态内容被安全性阻止

时间:2018-12-04 14:19:25

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

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

但是对根(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应用程序一起提供静态内容是否错误?

6 个答案:

答案 0 :(得分:2)

添加此其他替代项:

@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/**"
};

答案 1 :(得分:1)

尝试以下。

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

引用spring-securitys-antmatcher

答案 2 :(得分:0)

使用此方法可以使Spring安全性忽略静态资源

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

答案 3 :(得分:0)

根据StaticResourceLocation docs

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

将允许访问CSS,JS,ICO,图像,并且不能访问html

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

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

答案 4 :(得分:0)

//扩展了WebSecrityConfiguratesAdapeter

@配置 公共类SecurityConfiguration扩展了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);           
  }}

答案 5 :(得分:0)

您必须匹配真实路径,或示例:

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