Spring Security整合整合Oauth2实现流程详解实现流程详解
主要介绍了Spring Security整合Oauth2实现流程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友
可以参考下
一、创建项目并导入依赖一、创建项目并导入依赖
<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.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
注:这里的oauth2不是springboot那个,这是springsecurity。
Oauth2一共有四种认证模式是
本篇是password的认证模式,用于前后端分离登陆
第三方登陆一般是授权码模式
二、相关配置和代码二、相关配置和代码
注:授权服务器和资源服务器一般是分开来的,我这里就不分开了
2.1)application.properties
spring.redis.host=192.168.21.135
spring.redis.port=6379
spring.redis.database=0
spring.redis.password=520hufei520
2.2)创建授权服务
2.2.1)实现AuthorizationServiceConfigurerAdapter
@Configuration表示这个一个配置类
@EnableAuthorizationServer表示开启授权服务
2.2.2)注入AuthenticationManager、RedisConnectionFactory、UserDetailsService
AuthenticationManager表示支持password认证模式
RedisConnectionFactory登陆成功后的token需要存在redis里面,因为redis里面有过期机制
UserDetailsService里面存放着用户信息
2.2.3)重写方法
评论0