...
|
...
|
@@ -12,9 +12,7 @@ import org.springframework.scheduling.annotation.Async; |
|
|
import org.springframework.scheduling.annotation.EnableAsync;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -29,6 +27,9 @@ public class UserAuthLocal { |
|
|
|
|
|
private static ConcurrentHashMap<String, AuthModule> modulemap = new ConcurrentHashMap<>();
|
|
|
|
|
|
private static ConcurrentHashMap<String, Set<String>> moduleGroupmap = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
HttpRestClient httpRestClient;
|
|
|
|
...
|
...
|
@@ -44,57 +45,73 @@ public class UserAuthLocal { |
|
|
}
|
|
|
|
|
|
//加载mudule信息
|
|
|
BaseResponse<List<AuthModule>> moduleResponse = httpRestClient.exchangeForget(HttpUriContants.GET_All_MODULE, new ParameterizedTypeReference<BaseResponse<List<AuthModule>>>() {}, null);
|
|
|
BaseResponse<List<AuthModule>> moduleResponse = httpRestClient.exchangeForget(HttpUriContants.GET_All_MODULE, new ParameterizedTypeReference<BaseResponse<List<AuthModule>>>() {
|
|
|
}, null);
|
|
|
List<AuthModule> modulelist = moduleResponse.getData();
|
|
|
for (AuthModule module : modulelist) {
|
|
|
modulemap.put(module.getModuleName(), module);
|
|
|
}
|
|
|
|
|
|
//加载module组信息
|
|
|
for (Map.Entry<String,AuthModule> entry : modulemap.entrySet()){
|
|
|
AuthModule authModule = entry.getValue();
|
|
|
String group = authModule.getModuleGroup();
|
|
|
if (StringUtils.isNotBlank(group)){
|
|
|
if (moduleGroupmap.containsKey(group)){
|
|
|
moduleGroupmap.get(group).add(entry.getKey());
|
|
|
}else {
|
|
|
Set<String> groupSet = new HashSet<>();
|
|
|
groupSet.add(entry.getKey());
|
|
|
moduleGroupmap.put(group,groupSet);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* jian quan
|
|
|
*
|
|
|
* @param user
|
|
|
* @param module
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean auth(String user,String module){
|
|
|
if(usermap.isEmpty()){//改用延时加载
|
|
|
public boolean auth(String user, String module) {
|
|
|
if (usermap.isEmpty()) {//改用延时加载
|
|
|
init();
|
|
|
}
|
|
|
User u = usermap.get(user);
|
|
|
if (u == null){
|
|
|
if (u == null) {
|
|
|
return false;
|
|
|
}
|
|
|
AuthModule authModule = modulemap.get(module);
|
|
|
if (authModule == null){
|
|
|
if (authModule == null) {
|
|
|
return false;
|
|
|
}
|
|
|
//最高权限
|
|
|
if (StringUtils.isNotBlank(u.getModuleGroups())){
|
|
|
if (StringUtils.isNotBlank(u.getModuleGroups())) {
|
|
|
String[] moduleGroupArray = u.getModuleGroups().split(",");
|
|
|
for (String groupName:moduleGroupArray){
|
|
|
if ("all".equals(groupName)){
|
|
|
return true;
|
|
|
}
|
|
|
for (String groupName : moduleGroupArray) {
|
|
|
if ("all".equals(groupName)) {
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
//先查找group
|
|
|
String moduleGroup = authModule.getModuleGroup();
|
|
|
if (moduleGroup != null && StringUtils.isNotBlank(u.getModuleGroups())){
|
|
|
if (moduleGroup != null && StringUtils.isNotBlank(u.getModuleGroups())) {
|
|
|
String[] moduleGroupArray = u.getModuleGroups().split(",");
|
|
|
for (String groupName:moduleGroupArray){
|
|
|
if (moduleGroup.equals(groupName)){
|
|
|
for (String groupName : moduleGroupArray) {
|
|
|
if (moduleGroup.equals(groupName)) {
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//在查找单一类
|
|
|
if (StringUtils.isNotBlank(u.getModules())){
|
|
|
if (StringUtils.isNotBlank(u.getModules())) {
|
|
|
String[] modulesArray = u.getModules().split(",");
|
|
|
for (String moduleName:modulesArray){
|
|
|
if (module.equals(moduleName)){
|
|
|
for (String moduleName : modulesArray) {
|
|
|
if (module.equals(moduleName)) {
|
|
|
return true;
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -110,23 +127,50 @@ public class UserAuthLocal { |
|
|
* @return
|
|
|
*/
|
|
|
public User getUserByname(String name) {
|
|
|
if(usermap.isEmpty()){//改用延时加载
|
|
|
if (usermap.isEmpty()) {//改用延时加载
|
|
|
init();
|
|
|
}
|
|
|
return usermap.get(name);
|
|
|
}
|
|
|
|
|
|
public AuthModule getAuthModuleByname(String name) {
|
|
|
if (modulemap.isEmpty()) {//改用延时加载
|
|
|
init();
|
|
|
}
|
|
|
return modulemap.get(name);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取用户
|
|
|
* 获取用户所有的权限
|
|
|
*
|
|
|
* @param name
|
|
|
* @return
|
|
|
*/
|
|
|
public AuthModule getAuthModuleByname(String name) {
|
|
|
if(modulemap.isEmpty()){//改用延时加载
|
|
|
public Set<String> getAllModuleByname(String name) {
|
|
|
|
|
|
Set<String> set = new HashSet<>();
|
|
|
if (usermap.isEmpty()) {//改用延时加载
|
|
|
init();
|
|
|
}
|
|
|
return modulemap.get(name);
|
|
|
User u = usermap.get(name);
|
|
|
String groups = u.getModuleGroups();
|
|
|
if (StringUtils.isNotBlank(groups)){//添加组
|
|
|
for (String group : groups.split(",")) {
|
|
|
if ("all".equals(group)){
|
|
|
set.addAll(modulemap.keySet());
|
|
|
return set;
|
|
|
}
|
|
|
set.addAll(moduleGroupmap.get(group));
|
|
|
}
|
|
|
}
|
|
|
//添加但模块
|
|
|
String modules = u.getModules();
|
|
|
if (StringUtils.isNotBlank(modules)){//添加组
|
|
|
for (String module : modules.split(",")) {
|
|
|
set.add(module);
|
|
|
}
|
|
|
}
|
|
|
return set;
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -141,9 +185,9 @@ public class UserAuthLocal { |
|
|
BaseResponse<User> response = httpRestClient.exchangeForget(HttpUriContants.GET_USER_BY_NAME, new ParameterizedTypeReference<BaseResponse<User>>() {
|
|
|
}, map);
|
|
|
User user = response.getData();
|
|
|
if (user == null){
|
|
|
if (user == null) {
|
|
|
usermap.remove(name);
|
|
|
}else{
|
|
|
} else {
|
|
|
usermap.put(name, user);
|
|
|
}
|
|
|
}
|
...
|
...
|
@@ -157,13 +201,26 @@ public class UserAuthLocal { |
|
|
public void flushModule(String name) {
|
|
|
Map<String, String> map = new HashMap<>();
|
|
|
map.put("name", name);
|
|
|
AuthModule oldAuthModule = modulemap.get(name);
|
|
|
BaseResponse<AuthModule> response = httpRestClient.exchangeForget(HttpUriContants.MODULE_GET_BYNAME, new ParameterizedTypeReference<BaseResponse<AuthModule>>() {
|
|
|
}, map);
|
|
|
AuthModule authModule = response.getData();
|
|
|
if (authModule == null){
|
|
|
if (authModule == null) {
|
|
|
modulemap.remove(name);
|
|
|
}else{
|
|
|
//删除组里的模块
|
|
|
String group = oldAuthModule.getModuleGroup();
|
|
|
if (moduleGroupmap.containsKey(group)) {
|
|
|
moduleGroupmap.get(group).remove(name);
|
|
|
}
|
|
|
} else {
|
|
|
modulemap.put(name, authModule);
|
|
|
if (moduleGroupmap.containsKey(authModule.getModuleGroup())){
|
|
|
moduleGroupmap.get(authModule.getModuleGroup()).add(name);
|
|
|
}else {
|
|
|
Set<String> groupSet = new HashSet<>();
|
|
|
groupSet.add(name);
|
|
|
moduleGroupmap.put(authModule.getModuleGroup(),groupSet);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
...
|
...
|
|