UserController.java
12 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package com.genersoft.iot.vmp.vmanager.user;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.genersoft.iot.vmp.conf.exception.ControllerException;
import com.genersoft.iot.vmp.conf.security.JwtUtils;
import com.genersoft.iot.vmp.conf.security.SecurityUtils;
import com.genersoft.iot.vmp.conf.security.dto.LoginUser;
import com.genersoft.iot.vmp.service.IRoleService;
import com.genersoft.iot.vmp.service.IUserService;
import com.genersoft.iot.vmp.storager.mapper.dto.Role;
import com.genersoft.iot.vmp.storager.mapper.dto.User;
import com.genersoft.iot.vmp.utils.DateUtil;
import com.genersoft.iot.vmp.utils.HttpClientUtil;
import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
import com.genersoft.iot.vmp.vmanager.bean.UsLogin;
import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.security.sasl.AuthenticationException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
@Tag(name = "用户管理")
@RestController
@RequestMapping("/api/user")
public class UserController {
@Resource
private AuthenticationManager authenticationManager;
@Resource
private IUserService userService;
@Resource
private IRoleService roleService;
@GetMapping("/login")
@Operation(summary = "登录", description = "登录成功后返回AccessToken, 可以从返回值获取到也可以从响应头中获取到," +
"后续的请求需要添加请求头 'access-token'或者放在参数里")
@Parameter(name = "username", description = "用户名", required = true)
@Parameter(name = "password", description = "密码(32位md5加密)", required = true)
public LoginUser login(HttpServletRequest request, HttpServletResponse response, @RequestParam String username, @RequestParam String password){
LoginUser user;
try {
user = SecurityUtils.login(username, password, authenticationManager);
} catch (AuthenticationException e) {
throw new ControllerException(ErrorCode.ERROR100.getCode(), e.getMessage());
}
if (user == null) {
throw new ControllerException(ErrorCode.ERROR100.getCode(), "用户名或密码错误");
}else {
String jwt = JwtUtils.createToken(username);
response.setHeader(JwtUtils.getHeader(), jwt);
user.setAccessToken(jwt);
}
return user;
}
@PostMapping("/getInfo")
public User getInfo(@RequestBody Map<String,String> map) {
String token = map.get("token");
if (token != null) {
String sysCode = "SYSUS004";
String url = "http://10.10.2.23:8112/prod-api/system/utilitySystem/checkToken";
// //根据自己的网络环境自行选择访问方式
// //外网ip http://118.113.164.50:8112
// /prod-api/system/utilitySystem/checkToken
// //宿主机ip 10.10.2.23:8112
// /prod-api/system/utilitySystem/checkToken
// //容器ip 172.17.0.8:8112
// /prod-api/system/utilitySystem/checkToken
UsLogin usLogin = new UsLogin();
usLogin.setToken(token);
usLogin.setSysCode(sysCode);
String dataJsonStr = HttpClientUtil.httpRequest(url, "POST", usLogin);
JSONObject jsonObject = JSON.parseObject(dataJsonStr);
JSONObject dataJson = jsonObject.getJSONObject("data");
String resCode = dataJson.getString("code");
/**
* 登陆校验失败
*/
if ("9999".equals(resCode) || "9998".equals(resCode)) {
throw new RuntimeException(jsonObject.getString("msgUser"));
}
/**
* 回调数据
*/
JSONObject resDataJson = dataJson.getJSONObject("data");
/**
* 用户名
*/
String username = resDataJson.getString("userName");
User user = userService.selectUserByUserName(username);
if (user == null){
throw new RuntimeException("用户不存在");
}
return user;
}else {
throw new RuntimeException("token无效");
}
}
@PostMapping("/changePassword")
@Operation(summary = "修改密码", security = @SecurityRequirement(name = JwtUtils.HEADER))
@Parameter(name = "username", description = "用户名", required = true)
@Parameter(name = "oldpassword", description = "旧密码(已md5加密的密码)", required = true)
@Parameter(name = "password", description = "新密码(未md5加密的密码)", required = true)
public void changePassword(@RequestParam String oldPassword, @RequestParam String password){
// 获取当前登录用户id
LoginUser userInfo = SecurityUtils.getUserInfo();
if (userInfo== null) {
throw new ControllerException(ErrorCode.ERROR100);
}
String username = userInfo.getUsername();
LoginUser user = null;
try {
user = SecurityUtils.login(username, oldPassword, authenticationManager);
if (user == null) {
throw new ControllerException(ErrorCode.ERROR100);
}
//int userId = SecurityUtils.getUserId();
boolean result = userService.changePassword(user.getId(), DigestUtils.md5DigestAsHex(password.getBytes()));
if (!result) {
throw new ControllerException(ErrorCode.ERROR100);
}
} catch (AuthenticationException e) {
throw new ControllerException(ErrorCode.ERROR100.getCode(), e.getMessage());
}
}
@PostMapping("/add")
@Operation(summary = "添加用户", security = @SecurityRequirement(name = JwtUtils.HEADER))
@Parameter(name = "username", description = "用户名", required = true)
@Parameter(name = "password", description = "密码(未md5加密的密码)", required = true)
@Parameter(name = "roleId", description = "角色ID", required = true)
public void add(@RequestParam String username,
@RequestParam String password,
@RequestParam Integer roleId){
if (ObjectUtils.isEmpty(username) || ObjectUtils.isEmpty(password) || roleId == null) {
throw new ControllerException(ErrorCode.ERROR400.getCode(), "参数不可为空");
}
// 获取当前登录用户id
int currenRoleId = SecurityUtils.getUserInfo().getRole().getId();
if (currenRoleId != 1) {
// 只用角色id为1才可以删除和添加用户
throw new ControllerException(ErrorCode.ERROR400.getCode(), "用户无权限");
}
User user = new User();
user.setUsername(username);
user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
//新增用户的pushKey的生成规则为md5(时间戳+用户名)
user.setPushKey(DigestUtils.md5DigestAsHex((System.currentTimeMillis()+password).getBytes()));
Role role = roleService.getRoleById(roleId);
if (role == null) {
throw new ControllerException(ErrorCode.ERROR400.getCode(), "角色不存在");
}
user.setRole(role);
user.setCreateTime(DateUtil.getNow());
user.setUpdateTime(DateUtil.getNow());
int addResult = userService.addUser(user);
if (addResult <= 0) {
throw new ControllerException(ErrorCode.ERROR100);
}
}
@DeleteMapping("/delete")
@Operation(summary = "删除用户", security = @SecurityRequirement(name = JwtUtils.HEADER))
@Parameter(name = "id", description = "用户Id", required = true)
public void delete(@RequestParam Integer id){
// 获取当前登录用户id
int currenRoleId = SecurityUtils.getUserInfo().getRole().getId();
if (currenRoleId != 1) {
// 只用角色id为0才可以删除和添加用户
throw new ControllerException(ErrorCode.ERROR400.getCode(), "用户无权限");
}
int deleteResult = userService.deleteUser(id);
if (deleteResult <= 0) {
throw new ControllerException(ErrorCode.ERROR100);
}
}
@GetMapping("/all")
@Operation(summary = "查询用户", security = @SecurityRequirement(name = JwtUtils.HEADER))
public List<User> all(){
// 获取当前登录用户id
return userService.getAllUsers();
}
/**
* 分页查询用户
*
* @param page 当前页
* @param count 每页查询数量
* @return 分页用户列表
*/
@GetMapping("/users")
@Operation(summary = "分页查询用户", security = @SecurityRequirement(name = JwtUtils.HEADER))
@Parameter(name = "page", description = "当前页", required = true)
@Parameter(name = "count", description = "每页查询数量", required = true)
public PageInfo<User> users(int page, int count) {
return userService.getUsers(page, count);
}
@RequestMapping("/changePushKey")
@Operation(summary = "修改pushkey", security = @SecurityRequirement(name = JwtUtils.HEADER))
@Parameter(name = "userId", description = "用户Id", required = true)
@Parameter(name = "pushKey", description = "新的pushKey", required = true)
public void changePushKey(@RequestParam Integer userId,@RequestParam String pushKey) {
// 获取当前登录用户id
int currenRoleId = SecurityUtils.getUserInfo().getRole().getId();
WVPResult<String> result = new WVPResult<>();
if (currenRoleId != 1) {
// 只用角色id为0才可以删除和添加用户
throw new ControllerException(ErrorCode.ERROR400.getCode(), "用户无权限");
}
int resetPushKeyResult = userService.changePushKey(userId,pushKey);
if (resetPushKeyResult <= 0) {
throw new ControllerException(ErrorCode.ERROR100);
}
}
@PostMapping("/changePasswordForAdmin")
@Operation(summary = "管理员修改普通用户密码", security = @SecurityRequirement(name = JwtUtils.HEADER))
@Parameter(name = "adminId", description = "管理员id", required = true)
@Parameter(name = "userId", description = "用户id", required = true)
@Parameter(name = "password", description = "新密码(未md5加密的密码)", required = true)
public void changePasswordForAdmin(@RequestParam int userId, @RequestParam String password) {
// 获取当前登录用户id
LoginUser userInfo = SecurityUtils.getUserInfo();
if (userInfo == null) {
throw new ControllerException(ErrorCode.ERROR100);
}
Role role = userInfo.getRole();
if (role != null && role.getId() == 1) {
boolean result = userService.changePassword(userId, DigestUtils.md5DigestAsHex(password.getBytes()));
if (!result) {
throw new ControllerException(ErrorCode.ERROR100);
}
}
}
@PostMapping("/userInfo")
@Operation(summary = "管理员修改普通用户密码")
public LoginUser getUserInfo() {
// 获取当前登录用户id
LoginUser userInfo = SecurityUtils.getUserInfo();
if (userInfo == null) {
throw new ControllerException(ErrorCode.ERROR100);
}
User user = userService.getUser(userInfo.getUsername(), userInfo.getPassword());
return new LoginUser(user, LocalDateTime.now());
}
}