【发布时间】:2020-08-25 08:40:48
【问题描述】:
我用 *.js 和 *.css 资源建立了一个基本的 Spring Boot 项目。我激活了 Thymeleaf、Spring Web 和 Spring Security。现在:
- 对于 url:http://localhost:8080/css 和 js 文件正在被正确读取
- 对于网址:http://localhost:8080/secondcss 和 js 文件仍在正常读取中
- 对于 url:http://localhost:8080/second/third 应用程序不加载 css 和 js 文件
我猜这与 Spring 安全性有关。谁能指出我正确的方向,如何正确设置 SecurityConfig.java 类,以便应用程序在 url 中附加“/”之后读取文件?我知道those 解决方案,它们不起作用:( 将“/second/**”作为 antMatchers 还不够吗?
我的课:
HomeController.java
@Controller
public class HomeController {
@GetMapping("/")
public String goHome() {
return "index";
}
@GetMapping("/second")
public String goSecond() {
return "secondpage";
}
@GetMapping("/second/third")
public String goThird() {
return "thirdpage";
}
}
SecurityConfig.java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth)
throws Exception {
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
auth.inMemoryAuthentication()
.withUser("demo")
.password(encoder.encode("demo"))
.roles("USER", "ADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/second", "second/**").access("hasRole('USER')")
.and()
.formLogin();
}
}
这是项目结构:
【问题讨论】:
标签: css spring spring-boot spring-security