当前位置:网站首页>Query online users and forced withdrawal users based on oauth2
Query online users and forced withdrawal users based on oauth2
2022-06-26 10:44:00 【MervynLammm】
be based on OAuth2 Query online users and forced users
scene
Recent projects require certification and authorization 、 Restrict authorized login users .
You need to count the current number of online users , And forced withdrawal of designated users .
effect

Realization
Get all online users
After logging in ,OAuth2 Will send to Redis Store user's token Information , The key is token value , The value is OAuth2 Authentication object of .
Realize the idea : from Redis Get all the token value , And adoption RedisTokenStore Get OAuth2 Authentication object of .
/** * VO */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class OnlineUserVo {
private String token;
private Long userId;
private String name;
private Date loginTime; // Landing time
private Date expiration; // Due time
private Integer expiresIn; // The rest of the time
}
/** * RedisTokenStore Injection allocation */
@Configuration
@EnableAuthorizationServer
public class TokenStoreConfig {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public RedisTokenStore redisTokenStore() {
return new RedisTokenStore(redisConnectionFactory);
}
}
//redis Deposit token Key prefix for , It can be downloaded from RedisTokenStore See... In the source code
public static final String REDIS_AUTH_KEY="auth:";
@Autowired
private RedisTokenStore redisTokenStore;
public List<OnlineUserVo> getOnlineUserList() {
List<OnlineUserVo> list = new ArrayList<>();
//oauth All stored keys
Set<String> keys = redisTemplate.keys(OnlineConstants.REDIS_AUTH_KEY + "*");
for (String key : keys) {
//keys = "auth:" + token
// according to token obtain OAuth2AccessToken
String token = key.substring(key.indexOf(":") + 1);
// according to token Values obtained OAuth2AccessToken object
OAuth2AccessToken oAuth2AccessToken = redisTokenStore.readAccessToken(token);
// Get users id, Users of this project id Store in OAuth2AccessToken Object additionalInformation In the attribute .
// Modify... According to the actual situation
Map<String, Object> map = oAuth2AccessToken.getAdditionalInformation();
if (map != null) {
// Get users id
Long userId = (Long) map.get("openid");
if (userId != null) {
// Get user information
BaseUser user = this.userService.getUserById(userId);
// Get user login log
BaseAccountLogs logs = this.accountLogsService.getUserNewestLogin(userId);
list.add(new OnlineUserVo(token, userId, user.getFullName(),
logs.getLoginTime(),
oAuth2AccessToken.getExpiration(),
oAuth2AccessToken.getExpiresIn()));
}
}
}
return list;
}
Get the number of online users
Getting the number of online users is much easier , Just get Redis in token The number of keys .
public Integer getOnlineUserNum() {
Set<String> keys = redisTemplate.keys(OnlineConstants.REDIS_AUTH_KEY + "*");
return keys.size();
}
Force the user to log out
Will the user token from Redis Delete from
public void forceLogout(String token) {
redisTokenStore.removeAccessToken(token);
}
Reference material
边栏推荐
- Global and Chinese market of recycled paper 2022-2028: Research Report on technology, participants, trends, market size and share
- What is LSP
- 904. fruit baskets
- [depth first search] 312 Poke balloon
- 開發者,微服務架構到底是什麼?
- Progressive Web 应用程序PWA是应用程序开发的未来
- Vscode environment setup: synchronous configuration
- Redis中执行Lua脚本
- 【無標題】
- MySQL第四章总结
猜你喜欢
![[echart] i. how to learn echart and its characteristic document reading notes](/img/21/5405ae302df77d2ba07d9f5e5537f3.jpg)
[echart] i. how to learn echart and its characteristic document reading notes

Quantitative investment learning - Introduction to classic books

Linux下安装Mysql【详细】

How to start the learning journey of webrtc native cross platform development?

Reshape a two-dimensional array with 3 rows and 3 columns to find the sum of the diagonals

Linux下安裝Mysql【詳細】

What is in the method area - class file, class file constant pool, runtime constant pool

Servlet learning notes II

String constant pool, class constant pool, and runtime constant pool

【LeetCode】59. Spiral matrix II
随机推荐
Index summary of blog articles -- Industrial Internet
DBSCAN
Developers, what is the microservice architecture?
MySQL 9th job - connection Query & sub query
Problems encountered in the application and development of Hongmeng and some roast
MySQL第六次作业-查询数据-多条件
二叉树常见面试题
MySQL 13th job - transaction management
Vscode environment setup: synchronous configuration
About multi table query of MySQL
How to change the QR code material color of wechat applet
Basic MySQL
36 qtextedit control input multiline text
1. sum of two numbers (leetcode topic)
【在线仿真】Arduino UNO PWM 控制直流电机转速
Redis中执行Lua脚本
基础-MySQL
Small example of SSM project, detailed tutorial of SSM integration
Dynamic library connection - symbol conflict - global symbol intervention
What is a botnet