feat:后置校验

This commit is contained in:
wangyu 2022-01-06 23:12:09 +08:00
parent 87ee0bd55c
commit 7c323c2753

View File

@ -2,8 +2,8 @@ package com.flyfish.framework.service;
import com.flyfish.framework.config.constants.UserCacheKeys;
import com.flyfish.framework.configuration.jwt.TokenProvider;
import com.flyfish.framework.domain.AdminUserDetails;
import com.flyfish.framework.domain.base.IUser;
import com.flyfish.framework.domain.po.User;
import com.flyfish.framework.enums.UserStatus;
import com.flyfish.framework.utils.Assert;
import com.flyfish.framework.utils.ReactiveRedisOperations;
@ -79,27 +79,32 @@ public class MongoUserDetailsServiceImpl implements MongoUserDetailsService {
public Mono<UserDetails> findByUsername(String username) {
String key = UserCacheKeys.get(username);
// 优先从缓存读取如果缓存不存在查询转换后放入缓存
return reactiveRedisOperations.hasKey(key).flatMap(exists -> Boolean.TRUE.equals(exists) ? reactiveRedisOperations.get(key) :
return reactiveRedisOperations.hasKey(key).flatMap(exists -> Boolean.TRUE.equals(exists) ?
reactiveRedisOperations.get(key) :
userService.findByUsername(username)
.flatMap(this::validate)
.flatMap(userDetailsConverter::mapToUserDetails)
.flatMap(detail -> reactiveRedisOperations.set(key, detail).thenReturn(detail))
)
.flatMap(this::validate)
.switchIfEmpty(Mono.defer(() -> Mono.error(new UsernameNotFoundException("用户不存在!"))));
}
/**
* 校验
*
* @param user 用户
* @param details 用户
* @return 结果
*/
private Mono<User> validate(User user) {
return checkMap.entrySet().stream()
.filter(entry -> entry.getKey().apply(user))
.findFirst()
.map(entry -> Mono.<User>error(entry.getValue().get()))
.orElse(Mono.just(user));
private Mono<UserDetails> validate(UserDetails details) {
if (details instanceof AdminUserDetails) {
AdminUserDetails user = (AdminUserDetails) details;
return checkMap.entrySet().stream()
.filter(entry -> entry.getKey().apply(user))
.findFirst()
.map(entry -> Mono.<UserDetails>error(entry.getValue().get()))
.orElse(Mono.just(user));
}
return Mono.just(details);
}
/**