|
|
@@ -1,54 +1,90 @@
|
|
|
-//package com.njuzr.eaibackend.controller;
|
|
|
-//
|
|
|
-//import com.njuzr.eaibackend.vo.UserLoginVO;
|
|
|
-//import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-//import org.springframework.http.HttpStatus;
|
|
|
-//import org.springframework.security.authentication.AuthenticationManager;
|
|
|
-//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
-//import org.springframework.security.core.Authentication;
|
|
|
-//import org.springframework.security.core.AuthenticationException;
|
|
|
-//import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
-//import org.springframework.web.bind.annotation.PostMapping;
|
|
|
-//import org.springframework.web.bind.annotation.RequestBody;
|
|
|
-//import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-//import org.springframework.web.bind.annotation.RestController;
|
|
|
-//
|
|
|
-///**
|
|
|
-// * @author: Leonezhurui
|
|
|
-// * @Date: 2024/2/20 - 00:10
|
|
|
-// * @Package: EAI-Backend
|
|
|
-// */
|
|
|
-//
|
|
|
-//@RestController
|
|
|
-//@RequestMapping("/api/login")
|
|
|
-//public class AuthenticationController {
|
|
|
-//
|
|
|
-// private final AuthenticationManager authenticationManager;
|
|
|
-//
|
|
|
-// @Autowired
|
|
|
-// public AuthenticationController(AuthenticationManager authenticationManager) {
|
|
|
-// this.authenticationManager = authenticationManager;
|
|
|
-// }
|
|
|
-//
|
|
|
-// @PostMapping("/login")
|
|
|
-// public MyResponse login(@RequestBody UserLoginVO userLoginVO) {
|
|
|
-// try {
|
|
|
-// Authentication authentication = authenticationManager.authenticate(
|
|
|
-// new UsernamePasswordAuthenticationToken(
|
|
|
-// userLoginVO.getOfficialEmail(),
|
|
|
-// userLoginVO.getPassword()
|
|
|
-// )
|
|
|
-// );
|
|
|
-//
|
|
|
-// SecurityContextHolder.getContext().setAuthentication(authentication);
|
|
|
-//
|
|
|
-// // TODO 生成JWT
|
|
|
-// String jwt = jwtTokenUtil.generateToken(authentication);
|
|
|
-// return MyResponse.success({});
|
|
|
-// } catch (AuthenticationException e) {
|
|
|
-// return MyResponse.error(HttpStatus.UNAUTHORIZED.value(), "Error: Authentication failed");
|
|
|
-// }
|
|
|
-//
|
|
|
-// }
|
|
|
-//
|
|
|
-//}
|
|
|
+package com.njuzr.eaibackend.controller;
|
|
|
+
|
|
|
+import com.njuzr.eaibackend.dto.UserLoginDTO;
|
|
|
+import com.njuzr.eaibackend.dto.UserRegisterDTO;
|
|
|
+import com.njuzr.eaibackend.exception.MyException;
|
|
|
+import com.njuzr.eaibackend.po.MyUserDetails;
|
|
|
+import com.njuzr.eaibackend.service.UserService;
|
|
|
+import com.njuzr.eaibackend.utils.JWTTokenUtil;
|
|
|
+import com.njuzr.eaibackend.utils.ModelMapperUtil;
|
|
|
+import com.njuzr.eaibackend.vo.UserLoginVO;
|
|
|
+import com.njuzr.eaibackend.vo.UserVO;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.security.authentication.AuthenticationManager;
|
|
|
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
+import org.springframework.security.core.Authentication;
|
|
|
+import org.springframework.security.core.AuthenticationException;
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author: Leonezhurui
|
|
|
+ * @Date: 2024/2/20 - 00:10
|
|
|
+ * @Package: EAI-Backend
|
|
|
+ */
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@RestController
|
|
|
+@RequestMapping("/api/user")
|
|
|
+public class AuthenticationController {
|
|
|
+
|
|
|
+ private final AuthenticationManager authenticationManager;
|
|
|
+ private final UserService userService;
|
|
|
+
|
|
|
+ private final JWTTokenUtil jwtTokenUtil;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public AuthenticationController(AuthenticationManager authenticationManager, UserService userService, JWTTokenUtil jwtTokenUtil) {
|
|
|
+ this.authenticationManager = authenticationManager;
|
|
|
+ this.userService = userService;
|
|
|
+ this.jwtTokenUtil = jwtTokenUtil;
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("/login")
|
|
|
+ public MyResponse login(@RequestBody UserLoginDTO userLoginDTO) {
|
|
|
+ try {
|
|
|
+ // 用户登陆学号和密码进行登陆
|
|
|
+ Authentication authentication = authenticationManager.authenticate(
|
|
|
+ new UsernamePasswordAuthenticationToken(
|
|
|
+ userLoginDTO.getOfficialNumber(),
|
|
|
+ userLoginDTO.getPassword()
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ SecurityContextHolder.getContext().setAuthentication(authentication);
|
|
|
+ MyUserDetails userDetails = (MyUserDetails) authentication.getPrincipal();
|
|
|
+
|
|
|
+ log.info("用户认证通过,用户认证信息如下:"+userDetails.toString());
|
|
|
+
|
|
|
+ String jwt = jwtTokenUtil.generateToken(userDetails.getOfficialNumber());
|
|
|
+ log.info("生成jwt成功,token信息:"+jwt);
|
|
|
+
|
|
|
+ UserLoginVO userLoginVO = ModelMapperUtil.map(userDetails, UserLoginVO.class);
|
|
|
+ userLoginVO.setToken(jwt);
|
|
|
+ return MyResponse.success(userLoginVO);
|
|
|
+ } catch (AuthenticationException e) {
|
|
|
+ return MyResponse.error(HttpStatus.UNAUTHORIZED.value(), "Error: Authentication failed"+e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @PostMapping("/register")
|
|
|
+ public MyResponse createUser(@RequestBody UserRegisterDTO userRegisterDTO) {
|
|
|
+ log.info("createUser - 用户数据:"+userRegisterDTO.toString()); // 解析前端传递的数据
|
|
|
+
|
|
|
+ try {
|
|
|
+ UserVO userVO = userService.createUser(userRegisterDTO);
|
|
|
+ return MyResponse.success(userVO);
|
|
|
+ } catch (MyException e) {
|
|
|
+ return MyResponse.error(501, e.getMessage());
|
|
|
+ } catch (Exception e) {
|
|
|
+ return MyResponse.error(501, "创建用户失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|