验证码高度,宽度,长度,过期时间可配置
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
#欢迎查看boot-starter-security-core项目说明
|
||||
|
||||
##本项目的参数配置说明
|
||||
本项目说明中的配置参数是boot-starter-security-browser和boot-starter-security-app
|
||||
项目的交集
|
||||
- `ms.security.code.image.width` 图片验证码宽度,默认值是`67` 可以在请求中使用`width`参数设置
|
||||
- `ms.security.code.image.height`图片验证码高度,默认值是`23` 可以在请求中使用`height`参数设置
|
||||
- `ms.security.code.image.length`图片验证码的code长度,默认是`4` 不能在请求中设置
|
||||
- `ms.security.code.image.expireIn` 图片验证码的有效时间`60`秒 不能在请求中设置
|
||||
|
||||
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package com.micro.fast.security.core.config.param;
|
||||
|
||||
/**
|
||||
* 图形验证码默认的配置
|
||||
*/
|
||||
public class ImageCodeProperties {
|
||||
/**
|
||||
* 验证码的宽度
|
||||
*/
|
||||
private int width = 67;
|
||||
/**
|
||||
* 验证码的高度
|
||||
*/
|
||||
private int height = 23;
|
||||
/**
|
||||
* 验证码数字的长度
|
||||
*/
|
||||
private int length = 4;
|
||||
/**
|
||||
* 验证码的有效时间
|
||||
*/
|
||||
private int expireIn = 60;
|
||||
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public void setWidth(int width) {
|
||||
this.width = width;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public void setHeight(int height) {
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public int getLength() {
|
||||
return length;
|
||||
}
|
||||
|
||||
public void setLength(int length) {
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
public int getExpireIn() {
|
||||
return expireIn;
|
||||
}
|
||||
|
||||
public void setExpireIn(int expireIn) {
|
||||
this.expireIn = expireIn;
|
||||
}
|
||||
}
|
||||
+11
@@ -1,5 +1,6 @@
|
||||
package com.micro.fast.security.core.config.param;
|
||||
|
||||
import com.micro.fast.security.core.pojo.ValidateCode;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
import javax.validation.Valid;
|
||||
@@ -16,6 +17,8 @@ public class SecurityProperties {
|
||||
@Valid
|
||||
private BrowserProperties browser = new BrowserProperties();
|
||||
|
||||
private ValidateCodeProperties code = new ValidateCodeProperties();
|
||||
|
||||
public BrowserProperties getBrowser() {
|
||||
return browser;
|
||||
}
|
||||
@@ -23,4 +26,12 @@ public class SecurityProperties {
|
||||
public void setBrowser(BrowserProperties browser) {
|
||||
this.browser = browser;
|
||||
}
|
||||
|
||||
public ValidateCodeProperties getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(ValidateCodeProperties code) {
|
||||
this.code = code;
|
||||
}
|
||||
}
|
||||
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.micro.fast.security.core.config.param;
|
||||
|
||||
/**
|
||||
* 验证码配置参数
|
||||
*/
|
||||
public class ValidateCodeProperties {
|
||||
|
||||
private ImageCodeProperties image = new ImageCodeProperties();
|
||||
|
||||
public ImageCodeProperties getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public void setImage(ImageCodeProperties image) {
|
||||
this.image = image;
|
||||
}
|
||||
}
|
||||
+7
-1
@@ -1,9 +1,11 @@
|
||||
package com.micro.fast.security.core.controller;
|
||||
|
||||
import com.micro.fast.security.core.config.param.SecurityProperties;
|
||||
import com.micro.fast.security.core.constant.ValidateCodeConst;
|
||||
import com.micro.fast.security.core.pojo.ValidateCode;
|
||||
import com.micro.fast.security.core.util.ImageCodeUtil;
|
||||
import com.micro.fast.security.core.pojo.ImageCode;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.social.connect.web.HttpSessionSessionStrategy;
|
||||
import org.springframework.social.connect.web.SessionStrategy;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -21,13 +23,17 @@ import java.io.IOException;
|
||||
@RestController
|
||||
public class ValidateCodeController {
|
||||
|
||||
@Autowired
|
||||
private SecurityProperties securityProperties;
|
||||
|
||||
private SessionStrategy sessionStrategy = new HttpSessionSessionStrategy();
|
||||
|
||||
@GetMapping("/code/image")
|
||||
public void createCode(HttpServletResponse response, HttpServletRequest request) throws IOException {
|
||||
//创建图片验证码
|
||||
ImageCode imageCode = new ImageCodeUtil().createImageCode();
|
||||
ImageCodeUtil imageCodeUtil = new ImageCodeUtil();
|
||||
imageCodeUtil.setSecurityProperties(securityProperties);
|
||||
ImageCode imageCode = imageCodeUtil.createImageCode(new ServletWebRequest(request));
|
||||
ValidateCode validateCode = new ValidateCode(imageCode.getCode(),imageCode.getExpireTime());
|
||||
//将验证码对象写入session中
|
||||
sessionStrategy.setAttribute(new ServletWebRequest(request), ValidateCodeConst.SESSION_KEY_IMAGE_CODE,validateCode);
|
||||
|
||||
+28
-7
@@ -1,6 +1,12 @@
|
||||
package com.micro.fast.security.core.util;
|
||||
|
||||
import com.micro.fast.security.core.config.param.ImageCodeProperties;
|
||||
import com.micro.fast.security.core.config.param.SecurityProperties;
|
||||
import com.micro.fast.security.core.pojo.ImageCode;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.ServletRequestUtils;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
@@ -10,12 +16,18 @@ import java.util.Random;
|
||||
* 验证码工具类
|
||||
*/
|
||||
public class ImageCodeUtil {
|
||||
//TODO 实现验证码生成类
|
||||
public ImageCode createImageCode(){
|
||||
|
||||
private SecurityProperties securityProperties;
|
||||
|
||||
/**
|
||||
* 证码生成类
|
||||
*/
|
||||
public ImageCode createImageCode(ServletWebRequest request){
|
||||
ImageCodeProperties imageCodeProperties = securityProperties.getCode().getImage();
|
||||
//图片验证码的宽度
|
||||
int width = 63;
|
||||
int width = ServletRequestUtils.getIntParameter(request.getRequest(),"width",imageCodeProperties.getWidth());
|
||||
//图片验证码的高度
|
||||
int height =24;
|
||||
int height =ServletRequestUtils.getIntParameter(request.getRequest(),"height",imageCodeProperties.getHeight());
|
||||
//生成图片
|
||||
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics g = image.getGraphics();
|
||||
@@ -33,15 +45,16 @@ public class ImageCodeUtil {
|
||||
}
|
||||
//获取验证码数字
|
||||
String sRand = "";
|
||||
for (int i = 0; i < 4; i++) {
|
||||
//验证码的位数不能在请求中进行设置
|
||||
for (int i = 0; i < imageCodeProperties.getLength(); i++) {
|
||||
String rand = String.valueOf(random.nextInt(10));
|
||||
sRand += rand;
|
||||
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
|
||||
g.drawString(rand, 13 * i + 6, 16);
|
||||
g.drawString(rand, 13 * i + 6, (height-20)/2+20-2);
|
||||
}
|
||||
g.dispose();
|
||||
//设置多少时间过期返回图片验证码对象
|
||||
return new ImageCode(image, sRand, 300);
|
||||
return new ImageCode(image, sRand, imageCodeProperties.getExpireIn());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,4 +76,12 @@ public class ImageCodeUtil {
|
||||
int b = fc + random.nextInt(bc - fc);
|
||||
return new Color(r, g, b);
|
||||
}
|
||||
|
||||
public SecurityProperties getSecurityProperties() {
|
||||
return securityProperties;
|
||||
}
|
||||
|
||||
public void setSecurityProperties(SecurityProperties securityProperties) {
|
||||
this.securityProperties = securityProperties;
|
||||
}
|
||||
}
|
||||
|
||||
+3
@@ -17,4 +17,7 @@ ms:
|
||||
browser:
|
||||
html: /signIn.html
|
||||
loginType: JSON
|
||||
code:
|
||||
image:
|
||||
length: 6
|
||||
|
||||
|
||||
+1
-1
@@ -54,7 +54,7 @@ public class WatermarkerUtil {
|
||||
* @param text 要添加的水印的文字内容
|
||||
* @param inputImage 输入的图片文件
|
||||
* @param filePath 生成图片的输出路径
|
||||
* @return file 加上水印之后的图片对象
|
||||
* @return file 加上水印之后的图片对象
|
||||
*/
|
||||
public File markerWord(String text,File inputImage,String filePath){
|
||||
String imageName = inputImage.getName();
|
||||
|
||||
Reference in New Issue
Block a user