|
@@ -1,9 +1,11 @@
|
|
|
package com.zskk.pacsonline.modules.system.controller;
|
|
package com.zskk.pacsonline.modules.system.controller;
|
|
|
|
|
|
|
|
|
|
+import com.zskk.pacsonline.component.aop.SystemLogHandler;
|
|
|
import com.zskk.pacsonline.component.response.RestResult;
|
|
import com.zskk.pacsonline.component.response.RestResult;
|
|
|
import com.zskk.pacsonline.modules.system.entity.SysUser;
|
|
import com.zskk.pacsonline.modules.system.entity.SysUser;
|
|
|
import com.zskk.pacsonline.modules.system.request.LoginBody;
|
|
import com.zskk.pacsonline.modules.system.request.LoginBody;
|
|
|
import com.zskk.pacsonline.modules.system.service.SysUserService;
|
|
import com.zskk.pacsonline.modules.system.service.SysUserService;
|
|
|
|
|
+import com.zskk.pacsonline.modules.system.service.SysSmsCodeService;
|
|
|
import com.zskk.pacsonline.utils.JwtUtil;
|
|
import com.zskk.pacsonline.utils.JwtUtil;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
@@ -14,6 +16,8 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
@@ -21,6 +25,8 @@ import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
+@RestController
|
|
|
|
|
+@RequestMapping("/auth")
|
|
|
public class SysUserController {
|
|
public class SysUserController {
|
|
|
@Resource
|
|
@Resource
|
|
|
private AuthenticationManager authenticationManager;
|
|
private AuthenticationManager authenticationManager;
|
|
@@ -37,44 +43,64 @@ public class SysUserController {
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private RedisTemplate<String, String> redisTemplate;
|
|
private RedisTemplate<String, String> redisTemplate;
|
|
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private SysSmsCodeService sysSmsCodeService;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 用户名密码登录
|
|
* 用户名密码登录
|
|
|
* @param loginBody 登录信息
|
|
* @param loginBody 登录信息
|
|
|
* @return 登录结果
|
|
* @return 登录结果
|
|
|
*/
|
|
*/
|
|
|
|
|
+ @SystemLogHandler("用户登录|登录")
|
|
|
@PostMapping("/login")
|
|
@PostMapping("/login")
|
|
|
public RestResult<?> login(@RequestBody LoginBody loginBody, HttpServletRequest request) {
|
|
public RestResult<?> login(@RequestBody LoginBody loginBody, HttpServletRequest request) {
|
|
|
- // 验证用户名和密码
|
|
|
|
|
- Authentication authentication = authenticationManager.authenticate(
|
|
|
|
|
- new UsernamePasswordAuthenticationToken(loginBody.getUsername(), loginBody.getPassword())
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- // 设置认证信息
|
|
|
|
|
- SecurityContextHolder.getContext().setAuthentication(authentication);
|
|
|
|
|
-
|
|
|
|
|
- // 获取用户信息
|
|
|
|
|
- SysUser user = sysUserService.getUserByUsername(loginBody.getUsername());
|
|
|
|
|
-
|
|
|
|
|
- // 生成token
|
|
|
|
|
- Map<String, Object> claims = new HashMap<>();
|
|
|
|
|
- claims.put("username", user.getUsername());
|
|
|
|
|
- claims.put("userId", user.getId());
|
|
|
|
|
- String token = jwtUtils.generateToken(claims);
|
|
|
|
|
-
|
|
|
|
|
- // 将token存储到redis
|
|
|
|
|
- redisTemplate.opsForValue().set("token:" + user.getId(), token, 24, TimeUnit.HOURS);
|
|
|
|
|
-
|
|
|
|
|
- // 返回结果
|
|
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
|
|
- result.put("token", token);
|
|
|
|
|
- result.put("userInfo", user);
|
|
|
|
|
- return RestResult.ok("succes",result);
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ System.out.println("=== 开始登录流程 ===");
|
|
|
|
|
+ System.out.println("用户名: " + loginBody.getUsername());
|
|
|
|
|
+ System.out.println("密码长度: " + (loginBody.getPassword() != null ? loginBody.getPassword().length() : 0));
|
|
|
|
|
+
|
|
|
|
|
+ // 验证用户名和密码
|
|
|
|
|
+ System.out.println("1. 开始调用authenticationManager.authenticate()");
|
|
|
|
|
+ Authentication authentication = authenticationManager.authenticate(
|
|
|
|
|
+ new UsernamePasswordAuthenticationToken(loginBody.getUsername(), loginBody.getPassword())
|
|
|
|
|
+ );
|
|
|
|
|
+ System.out.println("2. 认证成功!");
|
|
|
|
|
+
|
|
|
|
|
+ // 设置认证信息
|
|
|
|
|
+ SecurityContextHolder.getContext().setAuthentication(authentication);
|
|
|
|
|
+
|
|
|
|
|
+ // 获取用户信息
|
|
|
|
|
+ SysUser user = sysUserService.getUserByUsername(loginBody.getUsername());
|
|
|
|
|
+
|
|
|
|
|
+ // 生成token
|
|
|
|
|
+ Map<String, Object> claims = new HashMap<>();
|
|
|
|
|
+ claims.put("username", user.getUsername());
|
|
|
|
|
+ claims.put("userId", user.getId());
|
|
|
|
|
+ String token = jwtUtils.generateToken(claims);
|
|
|
|
|
+
|
|
|
|
|
+ // 将token存储到redis
|
|
|
|
|
+ redisTemplate.opsForValue().set("token:" + user.getId(), token, 24, TimeUnit.HOURS);
|
|
|
|
|
+
|
|
|
|
|
+ // 返回结果
|
|
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
|
|
+ result.put("token", token);
|
|
|
|
|
+ result.put("userInfo", user);
|
|
|
|
|
+ System.out.println("=== 登录成功 ===");
|
|
|
|
|
+ return RestResult.ok("登录成功", result);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ System.err.println("=== 登录失败 ===");
|
|
|
|
|
+ System.err.println("异常类型: " + e.getClass().getName());
|
|
|
|
|
+ System.err.println("异常消息: " + e.getMessage());
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ throw e; // 重新抛出,让全局异常处理器处理
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 退出登录
|
|
* 退出登录
|
|
|
* @return 退出结果
|
|
* @return 退出结果
|
|
|
*/
|
|
*/
|
|
|
|
|
+ @SystemLogHandler("用户登出|登出")
|
|
|
@PostMapping("/logout")
|
|
@PostMapping("/logout")
|
|
|
public RestResult<?> logout() {
|
|
public RestResult<?> logout() {
|
|
|
// 获取当前用户
|
|
// 获取当前用户
|
|
@@ -93,82 +119,95 @@ public class SysUserController {
|
|
|
return RestResult.ok("退出成功");
|
|
return RestResult.ok("退出成功");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- ///**
|
|
|
|
|
- // * 手机号验证码登录
|
|
|
|
|
- // * @param loginBody 登录信息
|
|
|
|
|
- // * @return 登录结果
|
|
|
|
|
- // */
|
|
|
|
|
- //@PostMapping("/loginByPhone")
|
|
|
|
|
- //public RestResult<?> loginByPhone(@RequestBody LoginBody loginBody) {
|
|
|
|
|
- // // 验证手机号
|
|
|
|
|
- // if (!StringUtils.isPhone(loginBody.getPhone())) {
|
|
|
|
|
- // return ResponseResult.fail(ResultCode.PARAM_ERROR);
|
|
|
|
|
- // }
|
|
|
|
|
- //
|
|
|
|
|
- // // 验证验证码
|
|
|
|
|
- // String code = redisTemplate.opsForValue().get("sms:code:" + loginBody.getPhone());
|
|
|
|
|
- // if (code == null || !code.equals(loginBody.getCode())) {
|
|
|
|
|
- // return ResponseResult.fail(ResultCode.CAPTCHA_ERROR);
|
|
|
|
|
- // }
|
|
|
|
|
- //
|
|
|
|
|
- // // 获取用户信息
|
|
|
|
|
- // SysUser user = sysUserService.getUserByPhone(loginBody.getPhone());
|
|
|
|
|
- // if (user == null) {
|
|
|
|
|
- // return ResponseResult.fail(ResultCode.USER_NOT_EXIST);
|
|
|
|
|
- // }
|
|
|
|
|
- //
|
|
|
|
|
- // // 检查用户状态
|
|
|
|
|
- // if (user.getStatus() == 0) {
|
|
|
|
|
- // return ResponseResult.fail(ResultCode.FORBIDDEN);
|
|
|
|
|
- // }
|
|
|
|
|
- //
|
|
|
|
|
- // // 生成token
|
|
|
|
|
- // Map<String, Object> claims = new HashMap<>();
|
|
|
|
|
- // claims.put("username", user.getUsername());
|
|
|
|
|
- // claims.put("userId", user.getUserId());
|
|
|
|
|
- // String token = jwtUtils.generateToken(user.getUserId().toString(), claims);
|
|
|
|
|
- //
|
|
|
|
|
- // // 将token存储到redis
|
|
|
|
|
- // redisTemplate.opsForValue().set("token:" + user.getUserId(), token, 24, TimeUnit.HOURS);
|
|
|
|
|
- //
|
|
|
|
|
- // // 删除验证码
|
|
|
|
|
- // redisTemplate.delete("sms:code:" + loginBody.getPhone());
|
|
|
|
|
- //
|
|
|
|
|
- // // 返回结果
|
|
|
|
|
- // Map<String, Object> result = new HashMap<>();
|
|
|
|
|
- // result.put("token", token);
|
|
|
|
|
- // result.put("userInfo", user);
|
|
|
|
|
- // return ResponseResult.success(result);
|
|
|
|
|
- //}
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 生成BCrypt密码
|
|
|
|
|
+ * 仅用于开发调试,生产环境应删除
|
|
|
|
|
+ */
|
|
|
|
|
+ @PostMapping("/generate-password")
|
|
|
|
|
+ public RestResult<?> generatePassword(String password) {
|
|
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
|
|
+ try {
|
|
|
|
|
+ String encodedPassword = passwordEncoder.encode(password);
|
|
|
|
|
+
|
|
|
|
|
+ result.put("rawPassword", password);
|
|
|
|
|
+ result.put("encodedPassword", encodedPassword);
|
|
|
|
|
+ result.put("updateSqlForAdmin", "UPDATE sys_user SET password = '" + encodedPassword + "' WHERE username = 'admin';");
|
|
|
|
|
+ result.put("updateSqlForUser", "UPDATE sys_user SET password = '" + encodedPassword + "' WHERE username = 'user';");
|
|
|
|
|
+
|
|
|
|
|
+ return RestResult.ok("密码生成成功", result);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ return RestResult.error("生成失败: " + e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 手机号验证码登录
|
|
|
|
|
+ * @param loginBody 登录信息
|
|
|
|
|
+ * @return 登录结果
|
|
|
|
|
+ */
|
|
|
|
|
+ @SystemLogHandler("手机号验证码登录|登录")
|
|
|
|
|
+ @PostMapping("/loginByPhone")
|
|
|
|
|
+ public RestResult<?> loginByPhone(@RequestBody LoginBody loginBody) {
|
|
|
|
|
+ String phone = loginBody.getPhone();
|
|
|
|
|
+ String code = loginBody.getCode();
|
|
|
|
|
+ if (!isPhone(phone)) {
|
|
|
|
|
+ return RestResult.error("手机号格式不正确");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ boolean ok = sysSmsCodeService.validateAndConsumeCode(phone, code);
|
|
|
|
|
+ if (!ok) {
|
|
|
|
|
+ return RestResult.error("验证码错误或已过期");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ SysUser user = sysUserService.getUserByPhone(phone);
|
|
|
|
|
+ if (user == null) {
|
|
|
|
|
+ return RestResult.error("用户不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (user.getStatus() != null && user.getStatus() == 0) {
|
|
|
|
|
+ return RestResult.error("用户已被禁用");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> claims = new HashMap<>();
|
|
|
|
|
+ claims.put("username", user.getUsername());
|
|
|
|
|
+ claims.put("userId", user.getId());
|
|
|
|
|
+ String token = jwtUtils.generateToken(claims);
|
|
|
|
|
+
|
|
|
|
|
+ redisTemplate.opsForValue().set("token:" + user.getId(), token, 24, TimeUnit.HOURS);
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
|
|
+ result.put("token", token);
|
|
|
|
|
+ result.put("userInfo", user);
|
|
|
|
|
+ return RestResult.ok("登录成功", result);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
///**
|
|
///**
|
|
|
// * 发送验证码
|
|
// * 发送验证码
|
|
|
// * @param phone 手机号
|
|
// * @param phone 手机号
|
|
|
// * @return 发送结果
|
|
// * @return 发送结果
|
|
|
// */
|
|
// */
|
|
|
- //@PostMapping("/sendCode")
|
|
|
|
|
- //public ResponseResult<?> sendCode(String phone) {
|
|
|
|
|
- // // 验证手机号
|
|
|
|
|
- // if (!StringUtils.isPhone(phone)) {
|
|
|
|
|
- // return ResponseResult.fail(ResultCode.PARAM_ERROR);
|
|
|
|
|
- // }
|
|
|
|
|
- //
|
|
|
|
|
- // // 检查手机号是否存在
|
|
|
|
|
- // if (!sysUserService.checkPhoneExist(phone)) {
|
|
|
|
|
- // return ResponseResult.fail(ResultCode.PHONE_NOT_EXIST);
|
|
|
|
|
- // }
|
|
|
|
|
- //
|
|
|
|
|
- // // 生成验证码
|
|
|
|
|
- // String code = StringUtils.generateVerifyCode(6);
|
|
|
|
|
- //
|
|
|
|
|
- // // 存储验证码到redis,有效期5分钟
|
|
|
|
|
- // redisTemplate.opsForValue().set("sms:code:" + phone, code, 5, TimeUnit.MINUTES);
|
|
|
|
|
- //
|
|
|
|
|
- // // TODO: 调用短信发送服务发送验证码
|
|
|
|
|
- // System.out.println("发送验证码: " + code + " 到手机号: " + phone);
|
|
|
|
|
- //
|
|
|
|
|
- // return ResponseResult.success("验证码发送成功");
|
|
|
|
|
- //}
|
|
|
|
|
|
|
+ @PostMapping("/sendCode")
|
|
|
|
|
+ public RestResult<?> sendCode(String phone, HttpServletRequest request) {
|
|
|
|
|
+ if (!isPhone(phone)) {
|
|
|
|
|
+ return RestResult.error("手机号格式不正确");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!sysUserService.checkPhoneExist(phone)) {
|
|
|
|
|
+ return RestResult.error("手机号未注册");
|
|
|
|
|
+ }
|
|
|
|
|
+ String ip = request != null ? request.getRemoteAddr() : null;
|
|
|
|
|
+ sysSmsCodeService.generateAndSendCode(phone, ip);
|
|
|
|
|
+ return RestResult.ok("验证码发送成功");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean isPhone(String phone) {
|
|
|
|
|
+ if (phone == null) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ return phone.matches("^1[3-9]\\d{9}$");
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|