spring security + oauth2 使用RedisTokenStore 以json格式存储

1.项目架构

 2.自己对 TokenStore 的 redis实现

package com.enterprise.auth.config;import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.ScanOptions;
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.RedisTokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStoreSerializationStrategy;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.*;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 static final boolean springDataRedis_2_0 = ClassUtils.isPresent("org.springframework.data.redis.connection.RedisStandaloneConfiguration", RedisTokenStore.class.getClassLoader());private final RedisConnectionFactory connectionFactory;private AuthenticationKeyGenerator authenticationKeyGenerator = new DefaultAuthenticationKeyGenerator();private RedisTokenStoreSerializationStrategy serializationStrategy = new MyRedisTokenStoreSerializationStrategy();private String prefix = "";private Method redisConnectionSet_2_0;public MyRedisTokenStore(RedisConnectionFactory connectionFactory) {this.connectionFactory = connectionFactory;if (springDataRedis_2_0) {this.loadRedisConnectionMethods_2_0();}}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 void loadRedisConnectionMethods_2_0() {this.redisConnectionSet_2_0 = ReflectionUtils.findMethod(RedisConnection.class, "set", new Class[]{byte[].class, byte[].class});}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);}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;}public OAuth2Authentication readAuthentication(OAuth2AccessToken token) {return this.readAuthentication(token.getValue());}public OAuth2Authentication readAuthentication(String token) {byte[] bytes = null;RedisConnection conn = this.getConnection();try {bytes = conn.get(this.serializeKey("auth:" + token));} finally {conn.close();}OAuth2Authentication var4 = this.deserializeAuthentication(bytes);return var4;}public OAuth2Authentication readAuthenticationForRefreshToken(OAuth2RefreshToken token) {return this.readAuthenticationForRefreshToken(token.getValue());}public OAuth2Authentication readAuthenticationForRefreshToken(String token) {RedisConnection conn = this.getConnection();OAuth2Authentication var5;try {byte[] bytes = conn.get(this.serializeKey("refresh_auth:" + token));OAuth2Authentication auth = this.deserializeAuthentication(bytes);var5 = auth;} finally {conn.close();}return var5;}public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {byte[] serializedAccessToken = this.serialize((Object)token);byte[] serializedAuth = this.serialize((Object)authentication);byte[] accessKey = this.serializeKey("access:" + token.getValue());byte[] authKey = this.serializeKey("auth:" + token.getValue());byte[] authToAccessKey = this.serializeKey("auth_to_access:" + this.authenticationKeyGenerator.extractKey(authentication));byte[] approvalKey = this.serializeKey("uname_to_access:" + getApprovalKey(authentication));byte[] clientId = this.serializeKey("client_id_to_access:" + authentication.getOAuth2Request().getClientId());RedisConnection conn = this.getConnection();try {conn.openPipeline();if (springDataRedis_2_0) {try {this.redisConnectionSet_2_0.invoke(conn, accessKey, serializedAccessToken);this.redisConnectionSet_2_0.invoke(conn, authKey, serializedAuth);this.redisConnectionSet_2_0.invoke(conn, authToAccessKey, serializedAccessToken);} catch (Exception var24) {throw new RuntimeException(var24);}} else {conn.set(accessKey, serializedAccessToken);conn.set(authKey, serializedAuth);conn.set(authToAccessKey, serializedAccessToken);}if (!authentication.isClientOnly()) {conn.sAdd(approvalKey, new byte[][]{serializedAccessToken});}conn.sAdd(clientId, new byte[][]{serializedAccessToken});if (token.getExpiration() != null) {int seconds = token.getExpiresIn();conn.expire(accessKey, (long)seconds);conn.expire(authKey, (long)seconds);conn.expire(authToAccessKey, (long)seconds);conn.expire(clientId, (long)seconds);conn.expire(approvalKey, (long)seconds);}OAuth2RefreshToken refreshToken = token.getRefreshToken();if (refreshToken != null && refreshToken.getValue() != null) {byte[] refresh = this.serialize(token.getRefreshToken().getValue());byte[] auth = this.serialize(token.getValue());byte[] refreshToAccessKey = this.serializeKey("refresh_to_access:" + token.getRefreshToken().getValue());byte[] accessToRefreshKey = this.serializeKey("access_to_refresh:" + token.getValue());if (springDataRedis_2_0) {try {this.redisConnectionSet_2_0.invoke(conn, refreshToAccessKey, auth);this.redisConnectionSet_2_0.invoke(conn, accessToRefreshKey, refresh);} catch (Exception var23) {throw new RuntimeException(var23);}} else {conn.set(refreshToAccessKey, auth);conn.set(accessToRefreshKey, refresh);}if (refreshToken instanceof ExpiringOAuth2RefreshToken) {ExpiringOAuth2RefreshToken expiringRefreshToken = (ExpiringOAuth2RefreshToken)refreshToken;Date expiration = expiringRefreshToken.getExpiration();if (expiration != null) {int seconds = Long.valueOf((expiration.getTime() - System.currentTimeMillis()) / 1000L).intValue();conn.expire(refreshToAccessKey, (long)seconds);conn.expire(accessToRefreshKey, (long)seconds);}}}conn.closePipeline();} finally {conn.close();}}private static String getApprovalKey(OAuth2Authentication authentication) {String userName = authentication.getUserAuthentication() == null ? "" : authentication.getUserAuthentication().getName();return getApprovalKey(authentication.getOAuth2Request().getClientId(), userName);}private static String getApprovalKey(String clientId, String userName) {return clientId + (userName == null ? "" : ":" + userName);}public void removeAccessToken(OAuth2AccessToken accessToken) {this.removeAccessToken(accessToken.getValue());}public OAuth2AccessToken readAccessToken(String tokenValue) {byte[] key = this.serializeKey("access:" + tokenValue);byte[] bytes = null;RedisConnection conn = this.getConnection();try {bytes = conn.get(key);} finally {conn.close();}OAuth2AccessToken var5 = this.deserializeAccessToken(bytes);return var5;}public void removeAccessToken(String tokenValue) {byte[] accessKey = this.serializeKey("access:" + tokenValue);byte[] authKey = this.serializeKey("auth:" + tokenValue);byte[] accessToRefreshKey = this.serializeKey("access_to_refresh:" + tokenValue);RedisConnection conn = this.getConnection();try {conn.openPipeline();conn.get(accessKey);conn.get(authKey);conn.del(new byte[][]{accessKey});conn.del(new byte[][]{accessToRefreshKey});conn.del(new byte[][]{authKey});List<Object> results = conn.closePipeline();byte[] access = (byte[])((byte[])results.get(0));byte[] auth = (byte[])((byte[])results.get(1));OAuth2Authentication authentication = this.deserializeAuthentication(auth);if (authentication != null) {String key = this.authenticationKeyGenerator.extractKey(authentication);byte[] authToAccessKey = this.serializeKey("auth_to_access:" + key);byte[] unameKey = this.serializeKey("uname_to_access:" + getApprovalKey(authentication));byte[] clientId = this.serializeKey("client_id_to_access:" + authentication.getOAuth2Request().getClientId());conn.openPipeline();conn.del(new byte[][]{authToAccessKey});conn.sRem(unameKey, new byte[][]{access});conn.sRem(clientId, new byte[][]{access});conn.del(new byte[][]{this.serialize("access:" + key)});conn.closePipeline();}} finally {conn.close();}}public void storeRefreshToken(OAuth2RefreshToken refreshToken, OAuth2Authentication authentication) {byte[] refreshKey = this.serializeKey("refresh:" + refreshToken.getValue());byte[] refreshAuthKey = this.serializeKey("refresh_auth:" + refreshToken.getValue());byte[] serializedRefreshToken = this.serialize((Object)refreshToken);RedisConnection conn = this.getConnection();try {conn.openPipeline();if (springDataRedis_2_0) {try {this.redisConnectionSet_2_0.invoke(conn, refreshKey, serializedRefreshToken);this.redisConnectionSet_2_0.invoke(conn, refreshAuthKey, this.serialize((Object)authentication));} catch (Exception var13) {throw new RuntimeException(var13);}} else {conn.set(refreshKey, serializedRefreshToken);conn.set(refreshAuthKey, this.serialize((Object)authentication));}if (refreshToken instanceof ExpiringOAuth2RefreshToken) {ExpiringOAuth2RefreshToken expiringRefreshToken = (ExpiringOAuth2RefreshToken)refreshToken;Date expiration = expiringRefreshToken.getExpiration();if (expiration != null) {int seconds = Long.valueOf((expiration.getTime() - System.currentTimeMillis()) / 1000L).intValue();conn.expire(refreshKey, (long)seconds);conn.expire(refreshAuthKey, (long)seconds);}}conn.closePipeline();} finally {conn.close();}}public OAuth2RefreshToken readRefreshToken(String tokenValue) {byte[] key = this.serializeKey("refresh:" + tokenValue);byte[] bytes = null;RedisConnection conn = this.getConnection();try {bytes = conn.get(key);} finally {conn.close();}OAuth2RefreshToken var5 = this.deserializeRefreshToken(bytes);return var5;}public void removeRefreshToken(OAuth2RefreshToken refreshToken) {this.removeRefreshToken(refreshToken.getValue());}public void removeRefreshToken(String tokenValue) {byte[] refreshKey = this.serializeKey("refresh:" + tokenValue);byte[] refreshAuthKey = this.serializeKey("refresh_auth:" + tokenValue);byte[] refresh2AccessKey = this.serializeKey("refresh_to_access:" + tokenValue);byte[] access2RefreshKey = this.serializeKey("access_to_refresh:" + tokenValue);RedisConnection conn = this.getConnection();try {conn.openPipeline();conn.del(new byte[][]{refreshKey});conn.del(new byte[][]{refreshAuthKey});conn.del(new byte[][]{refresh2AccessKey});conn.del(new byte[][]{access2RefreshKey});conn.closePipeline();} finally {conn.close();}}public void removeAccessTokenUsingRefreshToken(OAuth2RefreshToken refreshToken) {this.removeAccessTokenUsingRefreshToken(refreshToken.getValue());}private void removeAccessTokenUsingRefreshToken(String refreshToken) {byte[] key = this.serializeKey("refresh_to_access:" + refreshToken);List<Object> results = null;RedisConnection conn = this.getConnection();try {conn.openPipeline();conn.get(key);conn.del(new byte[][]{key});results = conn.closePipeline();} finally {conn.close();}if (results != null) {byte[] bytes = (byte[])((byte[])results.get(0));String accessToken = this.deserializeString(bytes);if (accessToken != null) {this.removeAccessToken(accessToken);}}}private List<byte[]> getByteLists(byte[] approvalKey, RedisConnection conn) {Long size = conn.sCard(approvalKey);List<byte[]> byteList = new ArrayList(size.intValue());Cursor cursor = conn.sScan(approvalKey, ScanOptions.NONE);while(cursor.hasNext()) {Object next = cursor.next();byteList.add(next.toString().getBytes(StandardCharsets.UTF_8));}return byteList;}public Collection<OAuth2AccessToken> findTokensByClientIdAndUserName(String clientId, String userName) {byte[] approvalKey = this.serializeKey("uname_to_access:" + getApprovalKey(clientId, userName));List<byte[]> byteList = null;RedisConnection conn = this.getConnection();try {byteList = this.getByteLists(approvalKey, conn);} finally {conn.close();}if (byteList != null && byteList.size() != 0) {List<OAuth2AccessToken> accessTokens = new ArrayList(byteList.size());Iterator var7 = byteList.iterator();while(var7.hasNext()) {byte[] bytes = (byte[])var7.next();OAuth2AccessToken accessToken = this.deserializeAccessToken(bytes);accessTokens.add(accessToken);}return Collections.unmodifiableCollection(accessTokens);} else {return Collections.emptySet();}}public Collection<OAuth2AccessToken> findTokensByClientId(String clientId) {byte[] key = this.serializeKey("client_id_to_access:" + clientId);List<byte[]> byteList = null;RedisConnection conn = this.getConnection();try {byteList = this.getByteLists(key, conn);} finally {conn.close();}if (byteList != null && byteList.size() != 0) {List<OAuth2AccessToken> accessTokens = new ArrayList(byteList.size());Iterator var6 = byteList.iterator();while(var6.hasNext()) {byte[] bytes = (byte[])var6.next();OAuth2AccessToken accessToken = this.deserializeAccessToken(bytes);accessTokens.add(accessToken);}return Collections.unmodifiableCollection(accessTokens);} else {return Collections.emptySet();}}
}

