前言
本文将介绍,在spring security中最基本的form表单登录,以及一些相关的配置。
自定义表单登录
如果我们不进行配置的话,spring security默认的登录页和登录接口都是/login,只不过一个是get请求一个是post请求而已。
get http://localhost:8080/login.html 访问页面
post http://localhost:8080/login.html 提交form数据
我们也可以把它们单独定义出来如下图中:
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
PasswordEncoder passwordEncoder(){
return NoOpPasswordEncoder.getInstance();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("xiaoming")
.password("123456").roles("admin");
}
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/js/**","/css/**","/images/**"); //这个是用来忽略一些url地址,对其不进行校验,通常用在一些静态文件中。
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login.html")//登录页面地址(如果不指定loginProcessingUrl,它就代表登录页面地址和登录接口地址都是/login.html)
.loginProcessingUrl("/loginTest"