在学习spring boot security 时,遇到一个问题,在做权限控制时,想在访问接口时,都跳转到自定义的login.html页面。但是像这样写实现不了。究其原因,是因为把html放在了templates下面,而在springboot 中,templates中的文件是需要通过视图解析器才能去方问的。
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
.loginPage("/login.html")
.and()
.authorizeRequests()
.antMatchers("/login.html").permitAll()
.anyRequest()
.authenticated();
}
方法一:把login.html文 件放到static下。
方法二:新建WebMvcConfig类
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
}
}
把上面的登录页面改为
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
.loginPage("/login")
.and()
.authorizeRequests()
.antMatchers("/login").permitAll()
.anyRequest()
.authenticated();
}
这样就可以正常跳转了。