Spring Boot 提供被安全阻止的静态内容

问题描述 投票:0回答:7

我启动了 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/jnoacki/security-issue-demo

编辑2: 我尝试了这篇文章中的所有内容: 在 Spring Boot 和 Spring Security 应用程序中提供静态 Web 资源 我在概念层面上做错了什么吗?将静态内容与 Spring Boot 应用程序一起提供是错误的吗?

java spring angular spring-boot spring-security
7个回答
4
投票

添加此附加覆盖:

@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
投票

尝试下面。

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

参考spring-securitys-antmatcher


0
投票

使用此方法可以让静态资源被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/**");
    }

0
投票

根据

StaticResourceLocation
文档

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

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

要允许

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

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

0
投票

// 扩展 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);           
      }
}

0
投票

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

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

0
投票

试试这个:

.requestMatchers( “/v1/api/get-token”, “/swagger-ui.html”, “/swagger-ui/*”, “/v3/api-docs/”, “/swagger-资源/”, "/webjars/**").permitAll()

© www.soinside.com 2019 - 2024. All rights reserved.