|
|
@@ -0,0 +1,176 @@
|
|
|
+package com.zskk.pacsonline.modules.system.controller;
|
|
|
+
|
|
|
+import com.zskk.pacsonline.component.response.RestResult;
|
|
|
+import com.zskk.pacsonline.modules.system.entity.SysUser;
|
|
|
+import com.zskk.pacsonline.modules.system.request.LoginBody;
|
|
|
+import com.zskk.pacsonline.modules.system.service.SysUserService;
|
|
|
+import com.zskk.pacsonline.utils.JwtUtil;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.security.authentication.AuthenticationManager;
|
|
|
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
+import org.springframework.security.core.Authentication;
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
+import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+public class SysUserController {
|
|
|
+ @Resource
|
|
|
+ private AuthenticationManager authenticationManager;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private JwtUtil jwtUtils;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private SysUserService sysUserService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private PasswordEncoder passwordEncoder;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate<String, String> redisTemplate;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用户名密码登录
|
|
|
+ * @param loginBody 登录信息
|
|
|
+ * @return 登录结果
|
|
|
+ */
|
|
|
+ @PostMapping("/login")
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 退出登录
|
|
|
+ * @return 退出结果
|
|
|
+ */
|
|
|
+ @PostMapping("/logout")
|
|
|
+ public RestResult<?> logout() {
|
|
|
+ // 获取当前用户
|
|
|
+ Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
|
+ if (authentication != null) {
|
|
|
+ // 删除redis中的token
|
|
|
+ String username = authentication.getName();
|
|
|
+ SysUser user = sysUserService.getUserByUsername(username);
|
|
|
+ if (user != null) {
|
|
|
+ redisTemplate.delete("token:" + user.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 清空认证信息
|
|
|
+ SecurityContextHolder.clearContext();
|
|
|
+ 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);
|
|
|
+ //}
|
|
|
+
|
|
|
+ ///**
|
|
|
+ // * 发送验证码
|
|
|
+ // * @param phone 手机号
|
|
|
+ // * @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("验证码发送成功");
|
|
|
+ //}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|