gpt4 book ai didi

java - Spring Security 不允许加载 CSS 或 JS 资源

转载 作者:IT老高 更新时间:2023-10-28 13:45:58 25 4
gpt4 key购买 nike

资源在src/main/resources/static/css或src/main/resources/static/js下,我用的是spring boot,安全的class是:

@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();

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

最佳答案

您可能希望确保将包含这些元素的目录设置为 permitAll。

这是我的 Spring 安全上下文文件的摘录。在资源目录下,我有 js、css 和 images 文件夹,这些文件夹由这一行授予权限。

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

关于java - Spring Security 不允许加载 CSS 或 JS 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25368535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com