本文最后更新于:2022年5月20日 下午
摘要:SpringSecurity基于数据库实现自动登录功能的开发步骤。
基于数据库实现Remember-me
开发步骤
- 创建数据库表
1 2 3 4 5 6 7 8
| CREATE TABLE `persistent_logins` ( `username` varchar(64) NOT NULL, `series` varchar(64) NOT NULL, `token` varchar(64) NOT NULL, `last_used` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`series`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
- 修改配置类
1 2 3 4 5 6 7 8 9 10 11
| @Autowired private DataSource dataSource;
@Bean public PersistentTokenRepository persistentTokenRepository() { JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl(); jdbcTokenRepository.setDataSource(dataSource); return jdbcTokenRepository; }
|
- 在configure(HttpSecurity http)方法中设置自动登录
1 2 3
| .and().rememberMe().tokenRepository(persistentTokenRepository()) .tokenValiditySeconds(60) .userDetailsService(userDetailsService)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| package com.shg.securitydemo.config;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl; import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import javax.sql.DataSource;
@Configuration public class SecurityConfig3 extends WebSecurityConfigurerAdapter {
@Qualifier("userDetailsService2") @Autowired private UserDetailsService userDetailsService;
@Autowired private DataSource dataSource;
@Bean public PersistentTokenRepository persistentTokenRepository() { JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl(); jdbcTokenRepository.setDataSource(dataSource); return jdbcTokenRepository; }
@Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); }
@Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService) .passwordEncoder(passwordEncoder()); }
@Override protected void configure(HttpSecurity http) throws Exception { http.logout().logoutUrl("/logout").logoutSuccessUrl("/test/logout").permitAll(); http.exceptionHandling().accessDeniedPage("/403.html"); http.formLogin() .loginPage("/login.html") .loginProcessingUrl("/user/login") .defaultSuccessUrl("/success.html").permitAll() .and().authorizeRequests() .antMatchers("/", "/user/login").permitAll() .antMatchers("/test/hello").hasAuthority("admin") .antMatchers("/test/share").hasAnyAuthority("admin", "root") .antMatchers("/test/role").hasRole("admin") .anyRequest().authenticated() .and().rememberMe().tokenRepository(persistentTokenRepository()) .tokenValiditySeconds(60) .userDetailsService(userDetailsService) .and().csrf().disable(); } }
|
- 修改登录页面login.html
- 添加一个复选框,复选框的name必须为remember-me
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>login</title> </head> <body> <form action="/user/login" method="post"> 用户名:<input type="text" name="username"> <br /> 密码:<input type="text" name="password"> <br /> <input type="checkbox" name="remember-me" />记住我 <input type="submit" value="login"> </form> </body> </html>
|