Spring安全性-重定向到自定义登录页面,而无需通过控制器
我目前正在使用SpringBoot+SpringSecurity开发一个简单的网站,需要用户登录才能访问它。我已经在资源/templates1/bruceLogin1.html下创建了自己的自定义静态登录页面。任何未经身份验证的对我的网站的访问将首先重定向到登录页面URL,该URL为 注意:我没有为此登录URL创建任何控制器,因此我希望直接绕过控制器访问bruceLogin1.html。因为我直接允许访问这个html页面而不需要通过控制器,所以我假设不需要视图解析器(例如ThymryLeaf) 我打开我的浏览器,输入,然后浏览器将我重定向到,但遗憾的是,我出错了Spring安全性-重定向到自定义登录页面,而无需通过控制器,spring,security,authentication,Spring,Security,Authentication,我目前正在使用SpringBoot+SpringSecurity开发一个简单的网站,需要用户登录才能访问它。我已经在资源/templates1/bruceLogin1.html下创建了自己的自定义静态登录页面。任何未经身份验证的对我的网站的访问将首先重定向到登录页面URL,该URL为 注意:我没有为此登录URL创建任何控制器,因此我希望直接绕过控制器访问bruceLogin1.html。因为我直接允许访问这个html页面而不需要通过控制器,所以我假设不需要视图解析器(例如ThymryLeaf)
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Jan 11 12:40:37 SGT 2020
There was an unexpected error (type=Not Found, status=404).
No message available
下面是我的配置
@SpringBootApplication
@EnableWebSecurity
public class SpringSecurityCatalogApplication implements WebMvcConfigurer {
public static void main(String[] args) {
SpringApplication.run(SpringSecurityCatalogApplication.class, args);
}
@EnableWebSecurity
@Order(Ordered.HIGHEST_PRECEDENCE)
class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/resources/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/templates1/bruceLogin1.html")
.permitAll();
}
}
}
考虑一下,我不会将自定义登录页放在resources/template1文件夹下,而是将其放在resources/static或resources/public文件夹中。 然后按如下所示进行配置
@EnableWebSecurity
@Order(Ordered.HIGHEST_PRECEDENCE)
class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/resources/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/bruceLogin1.html")
.permitAll();
}
}
打开浏览器并访问任何URL,您将被重定向到。就这样请注意,这不需要任何控制器或视图解析器