【发布时间】:2016-09-08 23:17:05
【问题描述】:
我正在尝试使用 Spring Boot 1.4 和 Thymeleaf 2。我想保护我的应用程序。这是我的安全配置:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/resources/static/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
}
这是我的 MVC 控制器:
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/index").setViewName("index");
registry.addViewController("/").setViewName("index");
registry.addViewController("/login").setViewName("login");
}
}
但是,当我尝试访问 localhost:8080 时,它显示没有 css、js 的索引页面!当我通过localhost:8080/login.html 手动登录时,一切正常。我遵守 Spring 的约定 - Thymeleaf 集成和静态资源在/resources/static 下,模板在/resource/templates 下。
对我来说缺少什么?
编辑:
它以这种方式工作:
http
.authorizeRequests()
.antMatchers("/css/**").permitAll()
.antMatchers("/js/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
但是,我应该用这种方式添加静态资源下的所有文件夹吗?我好像错过了什么。
【问题讨论】:
-
这很奇怪,但是当您将css和js文件放入
src/main/resources/static/resources时,无需进一步配置即可工作
标签: java spring spring-mvc spring-security thymeleaf