package com.pdl.config;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.AuthenticationKeyGenerator;
import org.springframework.security.oauth2.provider.token.DefaultAuthenticationKeyGenerator;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.JdkSerializationStrategy;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStoreSerializationStrategy;
import org.springframework.stereotype.Component;
@Component
public class MyRedisTokenStore implements TokenStore {
private static final String ACCESS = "access:";
private static final String AUTH_TO_ACCESS = "auth_to_access:";
private static final String AUTH = "auth:";
private static final String REFRESH_AUTH = "refresh_auth:";
private static final String ACCESS_TO_REFRESH = "access_to_refresh:";
private static final String REFRESH = "refresh:";
private static final String REFRESH_TO_ACCESS = "refresh_to_access:";
private static final String CLIENT_ID_TO_ACCESS = "client_id_to_access:";
private static final String UNAME_TO_ACCESS = "uname_to_access:";
private final RedisConnectionFactory connectionFactory;
private AuthenticationKeyGenerator authenticationKeyGenerator = new DefaultAuthenticationKeyGenerator();
private RedisTokenStoreSerializationStrategy serializationStrategy = new JdkSerializationStrategy();
private String prefix = "";
public MyRedisTokenStore(RedisConnectionFactory connectionFactory) {
this.connectionFactory = connectionFactory;
}
public void setAuthenticationKeyGenerator(AuthenticationKeyGenerator authenticationKeyGenerator) {
this.authenticationKeyGenerator = authenticationKeyGenerator;
}
public void setSerializationStrategy(RedisTokenStoreSerializationStrategy serializationStrategy) {
this.serializationStrategy = serializationStrategy;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
private RedisConnection getConnection() {
return this.connectionFactory.getConnection();
}
private byte[] serialize(Object object) {
return this.serializationStrategy.serialize(object);
}
private byte[] serializeKey(String object) {
return this.serialize(this.prefix + object);
}
private OAuth2AccessToken deserializeAccessToken(byte[] bytes) {
return (OAuth2AccessToken)this.serializationStrategy.deserialize(bytes, OAuth2AccessToken.class);
}
private OAuth2Authentication deserializeAuthentication(byte[] bytes) {
return (OAuth2Authentication)this.serializationStrategy.deserialize(bytes, OAuth2Authentication.class);
}
private OAuth2RefreshToken deserializeRefreshToken(byte[] bytes) {
return (OAuth2RefreshToken)this.serializationStrategy.deserialize(bytes, OAuth2RefreshToken.class);
}
private byte[] serialize(String string) {
return this.serializationStrategy.serialize(string);
}
private String deserializeString(byte[] bytes) {
return this.serializationStrategy.deserializeString(bytes);
}
@Override
public OAuth2AccessToken getAccessToken(OAuth2Authentication authentication) {
String key = this.authenticationKeyGenerator.extractKey(authentication);
byte[] serializedKey = this.serializeKey(AUTH_TO_ACCESS + key);
byte[] bytes = null;
RedisConnection conn = this.getConnection();
try {
bytes = conn.get(serializedKey);
} finally {
conn.close();
}
OAuth2AccessToken accessToken = this.deserializeAccessToken(bytes);
if (accessToken != null) {
OAuth2Authentication storedAuthentication = this.readAuthentication(accessToken.getValue());
if (storedAuthentication == null || !key.equals(this.authenticationKeyGenerator.extractKey(storedAuthentication))) {
this.storeAccessToken(accessToken, authentication);
}
}
return accessToken;
}
@Override
public OAuth2Authentication readAuthentication(OAuth2AccessToken token) {
return this.readAuthentication(token.getValue());
}
@Override
public OAuth2Authentication readAuthentication(String token) {
byte[] bytes = null;
RedisConnection conn = this.getConnection();
try {
bytes = conn.get(this.serializeKey("auth:" + token));
} finally {
conn.close();
}
OAuth2Authentication auth = this.deserializeAuthentication(bytes);
return auth;
}
@Override
public OAuth2Authentication readAuthenticationForRefreshToken(OAuth2RefreshToken token) {
return this.readAuthenticationForRefreshToken(token.getValue());
}
public OAuth2Authentication readAuthenticationForRefreshToken(String token) {
RedisConnection conn = getConnection();
try {
byte[] bytes = conn.get(serializeKey(REFRESH_AUTH + token));
OAuth2Authentication auth = deserializeAuthentication(bytes);
return auth;
} finally {
conn.close();
}
}
@Override
public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
byte[] serializedAccessToken = serialize(token);
byte[] serializedAuth = serialize(authentication);
byte[] accessKey = serializeKey(ACCESS + token.getValue());
byte[] authKey = serializeKey(AUTH + token.getValue());
byte[] authToAccessKey = serializeKey(AUTH_TO_ACCESS + authenticationKeyGenerator.extractKey(authentication));
byte[] approvalKey = serializeKey(UNAME_TO_ACCESS + getApprovalKey(authentication));
byte[] clientId = serializeKey(CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId());
RedisConnection conn = getConnection();
try {
conn.openPipeline();
conn.stringCommands().set(accessKey, serializedAccessToken);
conn.stringCommands().set(authKey, serializedAuth);
conn.stringCommands().set(authToAccessKey, serializedAccessToken);
if (!authentication.isClientOnly()) {
conn.rPush(approvalKey, serializedAccessToken);
}
conn.rPush(clientId, serializedAccessToken);
if (token.getExpiration() != null) {
int seconds = token.getExpiresIn();
conn.expire(accessKey, seconds);
conn.expire(authKey, seconds);
conn.expire(authToAccessKey, seconds);
conn.expire(clientId, seconds);
conn.expire(approvalKey, seconds);
}
OAuth2RefreshToken refreshToken = token.getRefreshToken();
if (refreshToken != null && refreshToken.getValue() != null) {
byte[] refresh = serialize(token.getRefreshToken().getValue());
byte[] auth = serialize(token.getValue());
byte[] refreshToAccessKey = serializeKey(REFRESH_TO_ACCESS + token.getRefreshToken().getValue());
conn.stringCommands().set(refreshToAccessKey, auth);
byte[] acces
没有合适的资源?快使用搜索试试~ 我知道了~
SpringBoot OAuth2
共78个文件
java:32个
class:32个
xml:5个
4星 · 超过85%的资源 需积分: 50 31 下载量 144 浏览量
2019-02-18
15:00:59
上传
评论 2
收藏 82KB 7Z 举报
温馨提示
采用Spring Boot 2 搭建Spring Security OAuth2 支持授权码模式和客户端模式,同时搭建的有多数据源
资源推荐
资源详情
资源评论
收起资源包目录
SpringBoot.7z (78个子文件)
SpringBoot
SpringBoot.iml 16KB
mvnw.cmd 6KB
mvnw 9KB
src
test
java
com
pdl
ApplicationTests.java 319B
main
resources
mapper
hr
AdminMapper.xml 706B
crm
UserMapper.xml 2KB
templates
static
application.yml 2KB
java
com
pdl
service
AdminService.java 2KB
UserService.java 1KB
domain
User.java 2KB
Client.java 2KB
filter
controller
ClientController.java 1KB
UserController.java 683B
TestRest.java 687B
AdminController.java 789B
dao
hr
AdminMapper.java 356B
crm
UserMapper.java 535B
util
CommonUtils.java 8KB
config
ResourceServerConfiguration.java 1KB
AuthorizationServerConfiguration.java 2KB
MyRedisTokenStore.java 17KB
DataSourceCrmConfig.java 2KB
DruidConfig.java 2KB
DataSourceHRConfig.java 2KB
SecurityConfiguration.java 3KB
Application.java 642B
security
BootClientDetails.java 3KB
response
PageQueryItems.java 549B
BaseResponse.java 373B
HttpResponse.java 4KB
SimpleResponse.java 447B
PageAndSortResponse.java 397B
HttpStatusAndMsg.java 1KB
OrderType.java 139B
Items.java 342B
ListResponse.java 334B
BootUserDetailService.java 2KB
BootClientDetailsService.java 989B
target
generated-test-sources
test-annotations
generated-sources
annotations
classes
mapper
hr
AdminMapper.xml 706B
crm
UserMapper.xml 2KB
com
pdl
service
AdminService.class 2KB
UserService.class 2KB
domain
User.class 6KB
Client.class 9KB
controller
UserController.class 901B
TestRest.class 1KB
AdminController.class 1KB
ClientController.class 2KB
Application.class 901B
dao
hr
AdminMapper.class 530B
crm
UserMapper.class 698B
util
CommonUtils.class 7KB
config
DruidConfig.class 2KB
ResourceServerConfiguration.class 2KB
AuthorizationServerConfiguration.class 3KB
DataSourceHRConfig.class 3KB
MyRedisTokenStore.class 15KB
SecurityConfiguration.class 5KB
DataSourceCrmConfig.class 3KB
security
BootClientDetailsService.class 1KB
BootUserDetailService.class 2KB
response
PageQueryItems.class 3KB
HttpStatusAndMsg.class 1KB
PageAndSortResponse.class 1KB
BaseResponse.class 2KB
ListResponse.class 780B
SimpleResponse.class 815B
HttpResponse.class 5KB
Items.class 2KB
OrderType.class 1024B
BootClientDetails.class 4KB
META-INF
spring-configuration-metadata.json 60KB
application.yml 2KB
test-classes
com
pdl
ApplicationTests.class 592B
.mvn
wrapper
maven-wrapper.jar 47KB
maven-wrapper.properties 116B
pom.xml 5KB
.gitignore 268B
共 78 条
- 1
资源评论
- 李子捌2020-11-23你这数据库初始化是会自定建表么?我这登录就报错了
LOVAR
- 粉丝: 3
- 资源: 21
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功