栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Boot Security - Remember Me示例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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. 登录表格

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

${SPRING_SECURITY_LAST_EXCEPTION.message}
User name:
Password:
Remember Me:
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记住我身份验证

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/720641.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号