这两天被这一个小小的知识点卡的死死的。主要是使用Spring Security框架自定义登录界面问题。配置代码如下:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers( "/admin/css/**","/admin/js/**","/admin/img/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/admin/login")
.successForwardUrl("/admin/index")
.failureForwardUrl("/admin/login-error")
.permitAll()
.and()
.logout()
.permitAll()
.and()
.csrf().disable();
}
值得一提的就是formLogin()中使用。loginPage()方法配置自定义登录界面的路径。之前自学的时候是直接使用的Spring Security的相关示例代码,并没有去深究其中的东西。直到今天碰到这个问题。这个问题就是.loginPage(...)中传入的地址并不是html静态页面的相对地址,而是对整个web程序的Controller层发送的请求地址,也就是说你的Controller层中需要一个方法来解析.loginPage()中的请求信息。
之后会有我的Springboot+Spring Security的整合教程:
这是Springboot中Spring Security多配置的教程