mirror of
https://gitee.com/youlaitech/youlai-mall.git
synced 2024-12-23 05:00:25 +08:00
Merge branch 'develop-local' into develop
This commit is contained in:
commit
f68667496e
@ -21,6 +21,7 @@ public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, S
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
|
||||
|
||||
@Override
|
||||
public List<SysPermission> listPermRoles() {
|
||||
return this.baseMapper.listPermRoles();
|
||||
@ -50,6 +51,7 @@ public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, S
|
||||
urlPermRoles.put(perm, roles);
|
||||
});
|
||||
redisTemplate.opsForHash().putAll(GlobalConstants.URL_PERM_ROLES_KEY, urlPermRoles);
|
||||
redisTemplate.convertAndSend("cleanRoleLocalCache","true");
|
||||
}
|
||||
// 初始化URL【按钮->角色(集合)】规则
|
||||
List<SysPermission> btnPermList = permissions.stream()
|
||||
|
@ -16,7 +16,6 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
@Configuration
|
||||
@AutoConfigureBefore(RedisAutoConfiguration.class)
|
||||
public class RedisConfig {
|
||||
|
||||
@Bean
|
||||
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
|
||||
|
||||
|
@ -0,0 +1,45 @@
|
||||
package com.youlai.gateway.component;
|
||||
|
||||
import com.google.common.cache.Cache;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author DaniR
|
||||
* @version 1.0
|
||||
* @description 本地缓存设置
|
||||
* @createDate 2021/6/16 10:08
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class AdminRoleLocalCache<T> {
|
||||
private Cache<String,T> localCache = null;
|
||||
|
||||
@PostConstruct
|
||||
private void init(){
|
||||
localCache = CacheBuilder.newBuilder()
|
||||
//设置本地缓存容器的初始容量
|
||||
.initialCapacity(1)
|
||||
//设置本地缓存的最大容量
|
||||
.maximumSize(10)
|
||||
//设置写缓存后多少秒过期
|
||||
.expireAfterWrite(120, TimeUnit.SECONDS).build();
|
||||
}
|
||||
|
||||
|
||||
public void setLocalCache(String key,T object){
|
||||
localCache.put(key,object);
|
||||
}
|
||||
|
||||
public <T> T getCache(String key){
|
||||
return (T) localCache.getIfPresent(key);
|
||||
}
|
||||
public void remove(String key){
|
||||
localCache.invalidate(key);
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.youlai.gateway.component;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.youlai.common.constant.GlobalConstants;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.connection.Message;
|
||||
import org.springframework.data.redis.connection.MessageListener;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class RedisChannelListener implements MessageListener {
|
||||
|
||||
@Autowired
|
||||
private AdminRoleLocalCache adminRoleLocalCache;
|
||||
|
||||
@Override
|
||||
public void onMessage(Message message, byte[] bytes) {
|
||||
String msg = new String(message.getBody(), StandardCharsets.UTF_8);
|
||||
String channel = new String(message.getChannel(), StandardCharsets.UTF_8);
|
||||
adminRoleLocalCache.remove(GlobalConstants.URL_PERM_ROLES_KEY);
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package com.youlai.gateway.config;
|
||||
|
||||
import com.youlai.gateway.component.RedisChannelListener;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.listener.ChannelTopic;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
|
||||
|
||||
@Configuration
|
||||
public class RedisGatewyConfig {
|
||||
|
||||
@Autowired
|
||||
private RedisConnectionFactory connectionFactory;
|
||||
|
||||
@Bean
|
||||
public RedisMessageListenerContainer redisMessageListenerContainer(){
|
||||
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
||||
container.setConnectionFactory(connectionFactory);
|
||||
container.addMessageListener(messageListenerAdapter(),channelTopic());
|
||||
return container;
|
||||
}
|
||||
|
||||
@Bean
|
||||
MessageListenerAdapter messageListenerAdapter(){
|
||||
return new MessageListenerAdapter(redisChannelListener());
|
||||
}
|
||||
|
||||
@Bean
|
||||
RedisChannelListener redisChannelListener(){
|
||||
return new RedisChannelListener();
|
||||
}
|
||||
@Bean
|
||||
ChannelTopic channelTopic(){
|
||||
return new ChannelTopic("cleanRoleLocalCache");
|
||||
}
|
||||
|
||||
}
|
@ -8,8 +8,10 @@ import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.youlai.common.constant.AuthConstants;
|
||||
import com.youlai.common.constant.GlobalConstants;
|
||||
import com.youlai.gateway.component.AdminRoleLocalCache;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
@ -39,7 +41,11 @@ import java.util.Set;
|
||||
public class ResourceServerManager implements ReactiveAuthorizationManager<AuthorizationContext> {
|
||||
|
||||
private RedisTemplate redisTemplate;
|
||||
private AdminRoleLocalCache adminRoleLocalCache;
|
||||
|
||||
// 是否演示环境
|
||||
@Value("${demo}")
|
||||
private Boolean isDemoEnv;
|
||||
@Override
|
||||
public Mono<AuthorizationDecision> check(Mono<Authentication> mono, AuthorizationContext authorizationContext) {
|
||||
ServerHttpRequest request = authorizationContext.getExchange().getRequest();
|
||||
@ -65,9 +71,14 @@ public class ResourceServerManager implements ReactiveAuthorizationManager<Autho
|
||||
// Restful接口权限设计 @link https://www.cnblogs.com/haoxianrui/p/14396990.html
|
||||
String restfulPath = request.getMethodValue() + ":" + path;
|
||||
log.info("请求方法:RESTFul请求路径:{}", restfulPath);
|
||||
|
||||
// 缓存取【URL权限标识->角色集合】权限规则
|
||||
Map<String, Object> permRolesRules = redisTemplate.opsForHash().entries(GlobalConstants.URL_PERM_ROLES_KEY);
|
||||
Map<String, Object> permRolesRules = (Map<String, Object>) adminRoleLocalCache.getCache(GlobalConstants.URL_PERM_ROLES_KEY);
|
||||
if (isDemoEnv){
|
||||
// 缓存取【URL权限标识->角色集合】权限规则
|
||||
if(null==permRolesRules){
|
||||
permRolesRules = redisTemplate.opsForHash().entries(GlobalConstants.URL_PERM_ROLES_KEY);
|
||||
adminRoleLocalCache.setLocalCache(GlobalConstants.URL_PERM_ROLES_KEY,permRolesRules);
|
||||
}
|
||||
}
|
||||
|
||||
// 根据 “请求路径” 和 权限规则中的“URL权限标识”进行Ant匹配,得出拥有权限的角色集合
|
||||
Set<String> hasPermissionRoles = CollectionUtil.newHashSet(); // 【声明定义】有权限的角色集合
|
||||
|
Loading…
Reference in New Issue
Block a user