java - Spring Boot安全性在https之后阻止静态资源

标签 java spring spring-boot https

我在论坛上搜索了一下,但没有找到合适的东西。我最近在我的服务器(VPS)上发布了我的 spring boot multi maven 项目。一切都很好,但在我使用 Let's Encrypt 通过 HTTPS 保护站点后,站点的静态内容不再被提供,而是被阻止 (403)。 这是我的应用程序的结构:

app
--api
  --src/main/resources/static
--business
--model
--repository

静态资源位于 api maven 模块的 src/main/resources/static 文件夹中。

我可以使用(例如)访问我的网站主页:https://example-app.com/index.html

js等资源与index.html同级。

在我的 Spring Boot Security 安全配置中,我有:

protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable().exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().authorizeRequests()
            .antMatchers("/api/public/**").permitAll()
            .antMatchers("/api/**").authenticated()
            .anyRequest().permitAll();
    http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
}

正如我所说,在 https 之前我完全能够运行我的应用程序,但现在当我访问我的 index.html 时,我在浏览器控制台中有许多 403(js、图像、css、字体)。正如您所看到的,我的安全配置非常宽松,所以我认为这不是问题所在。 我认为这是一个 Spring Boot 配置错误的选项。

更新

经过一些测试后,我看到从应用程序内部调用的其余 api 总是以 403 结尾,但如果我尝试在应用程序上下文之外调用它们(来自 url、 postman ...),它们都会起作用。

最佳答案

这有点猜测,但让我们看看。 httpSecurity.antMatcher(/api/public/**).permitAll() 应该仅适用于已通过身份验证的用户。由于您使用 SessionCreationPolicy.STATELESS,当您在应用内调用资源时,AuthenticationContext 可能不可用。

如果您不介意将资源暴露给未经身份验证的用户,您可以尝试这样做:

public class SecurityConfig extends WebSecurityConfigurerAdapter {

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

关于java - Spring Boot安全性在https之后阻止静态资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55852322/

相关文章:

java - 如何覆盖/替换 Maven 项目中的 GWT 类?

java - mvn install 在 eclipse 中不起作用,但在命令行中工作正常

java - 如何检查、抛出和捕获 bean 的验证 ConstraintViolationException

spring - Kotlin - SpringApplicationBuilder 中的扩展运算符

angular - 访问被 CORS 策略 : Response to preflight request doesn't pass access control check 阻止

docker - Springboot 客户端无法使用 Docker 容器 id 向 Eureka 注册

java - 如何正确复制对象

java - 包含两个变量的列表

java - 来自另一个属性的属性占位符位置(Spring 3.1)

java - Spring Boot验证: get max size from property file