【问题标题】:Spring Boot doesn't serve HTML after Spring Security is enabled启用 Spring Security 后 Spring Boot 不提供 HTML
【发布时间】:2017-05-06 00:30:12
【问题描述】:

在我的 Spring Boot 应用程序中,我将 Spring Security 配置为

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  public void configure(HttpSecurity http) throws Exception {
    http.csrf().disable().authorizeRequests()
        .antMatchers("/css/**", "/js/**", "/images/**", "**/index.html").permitAll()
        .antMatchers("/**.html").permitAll()
        .antMatchers(HttpMethod.POST, "/login").permitAll()
        .anyRequest().authenticated()
        .and()
        .addFilterBefore(new LoginFilter("/login"), BasicAuthenticationFilter.class)
        .addFilterBefore(new TokenFilter(), BasicAuthenticationFilter.class);
  }
}

我在resources/static/index.html 下有index.html。在我启用安全性后,它停止了服务。我错过了什么?

如果我在没有任何安全性的情况下返回,我会在服务器上呈现我的 HTML。

【问题讨论】:

  • .antMatchers("/*.html").permitAll() 对你有用吗?我的印象是 ** 用于目录, * 用于字符:​​docs.spring.io/spring-framework/docs/current/javadoc-api/org/…
  • 这也不起作用
  • 您是否使用@Configuration 注释了您的WebSecurityConfig 类?并显示您的LoginFilterTokenFilter
  • 你得到什么回应? 401、403 或 ...

标签: java spring spring-boot spring-security


【解决方案1】:

尝试更改为/**/index.html。对于 Ant 路径匹配器

? matches one character
* matches zero or more characters
** matches zero or more directories in a path
{spring:[a-z]+} matches the regexp [a-z]+ as a path variable named "spring"

【讨论】:

  • 不,我猜这个顺序有问题
  • 您可以在org.springframework.security.web.FilterChainProxy#doFilterInternal 中调试以查看所有过滤器链。或者您可以启用 spring 安全调试器来观察记录器。
猜你喜欢
  • 2016-05-04
  • 2016-06-14
  • 1970-01-01
  • 2016-12-29
  • 2017-09-11
  • 2017-03-10
  • 2014-09-15
  • 2020-05-29
  • 2017-07-12
相关资源
最近更新 更多