Java 使用Spring security实现Thymeleaf中静态资源的可视性
我有如下目录层次结构:Java 使用Spring security实现Thymeleaf中静态资源的可视性,java,spring-boot,spring-security,thymeleaf,Java,Spring Boot,Spring Security,Thymeleaf,我有如下目录层次结构: -resources | |-static | |-css | |-js |-templates |-error |-fragments 这幅图: @Override protected void configure(HttpSecurity httpSecurity) throws Exception { httpSecurity .csrf() .disa
-resources
|
|-static
| |-css
| |-js
|-templates
|-error
|-fragments
这幅图:
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/login").not().fullyAuthenticated()
.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/main").hasRole("USER")
.antMatchers("/resources/**", "/registration").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/main")
.permitAll()
.and()
.logout()
.permitAll()
.logoutSuccessUrl("/login");
}
我需要一些帮助:1)我的静态资源在授权之前不可见
2) 授权后重定向到css或js文件
请帮帮我。我做错了什么?你应该在你的资源文件夹中创建一个公共文件夹,Spring将向所有人提供放置在
/css/**
、/js/**
、/images/**
文件夹中的文件,这些文件没有经过身份验证
如果您将img.jpg像那样放在images文件夹中,src/main/resources/public/images/img.jpg
可以在http://localhost:8080/images/img.jpg试试这个:
private static final String[] PUBLIC_MATCHERS = {
"/css/**",
"/js/**",
"/images/**",
};
//...
http
.authorizeRequests().
antMatchers(PUBLIC_MATCHERS).
permitAll().anyRequest().authenticated();
//...
不客气。:)几个月前,我还是SpringBoot和Thymeleaf的初学者,我很高兴我能帮助别人。