Spring Boot Security - Remember Me示例

Spring Boot Security - Remember Me示例,第1张

Spring Boot Security - Remember Me示例

Spring Boot Security- Remember Me Example - Websparrowhttps://www.websparrow.org/spring/spring-boot-security-remember-me-example

本页将指导您如何在应用程序中配置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.RELEASESpring Security 5.1.4.RELEASEJDK 8Maven 3STS 3Embedded Tomcat Server
必需的依赖项

要解决 JAR 依赖关系,请将以下代码添加到 pom.xml


	
		org.springframework.boot
		spring-boot-starter-security
	
	
		org.springframework.boot
		spring-boot-starter-web
	
	
	
		org.apache.tomcat.embed
		tomcat-embed-jasper
		provided
	
Copy 项目结构

我们在 STS IDE 中的应用程序的最终项目结构将如下所示。

 

1. 登录表格

创建一个简单的自定义登录表单并添加一个复选框以启用"记住我"功能。

 
 
 
  Copy
  
 
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");
	}	
}
Copy

在上面的 Java 配置类中:

rememberMe() 方法返回该类以进行进一步的自定义。RememberMeConfigurer

key(字符串)方法设置密钥以标识为记住我身份验证而创建的令牌。

rememberMecookieName(String rememberMecookieName)方法设置cookie的名称,该名称存储用于记住我身份验证的令牌。默认为"记住我"。

tokenValiditySeconds(int tokenValiditySeconds)方法允许指定令牌的有效时间(以秒为单位)。

3. 测试应用程序

要测试应用程序的"记住我"功能,请按照以下步骤 *** 作:

1. 将应用程序作为 Spring Boot 运行。

2.使用您的用户名,密码登录,不要忘记选中"记住我"复选框。

3. 成功登录后,关闭浏览器并重新打开。尝试访问您的安全页面,这次它不会要求重新输入用户名和密码。

4.这是因为它将用户的登录信息存储到Web浏览器cookie中。

下载源代码:spring-boot-security-remember-me-example.zip

引用
    类 HttpSecurity记住我身份验证

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5716954.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存