From 82b5c11b7f3bb0f74c108fe2c06721968ae2b5da Mon Sep 17 00:00:00 2001
From: kaiyu <404897439@qq.com>
Date: Thu, 18 Mar 2021 17:23:14 +0800
Subject: [PATCH] common: 完成验证码工具类 生成校验
---
screen-manage/src/main/java/com/moral/api/controller/LoginController.java | 125 +++++++++++++++++++++++++++++++
screen-manage/src/main/resources/application-dev.yml | 6 +
screen-common/src/main/java/com/moral/pojo/VerificationCode.java | 15 +++
screen-common/src/main/java/com/moral/constant/Constants.java | 1
screen-common/src/main/java/com/moral/util/KaptchaUtils.java | 23 ++++-
screen-manage/src/main/java/com/moral/api/controller/AccountController.java | 68 ++++-------------
6 files changed, 178 insertions(+), 60 deletions(-)
diff --git a/screen-common/src/main/java/com/moral/constant/Constants.java b/screen-common/src/main/java/com/moral/constant/Constants.java
index f30292d..c9cb5f2 100644
--- a/screen-common/src/main/java/com/moral/constant/Constants.java
+++ b/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";
+
}
diff --git a/screen-common/src/main/java/com/moral/pojo/VerificationCode.java b/screen-common/src/main/java/com/moral/pojo/VerificationCode.java
index f524a9f..03a2210 100644
--- a/screen-common/src/main/java/com/moral/pojo/VerificationCode.java
+++ b/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;
+ }
}
diff --git a/screen-common/src/main/java/com/moral/util/KaptchaUtils.java b/screen-common/src/main/java/com/moral/util/KaptchaUtils.java
index 7a1abe4..e79f9e3 100644
--- a/screen-common/src/main/java/com/moral/util/KaptchaUtils.java
+++ b/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,12 +73,19 @@
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) {
- return false;
+ 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;
}
diff --git a/screen-manage/src/main/java/com/moral/api/controller/AccountController.java b/screen-manage/src/main/java/com/moral/api/controller/AccountController.java
index 3e194d4..ece5b58 100644
--- a/screen-manage/src/main/java/com/moral/api/controller/AccountController.java
+++ b/screen-manage/src/main/java/com/moral/api/controller/AccountController.java
@@ -37,55 +37,27 @@
@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())
+ public ResultMessage insert(@RequestBody AccountInsertForm accountInsertForm) {
+ if (!accountInsertForm.valid())
return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
//������������������������������
AccountInsertDTO conditionDTO = accountInsertForm.paramValid();
- if(conditionDTO.getCode()!=ResponseCodeEnum.SUCCESS.getCode()){
- return new ResultMessage(conditionDTO.getCode(),conditionDTO.getMsg(),null);
+ if (conditionDTO.getCode() != ResponseCodeEnum.SUCCESS.getCode()) {
+ return new ResultMessage(conditionDTO.getCode(), conditionDTO.getMsg(), null);
}
AccountInsertDTO accountInsertDTO = accountService.insertAccount(accountInsertForm);
AccountInsertVO accountInsertVO = AccountInsertVO.convert(accountInsertDTO);
- return new ResultMessage(accountInsertDTO.getCode(),accountInsertDTO.getMsg(),accountInsertVO);
+ return new ResultMessage(accountInsertDTO.getCode(), accountInsertDTO.getMsg(), accountInsertVO);
}
@GetMapping("query")
- public ResultMessage query(AccountQueryForm accountQueryForm){
- if(!accountQueryForm.valid())
+ public ResultMessage query(AccountQueryForm accountQueryForm) {
+ if (!accountQueryForm.valid())
return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
@@ -93,32 +65,32 @@
AccountQueryVO accountQueryVO = AccountQueryVO.convert(accountQueryDTO);
- return new ResultMessage(accountQueryDTO.getCode(),accountQueryDTO.getMsg(),accountQueryVO);
+ return new ResultMessage(accountQueryDTO.getCode(), accountQueryDTO.getMsg(), accountQueryVO);
}
@PostMapping("update")
- public ResultMessage update(@RequestBody AccountUpdateForm accountUpdateRequest){
- if(!accountUpdateRequest.valid())
+ public ResultMessage update(@RequestBody AccountUpdateForm accountUpdateRequest) {
+ if (!accountUpdateRequest.valid())
return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
//������������������������������
AccountUpdateDTO conditionDTO = accountUpdateRequest.paramValid();
- if(conditionDTO.getCode()!=ResponseCodeEnum.SUCCESS.getCode()){
- return new ResultMessage(conditionDTO.getCode(),conditionDTO.getMsg(),null);
+ if (conditionDTO.getCode() != ResponseCodeEnum.SUCCESS.getCode()) {
+ return new ResultMessage(conditionDTO.getCode(), conditionDTO.getMsg(), null);
}
AccountUpdateDTO accountUpdateDTO = accountService.updateAccount(accountUpdateRequest);
AccountUpdateVO accountUpdateVO = AccountUpdateVO.convert(accountUpdateDTO);
- return new ResultMessage(accountUpdateDTO.getCode(),accountUpdateDTO.getMsg(),accountUpdateVO);
+ return new ResultMessage(accountUpdateDTO.getCode(), accountUpdateDTO.getMsg(), accountUpdateVO);
}
@PostMapping("delete")
- public ResultMessage delete(@RequestBody AccountDeleteForm accountDeleteForm){
- if(!accountDeleteForm.valid())
+ public ResultMessage delete(@RequestBody AccountDeleteForm accountDeleteForm) {
+ if (!accountDeleteForm.valid())
return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
@@ -126,16 +98,8 @@
AccountDeleteVO accountDeleteVO = AccountDeleteVO.convert(accountDeleteDTO);
- return new ResultMessage(accountDeleteDTO.getCode(),accountDeleteDTO.getMsg(),accountDeleteVO);
+ 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;
- }
-
}
diff --git a/screen-manage/src/main/java/com/moral/api/controller/LoginController.java b/screen-manage/src/main/java/com/moral/api/controller/LoginController.java
new file mode 100644
index 0000000..c705879
--- /dev/null
+++ b/screen-manage/src/main/java/com/moral/api/controller/LoginController.java
@@ -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();
+ }
+}
diff --git a/screen-manage/src/main/resources/application-dev.yml b/screen-manage/src/main/resources/application-dev.yml
index 7aa5cab..6f70051 100644
--- a/screen-manage/src/main/resources/application-dev.yml
+++ b/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
--
Gitblit v1.8.0