本页将指导您如何在应用程序中配置Spring Boot Security - Remember Me功能。记住我功能可帮助用户访问应用程序而无需重新登录。大多数情况下,我们使用登录页面上的复选框来启用它。
Spring Security- Remember Me功能将用户的登录信息存储到Web浏览器cookie中,该cookie能够在多个会话中识别用户。
注意:此示例基于简单基于哈希的令牌方法,该方法使用哈希技术创建唯一令牌。在此技术中,使用密钥、到期日期、密码和用户名创建令牌。饼干的组成如下:
base64(username + ":" + expirationTime + ":" + md5Hex(username + ":" + expirationTime + ":" password + ":" + key)) username: As identifiable to the UserDetailsService password: That matches the one in the retrieved UserDetails expirationTime: The date and time when the remember-me token expires, expressed in milliseconds key: A private key to prevent modification of the remember-me token
Copy我们还有一种方法,即持久令牌方法。在这种方法中,我们将令牌存储到数据库中。将创建表"persistent_logins"来存储登录令牌和系列。
使用的技术
查找此应用程序中使用的所有技术的列表。
- Spring Boot 2.1.2.RELEASE
- Spring Security 5.1.4.RELEASE
- JDK 8
- Maven 3
- STS 3
- Embedded Tomcat Server
必需的依赖项
要解决 JAR 依赖关系,请将以下代码添加到 pom.xml。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- JSP compiler jar -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
项目结构
我们在 STS IDE 中的应用程序的最终项目结构将如下所示。
1. 登录表格
创建一个简单的自定义登录表单并添加一个复选框以启用"记住我"功能。
<form action="login" method="post">
<table>
<tr style="color: red;">
<td></td>
<td>${SPRING_SECURITY_LAST_EXCEPTION.message}</td>
</tr>
<tr>
<td>User name:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>Remember Me:</td>
<td><input type="checkbox" name="remember-me" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Login"></td>
</tr>
</table>
</form>
2. 安全配置
Spring安全配置文件用于实现记住我的功能,如下所示:
package org.websparrow.config;
import org.springframework.beans.factory.annotation.Autowired;
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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests().antMatchers("/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login").permitAll();
//remember me configuration
http
.rememberMe()
.key("myUniqueKey")
.rememberMeCookieName("websparrow-login-remember-me")
.tokenValiditySeconds(10000000);
http
.logout()
.logoutUrl("/logout");
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("websparrow").password("{noop}web123").roles("USER");
}
}
在上面的 Java 配置类中:
rememberMe() 方法返回该类以进行进一步的自定义。RememberMeConfigurer
key(字符串键)方法设置密钥以标识为记住我身份验证而创建的令牌。
rememberMeCookieName(String rememberMeCookieName)方法设置cookie的名称,该名称存储用于记住我身份验证的令牌。默认为"记住我"。
tokenValiditySeconds(int tokenValiditySeconds)方法允许指定令牌的有效时间(以秒为单位)。
3. 测试应用程序
要测试应用程序的"记住我"功能,请按照以下步骤操作:
1. 将应用程序作为 Spring Boot 运行。
2.使用您的用户名,密码登录,不要忘记选中"记住我"复选框。
3. 成功登录后,关闭浏览器并重新打开。尝试访问您的安全页面,这次它不会要求重新输入用户名和密码。
4.这是因为它将用户的登录信息存储到Web浏览器cookie中。