当前位置:网站首页>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
边栏推荐
- Execute Lua script in redis
- Selection of webrtc video codec type VP8 H264 or other? (openh264 encoding, ffmpeg decoding)
- AIX basic operation record
- Yarn package management tool
- VS或Qt编译链接过程中出现“无法解析的外部符号”的原因:
- Linux下安裝Mysql【詳細】
- Notes - simple but adequate series_ KVM quick start
- 看我在Map<String, String>集合中,存入Integer类型数据
- SQL Server foundation introduction collation
- Blog article index Summary - wechat games
猜你喜欢

Under the double reduction, the amount of online education has plummeted. Share 12 interesting uses of webrtc

哪些PHP开源作品值得关注

DBSCAN

開發者,微服務架構到底是什麼?

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

36 qtextedit control input multiline text

OpenCV图像处理-灰度处理

SSH, SCP command appears permission denied, please try again solution

【Leetcode】76. Minimum covering substring

String constant pool, class constant pool, and runtime constant pool
随机推荐
MySQL第十一作业-视图的应用
[echart] II. User manual and configuration item reading notes
基础-MySQL
MySQL项目7总结
String constant pool, class constant pool, and runtime constant pool
1. sum of two numbers (leetcode topic)
Opencv image processing - grayscale processing
Yarn package management tool
Problems encountered in the application and development of Hongmeng and some roast
118. Yanghui triangle
Cmake / set command
磁带库简单记录1
瑞萨电子面向物联网应用推出完整的智能传感器解决方案
echo $?
MySQL第六次作业-查询数据-多条件
Hcia-dhcp experiment
MySQL project 8 summary
Matrix fast power notes
8- creating leecode algorithm with pictures and texts - algorithm solution of minimum stack and LRU caching mechanism
[sans titre]