kaiyu
2021-03-18 82b5c11b7f3bb0f74c108fe2c06721968ae2b5da
common:
完成验证码工具类 生成校验
1 files added
5 files modified
204 ■■■■ changed files
screen-common/src/main/java/com/moral/constant/Constants.java 1 ●●●● patch | view | raw | blame | history
screen-common/src/main/java/com/moral/pojo/VerificationCode.java 15 ●●●●● patch | view | raw | blame | history
screen-common/src/main/java/com/moral/util/KaptchaUtils.java 21 ●●●● patch | view | raw | blame | history
screen-manage/src/main/java/com/moral/api/controller/AccountController.java 36 ●●●●● patch | view | raw | blame | history
screen-manage/src/main/java/com/moral/api/controller/LoginController.java 125 ●●●●● patch | view | raw | blame | history
screen-manage/src/main/resources/application-dev.yml 6 ●●●●● patch | view | raw | blame | history
screen-common/src/main/java/com/moral/constant/Constants.java
@@ -7,4 +7,5 @@
    public static String DELETE = "1";
    public static String NOT_DELETE = "0";
}
screen-common/src/main/java/com/moral/pojo/VerificationCode.java
@@ -1,6 +1,7 @@
package com.moral.pojo;
import lombok.Data;
import org.springframework.util.ObjectUtils;
/**
 * @ClassName VerificationCode
@@ -14,5 +15,17 @@
    private String key;
    private String value;
    private String encode;
    private String inputText;
    public boolean valid(){
        if (
                ObjectUtils.isEmpty(key)        ||
                ObjectUtils.isEmpty(inputText)  ||
                        inputText.length()!=4
                )
            return false;
        return true;
    }
}
screen-common/src/main/java/com/moral/util/KaptchaUtils.java
@@ -7,6 +7,7 @@
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import sun.misc.BASE64Encoder;
import javax.annotation.Resource;
@@ -18,6 +19,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
 * @ClassName KaptchaUtils
@@ -33,6 +35,9 @@
    private static DefaultKaptcha defaultKaptcha;
    private static RedisTemplate redisTemplate;
    //验证码有效期 单位:秒
    private static final int validity_time = 60;
    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
@@ -54,6 +59,7 @@
     */
    public static VerificationCode createVerificationCode() throws IOException {
        VerificationCode verificationCode = new VerificationCode();
        //生成验证码内容
        String text = defaultKaptcha.createText();
        //生成图片
@@ -67,11 +73,18 @@
        String encode = encoder.encode(bytes);
        //将验证码存入redis
        String key = UUID.randomUUID().toString();
        //redisTemplate.opsForValsue().set(key,encode);
        //redisTemplate.expire();
        return null;
        redisTemplate.opsForValue().set(key,text);
        redisTemplate.expire(key,validity_time, TimeUnit.SECONDS);
        verificationCode.setKey(key);
        verificationCode.setEncode(encode);
        return verificationCode;
    }
    public boolean verify(String verificationCode) {
    public static boolean verify(VerificationCode code) {
        String key = code.getKey();
        String inputText = code.getInputText();
        String validText = (String) redisTemplate.opsForValue().get(key);
        if(inputText.equals(validText))
            return true;
       return false;
    }
screen-manage/src/main/java/com/moral/api/controller/AccountController.java
@@ -37,34 +37,6 @@
    @Autowired
    ManageAccountService accountService;
    @PostMapping("login")
    public ResultMessage login(@RequestBody LoginForm loginForm) {
        if (!loginForm.valid())
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        LoginDTO loginDTO = accountService.login(loginForm);
        LoginVO loginVO = LoginVO.convert(loginDTO);
        return new ResultMessage(loginDTO.getCode(),loginDTO.getMsg(),loginVO);
    }
    @PostMapping("logout")
    public ResultMessage logout (@RequestBody LogoutForm logoutForm, HttpServletRequest request) {
        if(!logoutForm.valid())
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        String token = request.getHeader("token");
        logoutForm.setToken(token);
        if(accountService.logout(logoutForm))
            return ResultMessage.ok();
        return ResultMessage.fail();
    }
    @PostMapping("insert")
    public ResultMessage insert(@RequestBody AccountInsertForm accountInsertForm){
        if(!accountInsertForm.valid())
@@ -128,14 +100,6 @@
        return new ResultMessage(accountDeleteDTO.getCode(),accountDeleteDTO.getMsg(),accountDeleteVO);
    }
    @PostMapping("yanzhengma")
    public String yanzhengma(HttpServletResponse response) throws IOException {
        KaptchaUtils.createVerificationCode();
        TokenUtils.getToken("1",1);
        return null;
    }
}
screen-manage/src/main/java/com/moral/api/controller/LoginController.java
New file
@@ -0,0 +1,125 @@
package com.moral.api.controller;
import com.moral.api.pojo.dto.login.LoginDTO;
import com.moral.api.pojo.form.LoginForm;
import com.moral.api.pojo.form.LogoutForm;
import com.moral.api.pojo.vo.login.LoginVO;
import com.moral.api.service.ManageAccountService;
import com.moral.constant.ResponseCodeEnum;
import com.moral.constant.ResultMessage;
import com.moral.pojo.VerificationCode;
import com.moral.util.KaptchaUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
 * @ClassName LoginController
 * @Description TODO
 * @Author 陈凯裕
 * @Date 2021/3/18 15:28
 * @Version TODO
 **/
