登录管理
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/login").permitAll()// 这些资源放行 只放行登录页面
.anyRequest().authenticated()// 其它资源要身份认证
.and().formLogin().loginPage("/login")// 登录页面
.defaultSuccessUrl("/index")// 登录成功后跳转到/index 主页
.failureUrl("/login?error")// 登录失败再跳转到登录页面
.permitAll()// 登录页面放行
.and().logout().permitAll() //退出请求放行
.and().rememberMe().tokenRepository(tokenRepository())//设置token 记住密码 记住登录凭证
.and().sessionManagement().maximumSessions(1).expiredUrl("/login?timeout");// 退出请求放行
}
登录验证
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.jdbcAuthentication()//读取数据库用户,权限,角色然后放到内存里验证
.dataSource(dataSource);
}
加密方法
@Bean
//加密方法
public Argon2PasswordEncoder passwordEncoder() {
Argon2PasswordEncoder encoder = new Argon2PasswordEncoder();
return encoder;
};
设置登录凭证
@Bean
public PersistentTokenRepository tokenRepository() {
JdbcTokenRepositoryImpl jdbcTokenRepositoryImpl = new JdbcTokenRepositoryImpl();
jdbcTokenRepositoryImpl.setDataSource(dataSource);
return jdbcTokenRepositoryImpl;
}