Skip to content

Commit

Permalink
💄 验证码生成规则修改
Browse files Browse the repository at this point in the history
  • Loading branch information
ZHENFENG13 committed Mar 4, 2022
1 parent c10939e commit 337164f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 31 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
</dependency>
<!-- 验证码 -->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
<groupId>cn.hutool</groupId>
<artifactId>hutool-captcha</artifactId>
<version>5.7.22</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
package ltd.newbee.mall.controller.admin;

import cn.hutool.captcha.ShearCaptcha;
import ltd.newbee.mall.common.ServiceResultEnum;
import ltd.newbee.mall.entity.AdminUser;
import ltd.newbee.mall.service.AdminUserService;
Expand Down Expand Up @@ -63,8 +64,8 @@ public String login(@RequestParam("userName") String userName,
session.setAttribute("errorMsg", "用户名或密码不能为空");
return "admin/login";
}
String kaptchaCode = session.getAttribute("verifyCode") + "";
if (StringUtils.isEmpty(kaptchaCode) || !verifyCode.toLowerCase().equals(kaptchaCode)) {
ShearCaptcha shearCaptcha = (ShearCaptcha) session.getAttribute("verifyCode");
if (shearCaptcha == null || !shearCaptcha.verify(verifyCode)) {
session.setAttribute("errorMsg", "验证码错误");
return "admin/login";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
*/
package ltd.newbee.mall.controller.common;

import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import ltd.newbee.mall.common.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -33,20 +33,13 @@ public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletRes
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.setContentType("image/png");

// 三个参数分别为宽、高、位数
SpecCaptcha captcha = new SpecCaptcha(150, 40, 4);

// 设置类型 数字和字母混合
captcha.setCharType(Captcha.TYPE_DEFAULT);

//设置字体
captcha.setCharType(Captcha.FONT_9);
ShearCaptcha shearCaptcha= CaptchaUtil.createShearCaptcha(150, 30, 4, 2);

// 验证码存入session
httpServletRequest.getSession().setAttribute("verifyCode", captcha.text().toLowerCase());
httpServletRequest.getSession().setAttribute("verifyCode", shearCaptcha);

// 输出图片流
captcha.out(httpServletResponse.getOutputStream());
shearCaptcha.write(httpServletResponse.getOutputStream());
}

@GetMapping("/common/mall/kaptcha")
Expand All @@ -56,19 +49,12 @@ public void mallKaptcha(HttpServletRequest httpServletRequest, HttpServletRespon
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.setContentType("image/png");

// 三个参数分别为宽、高、位数
SpecCaptcha captcha = new SpecCaptcha(110, 40, 4);

// 设置类型 数字和字母混合
captcha.setCharType(Captcha.TYPE_DEFAULT);

//设置字体
captcha.setCharType(Captcha.FONT_9);
ShearCaptcha shearCaptcha= CaptchaUtil.createShearCaptcha(110, 40, 4, 2);

// 验证码存入session
httpServletRequest.getSession().setAttribute(Constants.MALL_VERIFY_CODE_KEY, captcha.text().toLowerCase());
httpServletRequest.getSession().setAttribute(Constants.MALL_VERIFY_CODE_KEY, shearCaptcha);

// 输出图片流
captcha.out(httpServletResponse.getOutputStream());
shearCaptcha.write(httpServletResponse.getOutputStream());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
package ltd.newbee.mall.controller.mall;

import cn.hutool.captcha.ShearCaptcha;
import ltd.newbee.mall.common.Constants;
import ltd.newbee.mall.common.ServiceResultEnum;
import ltd.newbee.mall.controller.vo.NewBeeMallUserVO;
Expand Down Expand Up @@ -73,8 +74,9 @@ public Result login(@RequestParam("loginName") String loginName,
if (StringUtils.isEmpty(verifyCode)) {
return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_VERIFY_CODE_NULL.getResult());
}
String kaptchaCode = httpSession.getAttribute(Constants.MALL_VERIFY_CODE_KEY) + "";
if (StringUtils.isEmpty(kaptchaCode) || !verifyCode.toLowerCase().equals(kaptchaCode)) {
ShearCaptcha shearCaptcha = (ShearCaptcha) httpSession.getAttribute(Constants.MALL_VERIFY_CODE_KEY);

if (shearCaptcha == null || !shearCaptcha.verify(verifyCode)) {
return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_VERIFY_CODE_ERROR.getResult());
}
String loginResult = newBeeMallUserService.login(loginName, MD5Util.MD5Encode(password, "UTF-8"), httpSession);
Expand Down Expand Up @@ -103,8 +105,8 @@ public Result register(@RequestParam("loginName") String loginName,
if (StringUtils.isEmpty(verifyCode)) {
return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_VERIFY_CODE_NULL.getResult());
}
String kaptchaCode = httpSession.getAttribute(Constants.MALL_VERIFY_CODE_KEY) + "";
if (StringUtils.isEmpty(kaptchaCode) || !verifyCode.toLowerCase().equals(kaptchaCode)) {
ShearCaptcha shearCaptcha = (ShearCaptcha) httpSession.getAttribute(Constants.MALL_VERIFY_CODE_KEY);
if (shearCaptcha == null || !shearCaptcha.verify(verifyCode)) {
return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_VERIFY_CODE_ERROR.getResult());
}
String registerResult = newBeeMallUserService.register(loginName, password);
Expand Down

0 comments on commit 337164f

Please sign in to comment.