Spring Boot 2 安全限制索引页

问题描述

我正在尝试通过手动添加所有依赖项等方式向自定义 Java 项目添加 spring 安全性。到目前为止我已经成功了,但是我(认为我)我的 WebSecurityConfigurerAdapter 有问题:

/r/Cricket/comments/kunmyt/match_thread_3rd_test_australia_v_india_day_5/
/r/Cricket/comments/ku008u/match_thread_3rd_test_australia_v_india_day_4/
/r/Cricket/comments/ktcg7n/match_thread_3rd_test_australia_v_india_day_3/
...

当如上所述限制 index.html 时,用户在输入应用程序基本 URL(例如 localhost:8080/myapp/)时需要立即登录。但是,如果我将 antMatcher 更改为:

@Override
protected void configure(HttpSecurity http) throws Exception{
      http.authorizeRequests()
        .antMatchers("/index.html").hasAnyRole("USER","ADMIN")
        .and()
        .sessionManagement()
        .sessionCreationPolicy(SessionCreationPolicy.NEVER)
        .and()
        .formLogin()
        .loginPage("/sign-in")
        .permitAll();
}

我无需登录即可访问应用程序基本 URL。值得一提的是 index.html 和 test.html 完全相同(它们只包含一个 h1-tag),并且都位于生成的 .war 文件的根目录中: enter image description here

如何配置应用程序,以便用户在输入 base-url 时不必登录,而仅在请求 index.html(例如 localhost:8080/myapp/index.html)时才需要登录

提前致谢

编辑:我的应用程序在 localhost:8080/myapp/ 有一个端点,如下所示:

    ...
         http.authorizeRequests()
        .antMatchers("/test**").hasAnyRole("USER","ADMIN")
        .and()
    ...

这个想法是用户应该能够在无需身份验证的情况下访问它。

解决方法

你可以试试这个...

'''
         http.authorizeRequests()
        .antMatchers("/test**").permitAll().antMatchers("/index.html").hasAnyRole("USER","ADMIN")
        .and()
'''
    

如果 test.html 是您的基础文件。