3.自定义序列化类

package com.enterprise.auth.config;import com.google.gson.Gson;
import org.springframework.security.oauth2.provider.token.store.redis.BaseRedisTokenStoreSerializationStrategy;import java.nio.charset.StandardCharsets;
import java.util.Map;public class MyRedisTokenStoreSerializationStrategy extends BaseRedisTokenStoreSerializationStrategy {@Override//反序列化内部protected <T> T deserializeInternal(byte[] bytes, Class<T> aClass) {String s = new String(bytes);Gson gson = new Gson();return gson.fromJson(s,aClass);}@Override//反序列化字符串内部protected String deserializeStringInternal(byte[] bytes) {return new String(bytes);}@Override//序列化内部protected byte[] serializeInternal(Object o) {Gson gson = new Gson();String json = gson.toJson(o);return json.getBytes(StandardCharsets.UTF_8);}@Override//序列化内部protected byte[] serializeInternal(String s) {return  s.getBytes(StandardCharsets.UTF_8);}
}

4.配置使用redis类型的 TokenStore

package com.enterprise.auth.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.oauth2.provider.token.TokenStore;@Configuration
public class MyRedisTokenStoreConfig {@Autowiredprivate RedisConnectionFactory redisConnectionFactory;@Beanpublic TokenStore tokenStore() {MyRedisTokenStore redisTokenStore = new MyRedisTokenStore(redisConnectionFactory);return redisTokenStore;}
}

