SpringBoot 之Spring Security自动登录功能

版本SpringBoot2.2.6.RELEASE

1、pom.xml依赖

 <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>

完整pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.linst</groupId>
    <artifactId>rememberme</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>rememberme</name>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <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>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、Spring Security配置
SecurityConfig 类:

package cn.linst.remeberme;

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.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;


@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Bean
    PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance();
    }
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("zhangsan")
                .password("123").roles("admin");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .and()
                .rememberMe()
//               .key("coffee")   //可以指定这个 key,服务端重启或者浏览器打开再关闭,也能访问到 /hello 接口。
                .and()
                .csrf().disable();
    }
}

加上 .rememberMe()即可。

3、创建一个控制器

package cn.linst.remeberme;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }
}

4、启动访问http://localhost:8001/hello
跳转到登录页
在这里插入图片描述

登录成功后,跳转到/hello。
/login请求参数
在这里插入图片描述

查看控制台,/hello请求发现带上RememberMe
在这里插入图片描述

关闭浏览器,再去访问不用登录了。直接能访问到/hello。

5、remember-me分析
1)remember-me的值是一个 Base64 转码后的字符串。
remember格式是MD5 ( username + “:” + tokenExpiryTime + “:” + password + “:” + key), key 是一个散列盐值,用来防治令牌被修改。
2)在源码TokenBasedRememberMeServices中可以看到:

public class TokenBasedRememberMeServices extends AbstractRememberMeServices {
	@Override
	public void onLoginSuccess(HttpServletRequest request, HttpServletResponse response,
			Authentication successfulAuthentication) {

		// 从登录成功的 Authentication 中提取出用户名/密码
		String username = retrieveUserName(successfulAuthentication);
		String password = retrievePassword(successfulAuthentication);

		// If unable to find a username and password, just abort as
		// TokenBasedRememberMeServices is
		// unable to construct a valid token in this case.
		if (!StringUtils.hasLength(username)) {
			logger.debug("Unable to retrieve username");
			return;
		}

		if (!StringUtils.hasLength(password)) {
			UserDetails user = getUserDetailsService().loadUserByUsername(username);
			password = user.getPassword();

			if (!StringUtils.hasLength(password)) {
				logger.debug("Unable to obtain password for user: " + username);
				return;
			}
		}

		//去获取令牌的有效期,令牌有效期默认就是两周
		int tokenLifetime = calculateLoginLifetime(request, successfulAuthentication);
		long expiryTime = System.currentTimeMillis();
		// SEC-949,令牌有效期默认两个星期
		expiryTime += 1000L * (tokenLifetime < 0 ? TWO_WEEKS_S : tokenLifetime);
		
		// 计算散列值
		String signatureValue = makeTokenSignature(expiryTime, username, password);
		
		// 用户名、令牌有效期、散列值放入 Cookie 
		setCookie(new String[] { username, Long.toString(expiryTime), signatureValue },
				tokenLifetime, request, response);

		if (logger.isDebugEnabled()) {
			logger.debug("Added remember-me cookie for user '" + username
					+ "', expiry: '" + new Date(expiryTime) + "'");
		}
	}
	
	protected String makeTokenSignature(long tokenExpiryTime, String username, String password) {
			// getKey(),默认值是一个 UUID 字符串。如果服务端重启,这个 key 会变,这样之前的所有 remember-me 自动登录令牌失效。
			String data = username + ":" + tokenExpiryTime + ":" + password + ":" + getKey();
			MessageDigest digest;
			try {
				digest = MessageDigest.getInstance("MD5");
			}
			catch (NoSuchAlgorithmException e) {
				throw new IllegalStateException("No MD5 algorithm available!");
			}
	
			return new String(Hex.encode(digest.digest(data.getBytes())));
		}
	}

6、优化
可以指定这个 key,服务端重启或者浏览器打开再关闭,也能访问到 /hello 接口。
修改SecurityConfig #configure(HttpSecurity http) ,如下:


@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
// 省略其他
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .and()
                .rememberMe()
                .key("coffee")	// 任意
                .and()
                .csrf().disable();
    }
 }
当我们使用SpringBoot + Spring Security + MyBatis进行Web应用程序的开发时,通常会涉及以下几个方面的功能: 1. 用户认证和授权:Spring Security是一个强大的安全框架,可以帮助我们实现用户认证和授权功能。它提供了多种认证方式,支持基于用户名和密码、LDAP、OAuth等方式进行认证。同时,它也提供了多种授权策略,可以根据用户角色、权限等信息进行授权。在Spring Security中,我们通常需要实现UserDetailsService接口来加载用户信息,同时也需要实现AccessDecisionManager接口来控制访问权限。 2. 数据库访问:MyBatis是一个流行的ORM框架,可以帮助我们实现数据库访问功能。它提供了多种方式来执行SQL语句,包括注解、XML文件、动态SQL等方式。同时,它也提供了多种映射方式,可以将数据库表映射为Java对象,从而实现对象关系映射。在Spring Boot中,我们通常需要使用MyBatis-Spring-Boot-Starter来启用MyBatis的自动配置功能。 3. RESTful接口开发:Spring Boot提供了一套强大的RESTful框架,可以帮助我们实现Web API的开发。它提供了多种注解来定义API的请求路径、请求方法、请求参数等信息,同时也提供了多种返回类型,包括JSON、XML、HTML等。在Spring Boot中,我们通常需要使用@RestController注解来定义RestController,同时也需要使用@RequestMapping注解来定义API的请求路径和请求方法。 4. 日志记录:Spring Boot提供了一套强大的日志框架,可以帮助我们记录应用程序的运行日志。它提供了多种日志级别,可以根据需要进行配置,同时也提供了多种输出方式,包括控制台输出、文件输出等。在Spring Boot中,我们通常需要使用logback或log4j2来实现日志记录功能。 总之,当我们使用Spring Boot + Spring Security + MyBatis进行Web应用程序的开发时,需要掌握以上几个方面的功能,并将它们有机地结合起来,从而实现高效、安全、可维护的应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值