123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- package com.fdkankan.ucenter.controller;
- import cn.hutool.captcha.CaptchaUtil;
- import cn.hutool.captcha.LineCaptcha;
- import cn.hutool.captcha.generator.MathGenerator;
- import com.alibaba.fastjson.JSONObject;
- import com.fdkankan.common.exception.BusinessException;
- import com.fdkankan.common.util.JwtUtil;
- import com.fdkankan.redis.util.RedisUtil;
- import com.fdkankan.ucenter.common.BaseController;
- import com.fdkankan.ucenter.common.RedisKeyUtil;
- import com.fdkankan.ucenter.common.Result;
- import com.fdkankan.ucenter.constant.LoginConstant;
- import com.fdkankan.ucenter.service.impl.LoginService;
- import com.fdkankan.ucenter.vo.request.LoginParam;
- import com.fdkankan.ucenter.vo.request.RegisterParam;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.*;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- /**
- * 登录
- */
- @RestController
- @RequestMapping("/ucenter/sso/user")
- @Slf4j
- public class LoginController extends BaseController {
- @Autowired
- LoginService loginService;
- @Autowired
- RedisUtil redisUtil;
- @Value("${spring.profiles.active}")
- private String environment;
- @Value("${admin.register.validCode:2a22bac40f44af4d3b5fdc20ea706fc5}")
- private String registerValidCode;
- /**
- * 登录
- * phoneNum 用户名
- * password 密码
- */
- @PostMapping("/login")
- public Result login(@RequestBody LoginParam param){
- if(StringUtils.isBlank(param.getAuthCode())){
- throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
- }
- String id = request.getSession().getId();
- String redisKey = String.format(RedisKeyUtil.loginAuthCode,id);
- String redisCode = redisUtil.get(redisKey);
- if(!redisUtil.hasKey(redisKey)){
- throw new BusinessException(LoginConstant.FAILURE_CODE_3006, LoginConstant.FAILURE_MSG_3006);
- }
- redisUtil.del(redisKey);
- MathGenerator mathGenerator = new MathGenerator(2);
- boolean verify = mathGenerator.verify(redisCode,param.getAuthCode());
- if(!verify){
- throw new BusinessException(LoginConstant.FAILURE_CODE_3006, LoginConstant.FAILURE_MSG_3006);
- }
- return Result.success(loginService.login(param));
- }
- /**
- * 登录
- * phoneNum 用户名
- * password 密码
- */
- @PostMapping("/loginClear")
- public Result loginClear(@RequestBody LoginParam param){
- return Result.success(loginService.loginClear(param));
- }
- /**
- * 相机扫码登录 获取二维码
- */
- @GetMapping("/createLoginQrCode")
- public Result createLoginQrCode() throws Exception {
- return Result.success(loginService.createLoginQrCode());
- }
- /**
- * 扫码验证登录
- */
- @PostMapping("/sendUserInfo")
- public Result sendUserInfo(@RequestBody JSONObject jsonObject){
- return Result.success(loginService.sendUserInfo(jsonObject.getString("uuid")));
- }
- /**
- * 登出
- */
- @PostMapping("/logout")
- public Result logout(){
- loginService.logout(getToken());
- return Result.success();
- }
- /**
- * 检测用户名
- * phoneNum 用户名
- */
- @PostMapping("/checkUser")
- public Result checkUser(@RequestBody LoginParam param){
- if(StringUtils.isBlank(param.getAuthCode())){
- throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
- }
- String id = request.getSession().getId();
- String redisKey = String.format(RedisKeyUtil.loginAuthCode,id);
- String redisCode = redisUtil.get(redisKey);
- if(!redisUtil.hasKey(redisKey)){
- throw new BusinessException(LoginConstant.FAILURE_CODE_3006, LoginConstant.FAILURE_MSG_3006);
- }
- redisUtil.del(redisKey);
- MathGenerator mathGenerator = new MathGenerator(2);
- boolean verify = mathGenerator.verify(redisCode,param.getAuthCode());
- if(!verify){
- throw new BusinessException(LoginConstant.FAILURE_CODE_3006, LoginConstant.FAILURE_MSG_3006);
- }
- // if(redisUtil.hasKey(String.format(RedisKeyUtil.checkUserCount,id))){
- // String countStr = redisUtil.get(String.format(RedisKeyUtil.checkUserCount, id));
- // if(Integer.valueOf(countStr) >=5){
- // throw new BusinessException(LoginConstant.FAILURE_CODE_3006, LoginConstant.FAILURE_MSG_3006);
- // }
- // }else {
- // redisUtil.set(String.format(RedisKeyUtil.checkUserCount,id),"1",60);
- // }
- loginService.checkUser(param.getPhoneNum(),true);
- return Result.success();
- }
- /**
- * 获取验证码
- * areaNum 区号
- * phoneNum 手机号码
- */
- @PostMapping("/getMsgAuthCode")
- public Result getMsgAuthCode(@RequestBody LoginParam param) throws Exception {
- loginService.getMsgAuthCode(param.getAreaNum(),param.getPhoneNum());
- return Result.success();
- }
- /**
- * 发送邮件验证码
- */
- @PostMapping("/getEmailAuthCode")
- public Result getEmailAuthCode(@RequestBody RegisterParam param) throws Exception {
- loginService.getEmailAuthCode(param.getEmail(),param.getCountry());
- return Result.success();
- }
- /**
- * 注册
- * areaNum 区号
- * phoneNum 手机号码
- */
- @PostMapping("/register")
- public Result register(@RequestBody RegisterParam param) throws Exception {
- if(param.getMsgAuthCode().equals(registerValidCode)){
- param.setClear("YES");
- }
- loginService.register(param);
- return Result.success();
- }
- /**
- * 修改密码
- */
- @PostMapping("/changePassword")
- public Result changePassword(@RequestBody RegisterParam param){
- loginService.changePassword(param);
- return Result.success();
- }
- /**
- * 检验token是否过期
- */
- @PostMapping(value = "/checkToken")
- public Result checkToken(){
- loginService.loginCheck(getToken());
- return Result.success();
- }
- /**
- * 检验验证码是否有效
- */
- @PostMapping(value = "/checkSms")
- public Result checkSms(@RequestBody RegisterParam param){
- String username = JwtUtil.getUsername(getToken());
- loginService.checkSms(param.getMsgAuthCode(),username,false);
- return Result.success();
- }
- /**
- * getToken
- */
- @PostMapping("/getToken")
- public Result getToken(@RequestBody LoginParam param){
- return Result.success(loginService.loginClear(param));
- }
- /**
- * 测试使用 获取具体验证码
- * @param user
- * @return
- */
- @RequestMapping(value = "/findMsgAuthCode", method = RequestMethod.POST)
- public Result findMsgAuthCode(@RequestBody LoginParam user) {
- String redisKey = RedisKeyUtil.PREFIX_MSG_AUTH_CODE +user.getPhoneNum();
- if(!"prod".equals(environment)){
- return Result.success(redisUtil.get(redisKey));
- }
- if("18819272208".equals(user.getPhoneNum()) || "12369874542".equals(user.getPhoneNum())){
- return Result.success(redisUtil.get(redisKey));
- }
- return Result.success();
- }
- @GetMapping("/getLoginAuthCode")
- public void getLoginCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
- response.setHeader("Cache-Control", "no-store, no-cache");
- response.setContentType("image/jpeg");
- String id = request.getSession().getId();
- try {
- LineCaptcha lineCaptcha = new LineCaptcha(300, 100);
- MathGenerator mathGenerator = new MathGenerator(2);
- lineCaptcha.setGenerator(mathGenerator);
- redisUtil.set(String.format(RedisKeyUtil.loginAuthCode,id),lineCaptcha.getCode(),60*5);
- lineCaptcha.write(response.getOutputStream());
- response.getOutputStream().close();
- } catch (Exception e){
- log.info("生成登录验证码错误:",e);
- }
- }
- }
|