5.完成认证模块的编写后,测试登录

 

 json已经保存在数据库了

但是!!!!!,保存没问题,取出来的时候就有问题了,把这三个文件复制到资源服务器,让资源服务器也用MyRedisTokenStore 的方式读取权限信息.

 然后访问,到json权限信息转成实体类的时候,就有问题了,各种各样oauth2 中的数据实体类,没有构造方法,无法创建对象,先转成map在手动新建对象同样不行,

 redis中的配置已经读到了

 但是需要实例化的对象类路径,这么长一串,没有构造方法,无法新建对象.

报错>>>

 

结束<<<<<<<<

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/58968.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

第八篇: K8S Prometheus Operator实现Ceph集群企业微信机器人告警

Prometheus Operator实现Ceph集群企业微信告警 实现方案 我们的k8s集群与ceph集群是部署在不同的服务器上&#xff0c;因此实现方案如下&#xff1a; (1) ceph集群开启mgr内置的exporter服务&#xff0c;用于获取ceph集群的metrics (2) k8s集群通过 Service Endponit Ser…

关于mvvm简易封装(三)

序言 主要是关于前两篇文章的优化总结&#xff0c;之前很多人问demo啥的&#xff0c;这次优化了一些框架贴上代码。这次就不讲封装思路了&#xff0c;只讲一些优化思路方法。代码之前一直没传&#xff0c;忘了&#xff0c;最近传上来了&#xff0c;虽然有的地方没优化&#xf…