@Slf4j
@Api(tags = {"登陆模块"})
@RestController
public class LoginController {
    @Autowired
    ManageAccountService accountService;
    /**
     * @Description: 登陆接口
     * @Param: [loginForm]
     * @return: com.moral.constant.ResultMessage
     * @Author: 陈凯裕
     * @Date: 2021/3/18
     */
    @PostMapping("login")
    public ResultMessage login(@RequestBody LoginForm loginForm) {
        if (!loginForm.valid())
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        LoginDTO loginDTO = accountService.login(loginForm);
        LoginVO loginVO = LoginVO.convert(loginDTO);
        return new ResultMessage(loginDTO.getCode(), loginDTO.getMsg(), loginVO);
    }
    /**
     * @Description: 退出接口
     * @Param: [logoutForm, request]
     * @return: com.moral.constant.ResultMessage
     * @Author: 陈凯裕
     * @Date: 2021/3/18
     */
    @PostMapping("logout")
    public ResultMessage logout(@RequestBody LogoutForm logoutForm, HttpServletRequest request) {
        if (!logoutForm.valid())
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        String token = request.getHeader("token");
        logoutForm.setToken(token);
        if (accountService.logout(logoutForm))
            return ResultMessage.ok();
        return ResultMessage.fail();
    }
    /**
    * @Description: 获取验证码是否开启
            * @Param: []
            * @return: com.moral.constant.ResultMessage
            * @Author: 陈凯裕
            * @Date: 2021/3/18
            */
    @GetMapping
    public ResultMessage verifyConfig(){
        return null;
    }
    /**
     * @Description: 获取验证码接口
     * @Param: []
     * @return: com.moral.constant.ResultMessage
     * @Author: 陈凯裕
     * @Date: 2021/3/18
     */
    @GetMapping("verificationCode/get")
    public ResultMessage getVerificationCode() {
        VerificationCode verificationCode = null;
        try {
            verificationCode = KaptchaUtils.createVerificationCode();
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        if(ObjectUtils.isEmpty(verificationCode))
            return  ResultMessage.fail();
        System.out.println(verificationCode.getEncode());
        return ResultMessage.ok(verificationCode);
    }
    /**
    * @Description: 校验验证码
            * @Param: [verificationCode]
            * @return: com.moral.constant.ResultMessage
            * @Author: 陈凯裕
            * @Date: 2021/3/18
            */
    @GetMapping("verificationCode/verify")
    public ResultMessage gverifyVerificationCode(VerificationCode verificationCode) {
        if(!verificationCode.valid())
            return ResultMessage.fail();
        if(KaptchaUtils.verify(verificationCode))
            return ResultMessage.ok();
        return ResultMessage.fail();
    }
}
screen-manage/src/main/resources/application-dev.yml
@@ -113,16 +113,18 @@
mvc:
  interceptor:
    exclude:
      - /account/login
      - /login
      - /swagger-ui.html
      - /swagger-resources/**
      - /webjars/**
      - /account/logout
      - /logout
      - /account/insert
      - /account/query
      - /account/update
      - /account/delete
      - /account/yanzhengma
      - /verificationCode/get
      - /verificationCode/verify
AES:
  KEY:
    AD42F7787B035B7580000EF93BE20BAD