|
@@ -2,21 +2,33 @@ package com.tianbo.warehouse.controller; |
|
@@ -2,21 +2,33 @@ package com.tianbo.warehouse.controller; |
2
|
|
2
|
|
3
|
import com.alibaba.fastjson.JSON;
|
3
|
import com.alibaba.fastjson.JSON;
|
4
|
|
4
|
|
|
|
5
|
+import com.alibaba.fastjson.JSONException;
|
|
|
6
|
+import com.alibaba.fastjson.JSONObject;
|
5
|
import com.google.code.kaptcha.impl.DefaultKaptcha;
|
7
|
import com.google.code.kaptcha.impl.DefaultKaptcha;
|
6
|
|
8
|
|
7
|
import com.thoughtworks.xstream.core.util.Base64Encoder;
|
9
|
import com.thoughtworks.xstream.core.util.Base64Encoder;
|
8
|
import com.tianbo.warehouse.controller.response.ResultJson;
|
10
|
import com.tianbo.warehouse.controller.response.ResultJson;
|
9
|
import com.tianbo.warehouse.model.ROLE;
|
11
|
import com.tianbo.warehouse.model.ROLE;
|
10
|
import com.tianbo.warehouse.model.Token;
|
12
|
import com.tianbo.warehouse.model.Token;
|
|
|
13
|
+import com.tianbo.warehouse.model.USERS;
|
|
|
14
|
+import com.tianbo.warehouse.security.filter.JwtTokenUtil;
|
|
|
15
|
+import com.tianbo.warehouse.service.PermissionService;
|
11
|
import com.tianbo.warehouse.service.RoleService;
|
16
|
import com.tianbo.warehouse.service.RoleService;
|
12
|
|
17
|
|
|
|
18
|
+import com.tianbo.warehouse.service.UserService;
|
13
|
import com.tianbo.warehouse.util.RedisUtils;
|
19
|
import com.tianbo.warehouse.util.RedisUtils;
|
14
|
|
20
|
|
|
|
21
|
+import io.swagger.annotations.ApiOperation;
|
15
|
import lombok.extern.slf4j.Slf4j;
|
22
|
import lombok.extern.slf4j.Slf4j;
|
|
|
23
|
+import org.apache.commons.lang.StringUtils;
|
16
|
import org.springframework.beans.factory.annotation.Autowired;
|
24
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
25
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
26
|
+import org.springframework.http.ResponseEntity;
|
17
|
import org.springframework.web.bind.annotation.PostMapping;
|
27
|
import org.springframework.web.bind.annotation.PostMapping;
|
18
|
import org.springframework.web.bind.annotation.RequestMapping;
|
28
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
29
|
+import org.springframework.web.bind.annotation.RequestParam;
|
19
|
import org.springframework.web.bind.annotation.RestController;
|
30
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
31
|
+import org.springframework.web.client.RestTemplate;
|
20
|
|
32
|
|
21
|
import javax.imageio.ImageIO;
|
33
|
import javax.imageio.ImageIO;
|
22
|
|
34
|
|
|
@@ -34,11 +46,35 @@ public class AnonymousController { |
|
@@ -34,11 +46,35 @@ public class AnonymousController { |
34
|
RoleService roleService;
|
46
|
RoleService roleService;
|
35
|
|
47
|
|
36
|
@Autowired
|
48
|
@Autowired
|
|
|
49
|
+ private PermissionService permissionService;
|
|
|
50
|
+
|
|
|
51
|
+ @Autowired
|
37
|
RedisUtils redisUtils;
|
52
|
RedisUtils redisUtils;
|
38
|
|
53
|
|
39
|
@Autowired
|
54
|
@Autowired
|
40
|
private DefaultKaptcha captchaProducer;
|
55
|
private DefaultKaptcha captchaProducer;
|
41
|
|
56
|
|
|
|
57
|
+ @Autowired
|
|
|
58
|
+ RestTemplate restTemplate;
|
|
|
59
|
+
|
|
|
60
|
+ @Value("${sso.url}")
|
|
|
61
|
+ private String SSOUrl;
|
|
|
62
|
+
|
|
|
63
|
+ @Value("${jwt.max-alive}")
|
|
|
64
|
+ protected Integer jwtMaxAlive;
|
|
|
65
|
+
|
|
|
66
|
+ @Autowired
|
|
|
67
|
+ UserService userService;
|
|
|
68
|
+
|
|
|
69
|
+ /**
|
|
|
70
|
+ * SSO验证服务票据响应属性名
|
|
|
71
|
+ */
|
|
|
72
|
+ private static final String SERVICE_RESPONESE = "serviceResponse";
|
|
|
73
|
+ private static final String AUTHENTICATION_SUCCESS = "authenticationSuccess";
|
|
|
74
|
+ private static final String LOGIN_NAME = "LOGIN_NAME";
|
|
|
75
|
+ private static final String ATTRIBUTES = "attributes";
|
|
|
76
|
+ private static final String USER_ID = "USER_ID";
|
|
|
77
|
+
|
42
|
/**
|
78
|
/**
|
43
|
* 配置匿名者可以访问的路由,并更新到redis,匿名者默认可以访问的role_name =ROLE_anonymous
|
79
|
* 配置匿名者可以访问的路由,并更新到redis,匿名者默认可以访问的role_name =ROLE_anonymous
|
44
|
* 此方法会将所有符合权限组名=ROLE_anonymous的权限更新到redis中,供gateway调用判断权限
|
80
|
* 此方法会将所有符合权限组名=ROLE_anonymous的权限更新到redis中,供gateway调用判断权限
|
|
@@ -85,4 +121,126 @@ public class AnonymousController { |
|
@@ -85,4 +121,126 @@ public class AnonymousController { |
85
|
return new ResultJson("200","verify get ok",map,verifyToken);
|
121
|
return new ResultJson("200","verify get ok",map,verifyToken);
|
86
|
|
122
|
|
87
|
}
|
123
|
}
|
|
|
124
|
+
|
|
|
125
|
+ @ApiOperation(value = "查询用户列表及信息", notes = "查询用户列表及单个用户信息")
|
|
|
126
|
+ @RequestMapping("ssoTicket")
|
|
|
127
|
+ public ResultJson ssoLogin(@RequestParam("ticket") String ticket,
|
|
|
128
|
+ @RequestParam("myWebLoginUrl") String myWebLoginUrl
|
|
|
129
|
+ ){
|
|
|
130
|
+ try {
|
|
|
131
|
+ log.info("[SSO-AUTH-TICKET]-开始单点登录票据认证-[{}]",ticket);
|
|
|
132
|
+ // 构建接口地址
|
|
|
133
|
+ String url = SSOUrl+"?format=json&service="
|
|
|
134
|
+ + myWebLoginUrl
|
|
|
135
|
+ + "&ticket=" + ticket;
|
|
|
136
|
+
|
|
|
137
|
+ // 使用RestTemplate调用接口
|
|
|
138
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
139
|
+ /**
|
|
|
140
|
+ * 单点登录认证返回实体类
|
|
|
141
|
+ * {
|
|
|
142
|
+ * "serviceResponse" : {
|
|
|
143
|
+ * "authenticationSuccess" : {
|
|
|
144
|
+ * "user" : "zhangyf",
|
|
|
145
|
+ * "attributes" : {
|
|
|
146
|
+ * "isFromNewLogin" : [ false ],
|
|
|
147
|
+ * "authenticationDate" : [ 1.614564403617E9 ],
|
|
|
148
|
+ * "successfulAuthenticationHandlers" : [ "pwd" ],
|
|
|
149
|
+ * "USER_ID" : "8a0162a628aa4049a7840d75378f1a91",
|
|
|
150
|
+ * "USER_NAME" : "张炎锋",
|
|
|
151
|
+ * "extend" : [ ],
|
|
|
152
|
+ * "credentialType" : "UsernamePasswordCredential",
|
|
|
153
|
+ * "samlAuthenticationStatementAuthMethod" : "urn:oasis:names:tc:SAML:1.0:am:password",
|
|
|
154
|
+ * "ipTerritory" : "",
|
|
|
155
|
+ * "authenticationMethod" : "pwd",
|
|
|
156
|
+ * "equipType" : "pc",
|
|
|
157
|
+ * "clientIp" : "172.19.0.17",
|
|
|
158
|
+ * "isDefaultPwd" : "false",
|
|
|
159
|
+ * "longTermAuthenticationRequestTokenUsed" : [ false ],
|
|
|
160
|
+ * "LOGIN_NAME" : "zhangyf",
|
|
|
161
|
+ * "MOBILE" : "18739902467"
|
|
|
162
|
+ * }
|
|
|
163
|
+ * }
|
|
|
164
|
+ * }
|
|
|
165
|
+ * },
|
|
|
166
|
+ */
|
|
|
167
|
+ ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
|
|
|
168
|
+ String responseBody = responseEntity.getBody();
|
|
|
169
|
+
|
|
|
170
|
+ try {
|
|
|
171
|
+ JSONObject jsonObject = JSONObject.parseObject(responseBody);
|
|
|
172
|
+ USERS user = parseSSOObject(jsonObject);
|
|
|
173
|
+ if (StringUtils.isNotEmpty(user.getUsername())){
|
|
|
174
|
+ USERS loginUser = userService.loadByUsername(user.getUsername());
|
|
|
175
|
+ if (loginUser!=null && loginUser.getUserId()>-1){
|
|
|
176
|
+ log.info("[SSO-AUTH-TICKET]-从认证中心获取到用户[{}]信息,开始设置系统登录认证token",user.getUsername());
|
|
|
177
|
+ user.setUserId(loginUser.getUserId());
|
|
|
178
|
+ user.setUsername(loginUser.getUsername());
|
|
|
179
|
+ user.setUserface(loginUser.getUserface());
|
|
|
180
|
+ user.setUserId(loginUser.getUserId());
|
|
|
181
|
+ user.setRealname(loginUser.getRealname());
|
|
|
182
|
+ user.setCompanyId(loginUser.getCompanyId());
|
|
|
183
|
+ user.setCompanyName(loginUser.getCompanyName());
|
|
|
184
|
+ user.setCompanyInfo(loginUser.getCompanyInfo());
|
|
|
185
|
+ user.setUserStatus(loginUser.getUserStatus());
|
|
|
186
|
+ user.setState(loginUser.getState());
|
|
|
187
|
+ }else {
|
|
|
188
|
+ user.setPassword("sso");
|
|
|
189
|
+ user.setUserStatus(2);
|
|
|
190
|
+ user.setState(true);
|
|
|
191
|
+ userService.insertSelective(user);
|
|
|
192
|
+ }
|
|
|
193
|
+ //设置用户的TOKEN的有效时间,时间配置在配置文件中设置
|
|
|
194
|
+ int expirationSeconds = 3600*24*7;
|
|
|
195
|
+ String jwtToken = JwtTokenUtil.generateToken(user.getUsername(), jwtMaxAlive);
|
|
|
196
|
+ user.setToken(jwtToken);
|
|
|
197
|
+ //这里将登录成功的[user]对象数据写入redis缓存,KEY为token value为user的JSON对象
|
|
|
198
|
+ String json = JSON.toJSONString(loginUser);
|
|
|
199
|
+ redisUtils.set(jwtToken, json,expirationSeconds);
|
|
|
200
|
+ redisUtils.set(Token.USER_TOKEN_KEY + user.getUsername(),jwtToken,expirationSeconds);
|
|
|
201
|
+ Map<String,Object> menuMap = permissionService.getUserMenus(user.getUserId());
|
|
|
202
|
+ return new ResultJson("200","单点登录认证成功",user);
|
|
|
203
|
+ }
|
|
|
204
|
+ } catch (JSONException e) {
|
|
|
205
|
+ e.printStackTrace();
|
|
|
206
|
+ log.error("[SSO-AUTH-TICKET-ERR]-单点登录票据解析异常",e);
|
|
|
207
|
+ return new ResultJson("400","单点登录票据解析异常",e.getMessage());
|
|
|
208
|
+ }
|
|
|
209
|
+ }catch (Exception e){
|
|
|
210
|
+ e.printStackTrace();
|
|
|
211
|
+ log.error("[SSO-AUTH-TICKET-ERR]-单点登录票据认证异常",e);
|
|
|
212
|
+ }
|
|
|
213
|
+ return new ResultJson("401","单点登录票据认证失败");
|
|
|
214
|
+ }
|
|
|
215
|
+
|
|
|
216
|
+ /**
|
|
|
217
|
+ * 解析单点认证返回的信息
|
|
|
218
|
+ * @param ssoResp 返回实体类
|
|
|
219
|
+ * @return 用户类
|
|
|
220
|
+ */
|
|
|
221
|
+ private USERS parseSSOObject(JSONObject ssoResp){
|
|
|
222
|
+ USERS user = new USERS();
|
|
|
223
|
+ // 根节点
|
|
|
224
|
+ if (ssoResp.containsKey(SERVICE_RESPONESE)){
|
|
|
225
|
+ JSONObject root = ssoResp.getJSONObject(SERVICE_RESPONESE);
|
|
|
226
|
+
|
|
|
227
|
+ //成功节点
|
|
|
228
|
+ if (root.containsKey(AUTHENTICATION_SUCCESS)){
|
|
|
229
|
+ JSONObject auth = root.getJSONObject(AUTHENTICATION_SUCCESS);
|
|
|
230
|
+ //用户名获取
|
|
|
231
|
+ String userName = auth.getString("user");
|
|
|
232
|
+
|
|
|
233
|
+ //用户其他属性
|
|
|
234
|
+ JSONObject attributes = auth.getJSONObject(ATTRIBUTES);
|
|
|
235
|
+ String loginName = attributes.getString(LOGIN_NAME);
|
|
|
236
|
+ String userId = attributes.getString(USER_ID);
|
|
|
237
|
+ String realName = attributes.getString("USER_NAME");
|
|
|
238
|
+ log.info("[SSO-AUTH-TICKET-INFO]-用户:{}/{}",loginName,realName);
|
|
|
239
|
+
|
|
|
240
|
+ user.setUsername(loginName);
|
|
|
241
|
+// user.setUserId(userId);
|
|
|
242
|
+ }
|
|
|
243
|
+ }
|
|
|
244
|
+ return user;
|
|
|
245
|
+ }
|
88
|
} |
246
|
} |