|
|
@@ -3,11 +3,15 @@ package com.njuzr.eaibackend.service.impl;
|
|
|
import com.njuzr.eaibackend.controller.MyResponse;
|
|
|
import com.njuzr.eaibackend.dto.auth.EmailLoginDTO;
|
|
|
import com.njuzr.eaibackend.dto.auth.UserLoginDTO;
|
|
|
+import com.njuzr.eaibackend.dto.user.UserRegisterDTO;
|
|
|
+import com.njuzr.eaibackend.dto.user.UserUpdateDTO;
|
|
|
+import com.njuzr.eaibackend.enums.Role;
|
|
|
import com.njuzr.eaibackend.exception.MyException;
|
|
|
import com.njuzr.eaibackend.mapper.UserMapper;
|
|
|
import com.njuzr.eaibackend.po.MyUserDetails;
|
|
|
import com.njuzr.eaibackend.po.User;
|
|
|
import com.njuzr.eaibackend.service.AuthenticationService;
|
|
|
+import com.njuzr.eaibackend.service.UserService;
|
|
|
import com.njuzr.eaibackend.utils.JWTTokenUtil;
|
|
|
import com.njuzr.eaibackend.utils.ModelMapperUtil;
|
|
|
import com.njuzr.eaibackend.vo.auth.UserLoginVO;
|
|
|
@@ -23,6 +27,8 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
@@ -36,6 +42,8 @@ public class AuthenticationServiceImpl implements AuthenticationService {
|
|
|
private UserMapper userMapper;
|
|
|
@Autowired
|
|
|
private RedisTemplate<String, Object> redisTemplate;
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
|
|
|
|
|
|
@Override
|
|
|
@@ -62,10 +70,10 @@ public class AuthenticationServiceImpl implements AuthenticationService {
|
|
|
|
|
|
} catch (BadCredentialsException e) {
|
|
|
log.error("登陆失败,密码错误");
|
|
|
- return MyResponse.error(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase()+":"+"用户名或密码错误");
|
|
|
+ return MyResponse.error(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase() + ":" + "用户名或密码错误");
|
|
|
} catch (UsernameNotFoundException e) {
|
|
|
log.error("登陆失败,找不到该用户");
|
|
|
- return MyResponse.error(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase()+":"+"用户名或密码错误");
|
|
|
+ return MyResponse.error(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase() + ":" + "用户名或密码错误");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -78,14 +86,14 @@ public class AuthenticationServiceImpl implements AuthenticationService {
|
|
|
throw new MyException(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase() + ":" + "验证码已过期");
|
|
|
}
|
|
|
// 验证码校验
|
|
|
- if(!storedCode.equals(emailLoginDTO.getVerifyCode())) {
|
|
|
+ if (!storedCode.equals(emailLoginDTO.getVerifyCode())) {
|
|
|
throw new MyException(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase() + ":" + "验证码错误");
|
|
|
}
|
|
|
|
|
|
User user = userMapper.selectByOfficialEmail(emailLoginDTO.getOfficialEmail());
|
|
|
- if(user == null) {
|
|
|
+ if (user == null) {
|
|
|
log.error("登陆失败,找不到该用户");
|
|
|
- return MyResponse.error(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase()+":"+"邮箱未注册");
|
|
|
+ return MyResponse.error(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase() + ":" + "邮箱未注册");
|
|
|
}
|
|
|
String jwt = jwtTokenUtil.generateToken(user.getOfficialNumber());
|
|
|
log.info("生成jwt成功,token信息:" + jwt);
|
|
|
@@ -94,4 +102,40 @@ public class AuthenticationServiceImpl implements AuthenticationService {
|
|
|
return MyResponse.success(userLoginVO);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public MyResponse loginByPortal(String token) {
|
|
|
+ // 从门户返回的jwt中解析用户信息
|
|
|
+ Map<String, Object> userInfoMap = jwtTokenUtil.parseClaim(token).getBody().get("user_info", Map.class);
|
|
|
+ log.info("解析出的用户信息:{}", userInfoMap.toString());
|
|
|
+ //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());
|
|
|
+ // 如果EAI数据库中没有该用户,则添加该用户
|
|
|
+ if (!userService.userExists(pid)) {
|
|
|
+ log.info("用户不存在,添加用户{}", phone);
|
|
|
+ UserRegisterDTO reg = new UserRegisterDTO();
|
|
|
+ reg.setPid(pid);
|
|
|
+ reg.setOfficialEmail(email);
|
|
|
+ reg.setOfficialNumber(phone);
|
|
|
+ reg.setRole(role);
|
|
|
+ reg.setName(name);
|
|
|
+ reg.setPassword("no_password");
|
|
|
+ userService.createUser(reg);
|
|
|
+ } else {
|
|
|
+ // 否则更新用户信息
|
|
|
+ UserUpdateDTO update = new UserUpdateDTO();
|
|
|
+ update.setContentEmail(email);
|
|
|
+ update.setPhone(phone);
|
|
|
+ userService.updateUserByPid(pid, update);
|
|
|
+ }
|
|
|
+ UserLoginVO userLoginVO = ModelMapperUtil.map(userMapper.selectByOfficialNumber(phone), UserLoginVO.class);
|
|
|
+ userLoginVO.setToken(token);
|
|
|
+ return MyResponse.success(userLoginVO);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|