当前位置:网站首页>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
边栏推荐
- The sixth MySQL job - query data - multiple conditions
- SSH, SCP command appears permission denied, please try again solution
- 118. Yanghui triangle
- Use of exec series functions (EXECL, execlp, execle, execv, execvp)
- Global and Chinese market for baked potato chips 2022-2028: Research Report on technology, participants, trends, market size and share
- MySQL project 8 summary
- OpenCV图像处理-灰度处理
- MySQL Chapter 5 Summary
- 【LeetCode】59. Spiral matrix II
- Idea remote debugger
猜你喜欢

Postman入门教程

3行3列整形二维数组,求对角之和

Linux下安裝Mysql【詳細】

Write data to local file

Omni channel, multi scenario and cross platform, how does app analyze channel traffic with data

Call API interface to generate QR code of wechat applet with different colors

36 qtextedit control input multiline text

哪些PHP开源作品值得关注

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

Express (I) - easy to get started
随机推荐
35 qlineedit control synthesis example
Use of exec series functions (EXECL, execlp, execle, execv, execvp)
[echart] i. how to learn echart and its characteristic document reading notes
The sixth MySQL job - query data - multiple conditions
[binary search] 4 Find the median of two positive arrays
MySQL第十三次作业-事务管理
MySQL seventh job - update data
开发者,微服务架构到底是什么?
Introduction to sysbench Basics
Notes - simple but adequate series_ KVM quick start
8-图文打造LeeCode算法宝典-最小栈与LRU缓存机制算法题解
Which PHP open source works deserve attention
Vscode environment setup: synchronous configuration
【LeetCode】59. Spiral matrix II
MySQL 12th job - Application of stored procedure
Omni channel, multi scenario and cross platform, how does app analyze channel traffic with data
瑞萨电子面向物联网应用推出完整的智能传感器解决方案
Search engine advanced search method records
JS reverse | four libraries and one platform response data encryption
Is it safe to open an account in the school of Finance and business?