centos7 ESXi 磁盘扩充容量

1、背景 有一天&#xff0c;突然程序报空间不足了。。。。。。 2023-06-23 02:26:51.631 UTC [26190] LOG: could not open temporary statistics file "pg_stat_tmp/global.tmp": No space left on device 2023-06-23 02:26:51.631 UTC [26190] LOG: could not …

vue table动态合并, 自定义合并,参照合并,组合合并

<template><div><el-table:data"tableData":span-method"objectSpanMethod"border:header-cell-style"{ textAlign: center }"><el-table-column prop"area" label"区域" align"center">…

Django Rest_Framework(二)

文章目录 1. http请求响应1.1. 请求与响应1.1.1 Request1.1.1.1 常用属性1&#xff09;.data2&#xff09;.query_params3&#xff09;request._request 基本使用 1.1.2 Response1.1.2.1 构造方式1.1.2.2 response对象的属性1&#xff09;.data2&#xff09;.status_code3&…

【升职加薪秘籍】我在服务监控方面的实践(4)-日志监控

大家好,我是蓝胖子&#xff0c;关于性能分析的视频和文章我也大大小小出了有一二十篇了&#xff0c;算是已经有了一个系列&#xff0c;之前的代码已经上传到github.com/HobbyBear/performance-analyze 接下来这段时间我将在之前内容的基础上&#xff0c;结合自己在公司生产上构…

SpringCloud实用篇1——eureka注册中心 Ribbon负载均衡原理 nacos注册中心

目录 1 微服务1.1 微服务的演变1.2 微服务1.3 SpringCloud1.4 小结 2 服务拆分及远程调用2.1 服务拆分2.2 服务拆分案例2.3 实现远程调用2.4 提供者与消费者 3 Eureka注册中心3.1 Eureka的结构和作用3.2 搭建eureka-server3.3 服务注册3.4 服务发现 4 Ribbon负载均衡4.1 负载均…

【css】渐变

渐变是设置一种颜色或者多种颜色之间的过度变化。 两种渐变类型&#xff1a; 线性渐变&#xff08;向下/向上/向左/向右/对角线&#xff09; 径向渐变&#xff08;由其中心定义&#xff09; 1、线性渐变 语法&#xff1a;background-image: linear-gradient(direction, co…

webshell链接工具-Godzilla(哥斯拉)

项目地址 https://github.com/BeichenDream/Godzilla

将Map存到数据库中,并且支持数据类型原样取回

1.数据库设计 1.1 表设计 create table variables (id bigint not null comment 主键,business_key varchar(128) null comment 业务key,key varchar(128) null comment Map中的key,value varchar(25…

机器学习概述及其主要算法

目录 1、什么是机器学习 2、数据集 2.1、结构 3、算法分类 4、算法简介 4.1、K-近邻算法 4.2、贝叶斯分类 4.3、决策树和随机森林 4.4、逻辑回归 4.5、神经网络 4.6、线性回归 4.7、岭回归 4.8、K-means 5、机器学习开发流程 6、学习框架 1、什么是机器学习 机器…

Linux C 语言 mosquitto 方式 MQTT 发布消息

1 说明 采用 mosquitto 库&#xff0c;实现对主题发布消息。 其中服务器有做限制&#xff0c;需要对应的 cilent id &#xff0c;cafile 、certfile 、keyfile 等配置 2 开发环境 采用ubuntu 直接编译调试 安装mosquitto 库 sudo apt install libmosquitto-dev sudo apt-ge…