首页 文章

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

提问于
浏览
2

我启动了Spring Boot Angular应用程序,现在我想将整个东西部署为jar . 所以我创建了maven配置,在其中构建了角度应用程序,然后将其复制到/ 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应用程序一起提供静态内容是不对的?

4 回答

  • 0

    试试下面 .

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

    参考spring-securitys-antmatcher

  • 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

    根据 StaticResourceLocation docs

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

    将允许访问CSS,JS,ICO,图像和 NOT to html .

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

    http.authorizeRequests()
                    .antMatchers("/index.html", "/").permitAll()
                    .anyRequest().authenticated()
                    .and().httpBasic();
    
  • 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/**");
        }
    

相关问题