|
|
@@ -106,14 +106,17 @@ public class AuthenticationServiceImpl implements AuthenticationService {
|
|
|
public MyResponse loginByPortal(String token) {
|
|
|
// 从门户返回的jwt中解析用户信息
|
|
|
Map<String, Object> userInfoMap = jwtTokenUtil.parseClaim(token).getBody().get("user_info", Map.class);
|
|
|
- log.info("解析出的用户信息:{}", userInfoMap.toString());
|
|
|
+ if (userInfoMap == null) {
|
|
|
+ throw new MyException(HttpStatus.BAD_REQUEST.value(), "门户token缺少user_info");
|
|
|
+ }
|
|
|
+ log.info("解析出的用户信息:{}", userInfoMap);
|
|
|
//pid:门户用户id
|
|
|
- Integer pid = Integer.valueOf(userInfoMap.get("id").toString());
|
|
|
- String username = userInfoMap.get("username").toString();
|
|
|
- String name = userInfoMap.get("name").toString();
|
|
|
- String email = userInfoMap.get("email").toString();
|
|
|
- String phone = userInfoMap.get("phone").toString();
|
|
|
- Role role = Role.valueOf(userInfoMap.get("role").toString());
|
|
|
+ Integer pid = parseRequiredInt(userInfoMap, "id");
|
|
|
+ String username = parseRequiredString(userInfoMap, "username");
|
|
|
+ String name = parseRequiredString(userInfoMap, "name");
|
|
|
+ String email = parseOptionalString(userInfoMap, "email");
|
|
|
+ String phone = parseRequiredString(userInfoMap, "phone");
|
|
|
+ Role role = parseRequiredRole(userInfoMap, "role");
|
|
|
// 如果EAI数据库中没有该用户,则添加该用户
|
|
|
if (!userService.userExists(pid)) {
|
|
|
log.info("用户不存在,添加用户{}", phone);
|
|
|
@@ -130,6 +133,7 @@ public class AuthenticationServiceImpl implements AuthenticationService {
|
|
|
// 否则更新用户信息
|
|
|
UserUpdateDTO update = new UserUpdateDTO();
|
|
|
update.setContentEmail(email);
|
|
|
+ update.setPhone(phone);
|
|
|
update.setOfficialNumber(username);
|
|
|
update.setRole(role);
|
|
|
System.out.println(role);
|
|
|
@@ -140,5 +144,50 @@ public class AuthenticationServiceImpl implements AuthenticationService {
|
|
|
return MyResponse.success(userLoginVO);
|
|
|
}
|
|
|
|
|
|
+ private String parseRequiredString(Map<String, Object> map, String key) {
|
|
|
+ Object value = map.get(key);
|
|
|
+ if (value == null) {
|
|
|
+ throw new MyException(HttpStatus.BAD_REQUEST.value(), "门户token缺少字段: " + key);
|
|
|
+ }
|
|
|
+ String text = value.toString().trim();
|
|
|
+ if (text.isEmpty()) {
|
|
|
+ throw new MyException(HttpStatus.BAD_REQUEST.value(), "门户token字段为空: " + key);
|
|
|
+ }
|
|
|
+ return text;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String parseOptionalString(Map<String, Object> map, String key) {
|
|
|
+ Object value = map.get(key);
|
|
|
+ if (value == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String text = value.toString().trim();
|
|
|
+ return text.isEmpty() ? null : text;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Integer parseRequiredInt(Map<String, Object> map, String key) {
|
|
|
+ Object value = map.get(key);
|
|
|
+ if (value == null) {
|
|
|
+ throw new MyException(HttpStatus.BAD_REQUEST.value(), "门户token缺少字段: " + key);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return ((Number) value).intValue();
|
|
|
+ }
|
|
|
+ return Integer.valueOf(value.toString());
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ throw new MyException(HttpStatus.BAD_REQUEST.value(), "门户token字段格式错误: " + key);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Role parseRequiredRole(Map<String, Object> map, String key) {
|
|
|
+ String role = parseRequiredString(map, key);
|
|
|
+ try {
|
|
|
+ return Role.valueOf(role);
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ throw new MyException(HttpStatus.BAD_REQUEST.value(), "门户token角色非法: " + role);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
}
|