Spring Security OAuth2 实现登录互踢的示例代码
作者:冷冷 时间:2023-09-04 19:09:28
本文主要介绍了Spring Security OAuth2 实现登录互踢的示例代码,分享给大家,具体如下:
背景说明
一个账号只能一处登录,类似的业务需求在现有后管类系统是非常常见的。 但在原有的 spring security oauth2 令牌方法流程(所谓的登录)无法满足类似的需求。
我们先来看 TokenEndpoint
的方法流程
客户端 带参访问 /oauth/token 接口,最后去调用 TokenGranter
TokenGranter
根据不同的授权类型,获取用户认证信息 并去调用TokenServices
生成令牌
重新 TokenService
重写发放逻辑createAccessToken
,当用户管理的令牌存在时则删除重新创建,这样会导致之前登陆获取的token 失效,顺理成章的被挤掉。
@Transactional
public OAuth2AccessToken createAccessToken(OAuth2Authentication authentication) throws AuthenticationException {
OAuth2AccessToken existingAccessToken = tokenStore.getAccessToken(authentication);
OAuth2RefreshToken refreshToken = null;
// 重写此处,当用户关联的token 存在时,删除原有令牌
if (existingAccessToken != null) {
tokenStore.removeAccessToken(existingAccessToken);
}
else if (refreshToken instanceof ExpiringOAuth2RefreshToken) {
ExpiringOAuth2RefreshToken expiring = (ExpiringOAuth2RefreshToken) refreshToken;
if (System.currentTimeMillis() > expiring.getExpiration().getTime()) {
refreshToken = createRefreshToken(authentication);
}
}
OAuth2AccessToken accessToken = createAccessToken(authentication, refreshToken);
tokenStore.storeAccessToken(accessToken, authentication);
// In case it was modified
refreshToken = accessToken.getRefreshToken();
if (refreshToken != null) {
tokenStore.storeRefreshToken(refreshToken, authentication);
}
return accessToken;
}
重写 Token key 生成逻辑
如上代码,我们实现用户单一终端的唯一性登录,什么是单一终端 我们可以类比 QQ 登录 移动端和 PC 端可以同时登录,但 移动端 和移动端不能同时在线。
如何能够实现 在不同客户端也能够唯一性登录呢?
先来看上文源码 `OAuth2AccessToken existingAccessToken=tokenStore.getAccessToken(authentication);
是如何根据用户信息判断 token 存在的呢?
public OAuth2AccessToken getAccessToken(OAuth2Authentication authentication) {
String key = authenticationKeyGenerator.extractKey(authentication);
// redis 查询逻辑,根据 key
return accessToken;
}
AuthenticationKeyGenerator key值生成器 默认情况下根据 username
/clientId
/scope
参数组合生成唯一token
public String extractKey(OAuth2Authentication authentication) {
Map<String, String> values = new LinkedHashMap<String, String>();
OAuth2Request authorizationRequest = authentication.getOAuth2Request();
if (!authentication.isClientOnly()) {
values.put(USERNAME, authentication.getName());
}
values.put(CLIENT_ID, authorizationRequest.getClientId());
if (authorizationRequest.getScope() != null) {
values.put(SCOPE, OAuth2Utils.formatParameterList(new TreeSet<String>(authorizationRequest.getScope())));
}
return generateKey(values);
}
若想实现,多终端的唯一性登录,只需要使得同一个用户在多个终端生成的 token
一致,加上上文提到的 createToken 修改逻辑,既去掉extractKey
的 clientId 条件,不区分终端即可
public String extractKey(OAuth2Authentication authentication) {
Map<String, String> values = new LinkedHashMap<String, String>();
OAuth2Request authorizationRequest = authentication.getOAuth2Request();
if (!authentication.isClientOnly()) {
values.put(USERNAME, authentication.getName());
}
if (authorizationRequest.getScope() != null) {
values.put(SCOPE, OAuth2Utils.formatParameterList(new TreeSet<String>(authorizationRequest.getScope())));
}
return generateKey(values);
}
最后在 authserver 中注入新的 TokenService 即可
来源:https://segmentfault.com/a/1190000022513428
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#中Lambda表达式的三种写法
MyBatis动态SQL表达式详解
Android实现带描边的圆角图片
![](https://img.aspxhome.com/file/2023/8/95018_0s.jpg)
Eclipse快速添加get、set方法的操作技巧
![](https://img.aspxhome.com/file/2023/7/78937_0s.jpg)
详解SpringBoot+Lucene案例介绍
Android使用ContentResolver搜索手机通讯录的方法
Android中Window的管理深入讲解
Gradle属性设置及环境变量全面教程
java通过Idea远程一键部署springboot到Docker详解
![](https://img.aspxhome.com/file/2023/9/81369_0s.jpg)
Android中通过Notification&NotificationManager实现消息通知
![](https://img.aspxhome.com/file/2023/7/90187_0s.png)
spring boot如何使用AOP统一处理web请求
MyBatis分页插件PageHelper的使用与原理
![](https://img.aspxhome.com/file/2023/5/101985_0s.png)
弹出一个带确认和取消的dialog实例
详解SpringMVC @RequestBody接收Json对象字符串
详解如何为SpringBoot项目中的自定义配置添加IDE支持
![](https://img.aspxhome.com/file/2023/5/104205_0s.png)
Android 在程序运行时申请权限的实例讲解
![](https://img.aspxhome.com/file/2023/7/85997_0s.jpg)
spring aop之链式调用的实现
![](https://img.aspxhome.com/file/2023/4/84654_0s.png)