该资源位于src/main/resources/static/css或src/main/resources/static/js下,我使用的是Spring Boot,安全级别为:

@Configuration
@EnableWebMvcSecurity
@EnableGlobalAuthentication
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
//      http.authorizeRequests().antMatchers("/", "/index", "/quizStart")
//              .permitAll().anyRequest().authenticated();
//      http.formLogin().loginPage("/login").permitAll().and().logout()
//              .permitAll();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth)
            throws Exception {
        auth.inMemoryAuthentication().withUser("test").password("test")
                .roles("USER");
    }
}

当我从浏览器访问“/index”时,它运行良好(可以加载资源),但是,如果取消注释类中的四行,则无法加载资源,这四行表示:
    http.authorizeRequests().antMatchers("/", "/index", "/quizStart")
            .permitAll().anyRequest().authenticated();
    http.formLogin().loginPage("/login").permitAll().and().logout()
            .permitAll();

有人可以帮忙吗?提前致谢。

最佳答案

您可能要确保将包含这些元素的目录设置为allowAll。

这是我的spring安全上下文文件的摘录。在resources目录下,我有js,css和images文件夹,这些文件夹由此行授予权限。

<security:intercept-url pattern="/resources/**" access="permitAll" />

09-11 03:49
查看更多