feat:后置校验
This commit is contained in:
parent
87ee0bd55c
commit
7c323c2753
@ -2,8 +2,8 @@ package com.flyfish.framework.service;
|
|||||||
|
|
||||||
import com.flyfish.framework.config.constants.UserCacheKeys;
|
import com.flyfish.framework.config.constants.UserCacheKeys;
|
||||||
import com.flyfish.framework.configuration.jwt.TokenProvider;
|
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.base.IUser;
|
||||||
import com.flyfish.framework.domain.po.User;
|
|
||||||
import com.flyfish.framework.enums.UserStatus;
|
import com.flyfish.framework.enums.UserStatus;
|
||||||
import com.flyfish.framework.utils.Assert;
|
import com.flyfish.framework.utils.Assert;
|
||||||
import com.flyfish.framework.utils.ReactiveRedisOperations;
|
import com.flyfish.framework.utils.ReactiveRedisOperations;
|
||||||
@ -79,27 +79,32 @@ public class MongoUserDetailsServiceImpl implements MongoUserDetailsService {
|
|||||||
public Mono<UserDetails> findByUsername(String username) {
|
public Mono<UserDetails> findByUsername(String username) {
|
||||||
String key = UserCacheKeys.get(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)
|
userService.findByUsername(username)
|
||||||
.flatMap(this::validate)
|
|
||||||
.flatMap(userDetailsConverter::mapToUserDetails)
|
.flatMap(userDetailsConverter::mapToUserDetails)
|
||||||
.flatMap(detail -> reactiveRedisOperations.set(key, detail).thenReturn(detail))
|
.flatMap(detail -> reactiveRedisOperations.set(key, detail).thenReturn(detail))
|
||||||
)
|
)
|
||||||
|
.flatMap(this::validate)
|
||||||
.switchIfEmpty(Mono.defer(() -> Mono.error(new UsernameNotFoundException("用户不存在!"))));
|
.switchIfEmpty(Mono.defer(() -> Mono.error(new UsernameNotFoundException("用户不存在!"))));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 校验
|
* 校验
|
||||||
*
|
*
|
||||||
* @param user 用户
|
* @param details 用户
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
private Mono<User> validate(User user) {
|
private Mono<UserDetails> validate(UserDetails details) {
|
||||||
return checkMap.entrySet().stream()
|
if (details instanceof AdminUserDetails) {
|
||||||
.filter(entry -> entry.getKey().apply(user))
|
AdminUserDetails user = (AdminUserDetails) details;
|
||||||
.findFirst()
|
return checkMap.entrySet().stream()
|
||||||
.map(entry -> Mono.<User>error(entry.getValue().get()))
|
.filter(entry -> entry.getKey().apply(user))
|
||||||
.orElse(Mono.just(user));
|
.findFirst()
|
||||||
|
.map(entry -> Mono.<UserDetails>error(entry.getValue().get()))
|
||||||
|
.orElse(Mono.just(user));
|
||||||
|
}
|
||||||
|
return Mono.just(details);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user