LoginController.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package com.fdkankan.ucenter.controller;
  2. import cn.hutool.captcha.CaptchaUtil;
  3. import cn.hutool.captcha.LineCaptcha;
  4. import cn.hutool.captcha.generator.MathGenerator;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.fdkankan.common.exception.BusinessException;
  7. import com.fdkankan.common.util.JwtUtil;
  8. import com.fdkankan.redis.util.RedisUtil;
  9. import com.fdkankan.ucenter.common.BaseController;
  10. import com.fdkankan.ucenter.common.RedisKeyUtil;
  11. import com.fdkankan.ucenter.common.Result;
  12. import com.fdkankan.ucenter.constant.LoginConstant;
  13. import com.fdkankan.ucenter.service.impl.LoginService;
  14. import com.fdkankan.ucenter.vo.request.LoginParam;
  15. import com.fdkankan.ucenter.vo.request.RegisterParam;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.beans.factory.annotation.Value;
  20. import org.springframework.web.bind.annotation.*;
  21. import javax.servlet.http.HttpServletRequest;
  22. import javax.servlet.http.HttpServletResponse;
  23. import java.io.IOException;
  24. /**
  25. * 登录
  26. */
  27. @RestController
  28. @RequestMapping("/ucenter/sso/user")
  29. @Slf4j
  30. public class LoginController extends BaseController {
  31. @Autowired
  32. LoginService loginService;
  33. @Autowired
  34. RedisUtil redisUtil;
  35. @Value("${spring.profiles.active}")
  36. private String environment;
  37. @Value("${admin.register.validCode:2a22bac40f44af4d3b5fdc20ea706fc5}")
  38. private String registerValidCode;
  39. /**
  40. * 登录
  41. * phoneNum 用户名
  42. * password 密码
  43. */
  44. @PostMapping("/login")
  45. public Result login(@RequestBody LoginParam param){
  46. if(StringUtils.isBlank(param.getAuthCode())){
  47. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  48. }
  49. String id = request.getSession().getId();
  50. String redisKey = String.format(RedisKeyUtil.loginAuthCode,id);
  51. String redisCode = redisUtil.get(redisKey);
  52. if(!redisUtil.hasKey(redisKey)){
  53. throw new BusinessException(LoginConstant.FAILURE_CODE_3006, LoginConstant.FAILURE_MSG_3006);
  54. }
  55. redisUtil.del(redisKey);
  56. MathGenerator mathGenerator = new MathGenerator(2);
  57. boolean verify = mathGenerator.verify(redisCode,param.getAuthCode());
  58. if(!verify){
  59. throw new BusinessException(LoginConstant.FAILURE_CODE_3006, LoginConstant.FAILURE_MSG_3006);
  60. }
  61. return Result.success(loginService.login(param));
  62. }
  63. /**
  64. * 登录
  65. * phoneNum 用户名
  66. * password 密码
  67. */
  68. @PostMapping("/loginClear")
  69. public Result loginClear(@RequestBody LoginParam param){
  70. return Result.success(loginService.loginClear(param));
  71. }
  72. /**
  73. * 相机扫码登录 获取二维码
  74. */
  75. @GetMapping("/createLoginQrCode")
  76. public Result createLoginQrCode() throws Exception {
  77. return Result.success(loginService.createLoginQrCode());
  78. }
  79. /**
  80. * 扫码验证登录
  81. */
  82. @PostMapping("/sendUserInfo")
  83. public Result sendUserInfo(@RequestBody JSONObject jsonObject){
  84. return Result.success(loginService.sendUserInfo(jsonObject.getString("uuid")));
  85. }
  86. /**
  87. * 登出
  88. */
  89. @PostMapping("/logout")
  90. public Result logout(){
  91. loginService.logout(getToken());
  92. return Result.success();
  93. }
  94. /**
  95. * 检测用户名
  96. * phoneNum 用户名
  97. */
  98. @PostMapping("/checkUser")
  99. public Result checkUser(@RequestBody LoginParam param){
  100. if(StringUtils.isBlank(param.getAuthCode())){
  101. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  102. }
  103. String id = request.getSession().getId();
  104. String redisKey = String.format(RedisKeyUtil.loginAuthCode,id);
  105. String redisCode = redisUtil.get(redisKey);
  106. if(!redisUtil.hasKey(redisKey)){
  107. throw new BusinessException(LoginConstant.FAILURE_CODE_3006, LoginConstant.FAILURE_MSG_3006);
  108. }
  109. redisUtil.del(redisKey);
  110. MathGenerator mathGenerator = new MathGenerator(2);
  111. boolean verify = mathGenerator.verify(redisCode,param.getAuthCode());
  112. if(!verify){
  113. throw new BusinessException(LoginConstant.FAILURE_CODE_3006, LoginConstant.FAILURE_MSG_3006);
  114. }
  115. // if(redisUtil.hasKey(String.format(RedisKeyUtil.checkUserCount,id))){
  116. // String countStr = redisUtil.get(String.format(RedisKeyUtil.checkUserCount, id));
  117. // if(Integer.valueOf(countStr) >=5){
  118. // throw new BusinessException(LoginConstant.FAILURE_CODE_3006, LoginConstant.FAILURE_MSG_3006);
  119. // }
  120. // }else {
  121. // redisUtil.set(String.format(RedisKeyUtil.checkUserCount,id),"1",60);
  122. // }
  123. loginService.checkUser(param.getPhoneNum(),true);
  124. return Result.success();
  125. }
  126. /**
  127. * 获取验证码
  128. * areaNum 区号
  129. * phoneNum 手机号码
  130. */
  131. @PostMapping("/getMsgAuthCode")
  132. public Result getMsgAuthCode(@RequestBody LoginParam param) throws Exception {
  133. loginService.getMsgAuthCode(param.getAreaNum(),param.getPhoneNum());
  134. return Result.success();
  135. }
  136. /**
  137. * 发送邮件验证码
  138. */
  139. @PostMapping("/getEmailAuthCode")
  140. public Result getEmailAuthCode(@RequestBody RegisterParam param) throws Exception {
  141. loginService.getEmailAuthCode(param.getEmail(),param.getCountry());
  142. return Result.success();
  143. }
  144. /**
  145. * 注册
  146. * areaNum 区号
  147. * phoneNum 手机号码
  148. */
  149. @PostMapping("/register")
  150. public Result register(@RequestBody RegisterParam param) throws Exception {
  151. if(param.getMsgAuthCode().equals(registerValidCode)){
  152. param.setClear("YES");
  153. }
  154. loginService.register(param);
  155. return Result.success();
  156. }
  157. /**
  158. * 修改密码
  159. */
  160. @PostMapping("/changePassword")
  161. public Result changePassword(@RequestBody RegisterParam param){
  162. loginService.changePassword(param);
  163. return Result.success();
  164. }
  165. /**
  166. * 检验token是否过期
  167. */
  168. @PostMapping(value = "/checkToken")
  169. public Result checkToken(){
  170. loginService.loginCheck(getToken());
  171. return Result.success();
  172. }
  173. /**
  174. * 检验验证码是否有效
  175. */
  176. @PostMapping(value = "/checkSms")
  177. public Result checkSms(@RequestBody RegisterParam param){
  178. String username = JwtUtil.getUsername(getToken());
  179. loginService.checkSms(param.getMsgAuthCode(),username,false);
  180. return Result.success();
  181. }
  182. /**
  183. * getToken
  184. */
  185. @PostMapping("/getToken")
  186. public Result getToken(@RequestBody LoginParam param){
  187. return Result.success(loginService.loginClear(param));
  188. }
  189. /**
  190. * 测试使用 获取具体验证码
  191. * @param user
  192. * @return
  193. */
  194. @RequestMapping(value = "/findMsgAuthCode", method = RequestMethod.POST)
  195. public Result findMsgAuthCode(@RequestBody LoginParam user) {
  196. String redisKey = RedisKeyUtil.PREFIX_MSG_AUTH_CODE +user.getPhoneNum();
  197. if(!"prod".equals(environment)){
  198. return Result.success(redisUtil.get(redisKey));
  199. }
  200. if("18819272208".equals(user.getPhoneNum()) || "12369874542".equals(user.getPhoneNum())){
  201. return Result.success(redisUtil.get(redisKey));
  202. }
  203. return Result.success();
  204. }
  205. @GetMapping("/getLoginAuthCode")
  206. public void getLoginCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
  207. response.setHeader("Cache-Control", "no-store, no-cache");
  208. response.setContentType("image/jpeg");
  209. String id = request.getSession().getId();
  210. try {
  211. LineCaptcha lineCaptcha = new LineCaptcha(300, 100);
  212. MathGenerator mathGenerator = new MathGenerator(2);
  213. lineCaptcha.setGenerator(mathGenerator);
  214. redisUtil.set(String.format(RedisKeyUtil.loginAuthCode,id),lineCaptcha.getCode(),60*5);
  215. lineCaptcha.write(response.getOutputStream());
  216. response.getOutputStream().close();
  217. } catch (Exception e){
  218. log.info("生成登录验证码错误:",e);
  219. }
  220. }
  221. }