UserAuthLocal.java
3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.ui.User;
import com.ui.contants.HttpUriContants;
import com.ui.http.HttpRestClient;
import com.ui.model.BaseResponse;
import com.ui.model.req.AuthModule;
import com.ui.model.req.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
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.concurrent.ConcurrentHashMap;
/**
* Created by zhengyouwei on 2016/7/13.
* 鉴权
*/
@Component
@EnableAsync
public class UserAuthLocal {
private static ConcurrentHashMap<String, User> usermap = new ConcurrentHashMap<>();
private static ConcurrentHashMap<String, AuthModule> modulemap = new ConcurrentHashMap<>();
@Autowired
HttpRestClient httpRestClient;
//@PostConstruct
public synchronized void init() {
//加载用户信息
BaseResponse<List<User>> response = httpRestClient.exchangeForget(HttpUriContants.GET_All_USER, new ParameterizedTypeReference<BaseResponse<List<User>>>() {
}, null);
List<User> userlist = response.getData();
for (User user : userlist) {
usermap.put(user.getName(), user);
}
//加载mudule信息
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);
}
}
/**
* jian quan
* @param user
* @param module
* @return
*/
public boolean auth(String user,String module){
if(usermap.isEmpty()){//改用延时加载
init();
}
User u = usermap.get(user);
if (u == null){
return false;
}
AuthModule authModule = modulemap.get(module);
if (authModule == null){
return false;
}
if(u.getLevel() >= authModule.getModuleLevel()){
return true;
}
return false;
}
/**
* 获取用户
*
* @param name
* @return
*/
public User getUserByname(String name) {
if(usermap.isEmpty()){//改用延时加载
init();
}
return usermap.get(name);
}
/**
* 获取用户
*
* @param name
* @return
*/
public AuthModule getAuthModuleByname(String name) {
if(modulemap.isEmpty()){//改用延时加载
init();
}
return modulemap.get(name);
}
/**
* 更新用户信息
*
* @param name
*/
@Async
public void flushUser(String name) {
Map<String, String> map = new HashMap<>();
map.put("name", name);
BaseResponse<User> response = httpRestClient.exchangeForget(HttpUriContants.GET_USER_BY_NAME, new ParameterizedTypeReference<BaseResponse<User>>() {
}, map);
User user = response.getData();
if (user == null){
usermap.remove(name);
}else{
usermap.put(name, user);
}
}
/**
* 更新用户信息
*
* @param name
*/
@Async
public void flushModule(String name) {
Map<String, String> map = new HashMap<>();
map.put("name", name);
BaseResponse<AuthModule> response = httpRestClient.exchangeForget(HttpUriContants.MODULE_GET_BYNAME, new ParameterizedTypeReference<BaseResponse<AuthModule>>() {
}, map);
AuthModule authModule = response.getData();
if (authModule == null){
modulemap.remove(name);
}else{
modulemap.put(name, authModule);
}
}
}