spring - Spring Boot+Thymeleaf 无法解析 login.html
问题描述
我基本上已经复制了将 Spring Web Security 与 Spring Boot 和 Thymeleaf 一起使用的教程。https://spring.io/guides/gs/securering-web/
对于配置:
@Configuration
public class WebConfig implements WebMvcConfigurer {
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
registry.addViewController("/").setViewName("home");
}}
为了安全public class WebSec extends WebSecurityConfigurerAdapter
:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.antMatchers("/users*").hasRole("ADMIN")
.antMatchers("/users/*").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").permitAll()
.and()
.logout().permitAll();
}
所有的html文件都在
/src/main/resources/templates
现在,home.html
很好地找到了。但是,每当需要登录页面时,login.html
在同一文件夹中都找不到,错误是:
Error resolving template "login", template might not exist or might not be accessible by any of the configured Template Resolvers
我不知道如何从这里开始。
解决方案
解决方案:不要将您的模板文件称为与您的路线相同的文件。这个问题可以通过命名文件login_template.html
什么的来解决。或者更好的是,换行:
registry.addViewController("/login").setViewName("login");
只是
registry.addViewController("/login");
我在用于ViewControllerRegistration.setViewName
.