Skip to content

Commit

Permalink
💄 验证码生成规则修改
Browse files Browse the repository at this point in the history
  • Loading branch information
newbee-mall committed Dec 23, 2020
1 parent e96e2dc commit a146a49
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 100 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ newbee-mall 项目是一套电商系统,包括 newbee-mall 商城系统及 new
- [tonytomov](https://github.com/tonytomov/jqGrid)
- [t4t5](https://github.com/t4t5/sweetalert)
- [skytotwo](https://github.com/skytotwo/Alipay-WeChat-HTML)
- [EasyCaptcha](https://github.com/whvcse/EasyCaptcha)
- [wangeditor-team](https://github.com/wangeditor-team/wangEditor)
- [Vue](https://github.com/vuejs/vue)
- [Vant](https://github.com/youzan/vant)
7 changes: 4 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@
</dependency>
<!-- 验证码 -->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,14 @@
*/
package ltd.newbee.mall.controller.common;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import ltd.newbee.mall.common.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.Properties;

/**
* @author 13
Expand All @@ -32,68 +26,49 @@
@Controller
public class CommonController {

@Autowired
private DefaultKaptcha captchaProducer;

@GetMapping("/common/kaptcha")
public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
byte[] captchaOutputStream = null;
ByteArrayOutputStream imgOutputStream = new ByteArrayOutputStream();
try {
//生产验证码字符串并保存到session中
String verifyCode = captchaProducer.createText();
httpServletRequest.getSession().setAttribute("verifyCode", verifyCode);
BufferedImage challenge = captchaProducer.createImage(verifyCode);
ImageIO.write(challenge, "jpg", imgOutputStream);
} catch (IllegalArgumentException e) {
httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
captchaOutputStream = imgOutputStream.toByteArray();
httpServletResponse.setHeader("Cache-Control", "no-store");
httpServletResponse.setHeader("Pragma", "no-cache");
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.setContentType("image/jpeg");
ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
responseOutputStream.write(captchaOutputStream);
responseOutputStream.flush();
responseOutputStream.close();
httpServletResponse.setContentType("image/png");

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

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

//设置字体
captcha.setCharType(Captcha.FONT_9);

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

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

@GetMapping("/common/mall/kaptcha")
public void mallKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
com.google.code.kaptcha.impl.DefaultKaptcha newBeeMallLoginKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
Properties properties = new Properties();
properties.put("kaptcha.border", "no");
properties.put("kaptcha.textproducer.font.color", "27,174,171");
properties.put("kaptcha.noise.color", "20,33,42");
properties.put("kaptcha.textproducer.font.size", "36");
properties.put("kaptcha.image.width", "110");
properties.put("kaptcha.image.height", "40");
properties.put("kaptcha.session.key", Constants.MALL_VERIFY_CODE_KEY);
properties.put("kaptcha.textproducer.char.length", "4");
Config config = new Config(properties);
newBeeMallLoginKaptcha.setConfig(config);
byte[] captchaOutputStream = null;
ByteArrayOutputStream imgOutputStream = new ByteArrayOutputStream();
try {
//生产验证码字符串并保存到session中
String verifyCode = newBeeMallLoginKaptcha.createText();
httpServletRequest.getSession().setAttribute(Constants.MALL_VERIFY_CODE_KEY, verifyCode);
BufferedImage challenge = newBeeMallLoginKaptcha.createImage(verifyCode);
ImageIO.write(challenge, "jpg", imgOutputStream);
} catch (IllegalArgumentException e) {
httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
captchaOutputStream = imgOutputStream.toByteArray();
httpServletResponse.setHeader("Cache-Control", "no-store");
httpServletResponse.setHeader("Pragma", "no-cache");
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.setContentType("image/jpeg");
ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
responseOutputStream.write(captchaOutputStream);
responseOutputStream.flush();
responseOutputStream.close();
httpServletResponse.setContentType("image/png");

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

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

//设置字体
captcha.setCharType(Captcha.FONT_9);

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

// 输出图片流
captcha.out(httpServletResponse.getOutputStream());
}
}
36 changes: 0 additions & 36 deletions src/main/java/ltd/newbee/mall/controller/common/KaptchaConfig.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/resources/templates/mall/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
验证码:&nbsp;
<input class="login-info-input verify-code" type="text" name="verifyCode"
placeholder="请输入验证码" id="verifyCode"/>
<img alt="单击图片刷新!" style="top: 14px;position: relative;" th:src="@{/common/mall/kaptcha}"
<img alt="单击图片刷新!" style="top: 16px;position: relative;" th:src="@{/common/mall/kaptcha}"
onclick="this.src='/common/mall/kaptcha?d='+new Date()*1">
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/mall/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
验证码:&nbsp;
<input class="login-info-input verify-code" type="text" name="verifyCode" id="verifyCode"
placeholder="请输入验证码"/>
<img alt="单击图片刷新!" style="top: 14px;position: relative;" th:src="@{/common/mall/kaptcha}"
<img alt="单击图片刷新!" style="top: 16px;position: relative;" th:src="@{/common/mall/kaptcha}"
onclick="this.src='/common/mall/kaptcha?d='+new Date()*1">
</div>
</div>
Expand Down

0 comments on commit a146a49

Please sign in to comment.