From 57cfad671fb6450c12e4f0dede7b248c5b9e799d Mon Sep 17 00:00:00 2001 From: 疯狂的狮子li <15040126243@163.com> Date: 星期五, 12 三月 2021 16:52:55 +0800 Subject: [PATCH] update 使用hutool重写验证码生成 --- /dev/null | 75 ------------ ruoyi-framework/pom.xml | 12 -- ruoyi-framework/src/main/java/com/ruoyi/framework/config/CaptchaConfig.java | 112 +++++++----------- ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java | 126 +++++++++++++------- ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java | 5 pom.xml | 10 - ruoyi-admin/src/main/resources/application.yml | 8 + ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java | 5 README.md | 1 9 files changed, 138 insertions(+), 216 deletions(-) diff --git a/README.md b/README.md index c30be9a..c8d2829 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ * 瀹瑰櫒鏀瑰姩 Tomcat 鏀逛负 骞跺彂鎬ц兘鏇村ソ鐨� undertow * 浠g爜鐢熸垚妯℃澘 鏀逛负閫傞厤 Mybatis-Plus 鐨勪唬鐮� * 椤圭洰淇敼涓� maven澶氱幆澧冮厤缃� +* 闆嗘垚 Hutool 5.X 骞堕噸鍐橰uoYi閮ㄥ垎鍔熻兘 * 闆嗘垚 Feign 鎺ュ彛鍖栫鐞� Http 璇锋眰(濡備笁鏂硅姹� 鏀粯,鐭俊,鎺ㄩ�佺瓑) * 鍗囩骇MybatisPlus 3.4.2 * 澧炲姞demo妯″潡绀轰緥(缁欎笉浼氬鍔犳ā鍧楃殑灏忎紮浼村仛鍙傝��) diff --git a/pom.xml b/pom.xml index c648de5..328bad0 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,6 @@ <druid.version>1.2.4</druid.version> <bitwalker.version>1.21</bitwalker.version> <swagger.version>2.9.2</swagger.version> - <kaptcha.version>2.3.2</kaptcha.version> <pagehelper.boot.version>1.3.0</pagehelper.boot.version> <fastjson.version>1.2.75</fastjson.version> <oshi.version>5.6.0</oshi.version> @@ -32,7 +31,7 @@ <velocity.version>1.7</velocity.version> <jwt.version>0.9.1</jwt.version> <mybatis-plus.version>3.4.2</mybatis-plus.version> - <hutool.version>5.4.0</hutool.version> + <hutool.version>5.5.8</hutool.version> <feign.version>2.2.6.RELEASE</feign.version> <feign-okhttp.version>11.0</feign-okhttp.version> </properties> @@ -160,13 +159,6 @@ <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>${jwt.version}</version> - </dependency> - - <!--楠岃瘉鐮� --> - <dependency> - <groupId>com.github.penggle</groupId> - <artifactId>kaptcha</artifactId> - <version>${kaptcha.version}</version> </dependency> <dependency> diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java index 8cafcef..b170b1d 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java @@ -1,86 +1,120 @@ package com.ruoyi.web.controller.common; -import java.awt.image.BufferedImage; -import java.io.IOException; import java.util.concurrent.TimeUnit; -import javax.annotation.Resource; -import javax.imageio.ImageIO; -import javax.servlet.http.HttpServletResponse; + +import cn.hutool.captcha.CircleCaptcha; +import cn.hutool.captcha.ICaptcha; +import cn.hutool.captcha.LineCaptcha; +import cn.hutool.captcha.ShearCaptcha; +import cn.hutool.captcha.generator.CodeGenerator; +import cn.hutool.captcha.generator.MathGenerator; +import cn.hutool.captcha.generator.RandomGenerator; +import cn.hutool.core.util.IdUtil; +import cn.hutool.core.util.StrUtil; +import com.ruoyi.common.core.text.Convert; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.util.FastByteArrayOutputStream; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; -import com.google.code.kaptcha.Producer; import com.ruoyi.common.constant.Constants; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.redis.RedisCache; -import com.ruoyi.common.utils.sign.Base64; -import com.ruoyi.common.utils.uuid.IdUtils; + +import javax.annotation.Resource; /** * 楠岃瘉鐮佹搷浣滃鐞� - * - * @author ruoyi + * + * @author Lion Li */ @RestController -public class CaptchaController -{ - @Resource(name = "captchaProducer") - private Producer captchaProducer; +public class CaptchaController { - @Resource(name = "captchaProducerMath") - private Producer captchaProducerMath; + // 鍦嗗湀骞叉壈楠岃瘉鐮� + @Resource(name = "CircleCaptcha") + private CircleCaptcha circleCaptcha; + // 绾挎骞叉壈鐨勯獙璇佺爜 + @Resource(name = "LineCaptcha") + private LineCaptcha lineCaptcha; + // 鎵洸骞叉壈楠岃瘉鐮� + @Resource(name = "ShearCaptcha") + private ShearCaptcha shearCaptcha; @Autowired private RedisCache redisCache; - + // 楠岃瘉鐮佺被鍨� - @Value("${ruoyi.captchaType}") + @Value("${captcha.captchaType}") private String captchaType; + // 楠岃瘉鐮佺被鍒� + @Value("${captcha.captchaCategory}") + private String captchaCategory; + // 鏁板瓧楠岃瘉鐮佷綅鏁� + @Value("${captcha.captchaNumberLength}") + private int numberLength; + // 瀛楃楠岃瘉鐮侀暱搴� + @Value("${captcha.captchaCharLength}") + private int charLength; /** * 鐢熸垚楠岃瘉鐮� */ @GetMapping("/captchaImage") - public AjaxResult getCode(HttpServletResponse response) throws IOException - { + public AjaxResult getCode() { // 淇濆瓨楠岃瘉鐮佷俊鎭� - String uuid = IdUtils.simpleUUID(); + String uuid = IdUtil.simpleUUID(); String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid; - String capStr = null, code = null; - BufferedImage image = null; - // 鐢熸垚楠岃瘉鐮� - if ("math".equals(captchaType)) - { - String capText = captchaProducerMath.createText(); - capStr = capText.substring(0, capText.lastIndexOf("@")); - code = capText.substring(capText.lastIndexOf("@") + 1); - image = captchaProducerMath.createImage(capStr); + CodeGenerator codeGenerator; + if ("math".equals(captchaType)) { + codeGenerator = new MathGenerator(numberLength); + } else if ("char".equals(captchaType)) { + codeGenerator = new RandomGenerator(charLength); + } else { + throw new IllegalArgumentException("楠岃瘉鐮佺被鍨嬪紓甯�"); } - else if ("char".equals(captchaType)) - { - capStr = code = captchaProducer.createText(); - image = captchaProducer.createImage(capStr); + if ("line".equals(captchaCategory)) { + lineCaptcha.setGenerator(codeGenerator); + capStr = lineCaptcha.getCode(); + } else if ("circle".equals(captchaCategory)) { + circleCaptcha.setGenerator(codeGenerator); + capStr = circleCaptcha.getCode(); + } else if ("shear".equals(captchaCategory)) { + shearCaptcha.setGenerator(codeGenerator); + capStr = shearCaptcha.getCode(); + } else { + throw new IllegalArgumentException("楠岃瘉鐮佺被鍒紓甯�"); + } + if ("math".equals(captchaType)) { + code = getCodeResult(capStr); + } else if ("char".equals(captchaType)) { + code = capStr; } redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES); - // 杞崲娴佷俊鎭啓鍑� - FastByteArrayOutputStream os = new FastByteArrayOutputStream(); - try - { - ImageIO.write(image, "jpg", os); - } - catch (IOException e) - { - return AjaxResult.error(e.getMessage()); - } + circleCaptcha.createImage(capStr); AjaxResult ajax = AjaxResult.success(); ajax.put("uuid", uuid); - ajax.put("img", Base64.encode(os.toByteArray())); + ajax.put("img", circleCaptcha.getImageBase64()); return ajax; } + + private String getCodeResult(String capStr) { + int a = Convert.toInt(StrUtil.sub(capStr, 0, numberLength).trim()); + char operator = capStr.charAt(numberLength); + int b = Convert.toInt(StrUtil.sub(capStr, numberLength + 1, numberLength + 1 + numberLength).trim()); + switch (operator) { + case '*': + return a * b + ""; + case '+': + return a + b + ""; + case '-': + return a - b + ""; + default: + return ""; + } + } + } diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index 2255d69..963614d 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -12,8 +12,16 @@ profile: ${user.dir}/ruoyi/uploadPath # 鑾峰彇ip鍦板潃寮�鍏� addressEnabled: false + +captcha: # 楠岃瘉鐮佺被鍨� math 鏁扮粍璁$畻 char 瀛楃楠岃瘉 captchaType: math + # line 绾挎骞叉壈 circle 鍦嗗湀骞叉壈 shear 鎵洸骞叉壈 + captchaCategory: circle + # 鏁板瓧楠岃瘉鐮佷綅鏁� + captchaNumberLength: 1 + # 瀛楃楠岃瘉鐮侀暱搴� + captchaCharLength: 4 # 寮�鍙戠幆澧冮厤缃� server: diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/VerifyCodeUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/VerifyCodeUtils.java deleted file mode 100644 index 5fdf3a9..0000000 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/VerifyCodeUtils.java +++ /dev/null @@ -1,228 +0,0 @@ -package com.ruoyi.common.utils; - -import java.awt.Color; -import java.awt.Font; -import java.awt.Graphics; -import java.awt.Graphics2D; -import java.awt.RenderingHints; -import java.awt.geom.AffineTransform; -import java.awt.image.BufferedImage; -import java.io.IOException; -import java.io.OutputStream; -import java.security.SecureRandom; -import java.util.Arrays; -import java.util.Random; -import javax.imageio.ImageIO; - -/** - * 楠岃瘉鐮佸伐鍏风被 - * - * @author ruoyi - */ -public class VerifyCodeUtils -{ - // 浣跨敤鍒癆lgerian瀛椾綋锛岀郴缁熼噷娌℃湁鐨勮瘽闇�瑕佸畨瑁呭瓧浣擄紝瀛椾綋鍙樉绀哄ぇ鍐欙紝鍘绘帀浜�1,0,i,o鍑犱釜瀹规槗娣锋穯鐨勫瓧绗� - public static final String VERIFY_CODES = "123456789ABCDEFGHJKLMNPQRSTUVWXYZ"; - - private static Random random = new SecureRandom(); - - /** - * 浣跨敤绯荤粺榛樿瀛楃婧愮敓鎴愰獙璇佺爜 - * - * @param verifySize 楠岃瘉鐮侀暱搴� - * @return - */ - public static String generateVerifyCode(int verifySize) - { - return generateVerifyCode(verifySize, VERIFY_CODES); - } - - /** - * 浣跨敤鎸囧畾婧愮敓鎴愰獙璇佺爜 - * - * @param verifySize 楠岃瘉鐮侀暱搴� - * @param sources 楠岃瘉鐮佸瓧绗︽簮 - * @return - */ - public static String generateVerifyCode(int verifySize, String sources) - { - if (sources == null || sources.length() == 0) - { - sources = VERIFY_CODES; - } - int codesLen = sources.length(); - Random rand = new Random(System.currentTimeMillis()); - StringBuilder verifyCode = new StringBuilder(verifySize); - for (int i = 0; i < verifySize; i++) - { - verifyCode.append(sources.charAt(rand.nextInt(codesLen - 1))); - } - return verifyCode.toString(); - } - - /** - * 杈撳嚭鎸囧畾楠岃瘉鐮佸浘鐗囨祦 - * - * @param w - * @param h - * @param os - * @param code - * @throws IOException - */ - public static void outputImage(int w, int h, OutputStream os, String code) throws IOException - { - int verifySize = code.length(); - BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); - Random rand = new Random(); - Graphics2D g2 = image.createGraphics(); - g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - Color[] colors = new Color[5]; - Color[] colorSpaces = new Color[] { Color.WHITE, Color.CYAN, Color.GRAY, Color.LIGHT_GRAY, Color.MAGENTA, - Color.ORANGE, Color.PINK, Color.YELLOW }; - float[] fractions = new float[colors.length]; - for (int i = 0; i < colors.length; i++) - { - colors[i] = colorSpaces[rand.nextInt(colorSpaces.length)]; - fractions[i] = rand.nextFloat(); - } - Arrays.sort(fractions); - - g2.setColor(Color.GRAY);// 璁剧疆杈规鑹� - g2.fillRect(0, 0, w, h); - - Color c = getRandColor(200, 250); - g2.setColor(c);// 璁剧疆鑳屾櫙鑹� - g2.fillRect(0, 2, w, h - 4); - - // 缁樺埗骞叉壈绾� - Random random = new Random(); - g2.setColor(getRandColor(160, 200));// 璁剧疆绾挎潯鐨勯鑹� - for (int i = 0; i < 20; i++) - { - int x = random.nextInt(w - 1); - int y = random.nextInt(h - 1); - int xl = random.nextInt(6) + 1; - int yl = random.nextInt(12) + 1; - g2.drawLine(x, y, x + xl + 40, y + yl + 20); - } - - // 娣诲姞鍣偣 - float yawpRate = 0.05f;// 鍣0鐜� - int area = (int) (yawpRate * w * h); - for (int i = 0; i < area; i++) - { - int x = random.nextInt(w); - int y = random.nextInt(h); - int rgb = getRandomIntColor(); - image.setRGB(x, y, rgb); - } - - shear(g2, w, h, c);// 浣垮浘鐗囨壄鏇� - - g2.setColor(getRandColor(100, 160)); - int fontSize = h - 4; - Font font = new Font("Algerian", Font.ITALIC, fontSize); - g2.setFont(font); - char[] chars = code.toCharArray(); - for (int i = 0; i < verifySize; i++) - { - AffineTransform affine = new AffineTransform(); - affine.setToRotation(Math.PI / 4 * rand.nextDouble() * (rand.nextBoolean() ? 1 : -1), - (w / verifySize) * i + fontSize / 2, h / 2); - g2.setTransform(affine); - g2.drawChars(chars, i, 1, ((w - 10) / verifySize) * i + 5, h / 2 + fontSize / 2 - 10); - } - - g2.dispose(); - ImageIO.write(image, "jpg", os); - } - - private static Color getRandColor(int fc, int bc) - { - if (fc > 255) { - fc = 255; - } - if (bc > 255) { - bc = 255; - } - int r = fc + random.nextInt(bc - fc); - int g = fc + random.nextInt(bc - fc); - int b = fc + random.nextInt(bc - fc); - return new Color(r, g, b); - } - - private static int getRandomIntColor() - { - int[] rgb = getRandomRgb(); - int color = 0; - for (int c : rgb) - { - color = color << 8; - color = color | c; - } - return color; - } - - private static int[] getRandomRgb() - { - int[] rgb = new int[3]; - for (int i = 0; i < 3; i++) - { - rgb[i] = random.nextInt(255); - } - return rgb; - } - - private static void shear(Graphics g, int w1, int h1, Color color) - { - shearX(g, w1, h1, color); - shearY(g, w1, h1, color); - } - - private static void shearX(Graphics g, int w1, int h1, Color color) - { - - int period = random.nextInt(2); - - boolean borderGap = true; - int frames = 1; - int phase = random.nextInt(2); - - for (int i = 0; i < h1; i++) - { - double d = (double) (period >> 1) - * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames); - g.copyArea(0, i, w1, 1, (int) d, 0); - if (borderGap) - { - g.setColor(color); - g.drawLine((int) d, i, 0, i); - g.drawLine((int) d + w1, i, w1, i); - } - } - - } - - private static void shearY(Graphics g, int w1, int h1, Color color) - { - - int period = random.nextInt(40) + 10; // 50; - - boolean borderGap = true; - int frames = 20; - int phase = 7; - for (int i = 0; i < w1; i++) - { - double d = (double) (period >> 1) - * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames); - g.copyArea(i, 0, 1, h1, 0, (int) d); - if (borderGap) - { - g.setColor(color); - g.drawLine(i, (int) d, i, 0); - g.drawLine(i, (int) d + h1, i, h1); - } - - } - } -} \ No newline at end of file diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java index 86141df..2d1bc95 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUploadUtils.java @@ -2,6 +2,8 @@ import java.io.File; import java.io.IOException; + +import cn.hutool.core.util.IdUtil; import org.apache.commons.io.FilenameUtils; import org.springframework.web.multipart.MultipartFile; import com.ruoyi.common.config.RuoYiConfig; @@ -11,7 +13,6 @@ import com.ruoyi.common.exception.file.InvalidExtensionException; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.StringUtils; -import com.ruoyi.common.utils.uuid.IdUtils; /** * 鏂囦欢涓婁紶宸ュ叿绫� @@ -123,7 +124,7 @@ { String fileName = file.getOriginalFilename(); String extension = getExtension(file); - fileName = DateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + extension; + fileName = DateUtils.datePath() + "/" + IdUtil.fastUUID() + "." + extension; return fileName; } diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/uuid/IdUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/uuid/IdUtils.java deleted file mode 100644 index 0d14f57..0000000 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/uuid/IdUtils.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.ruoyi.common.utils.uuid; - -import com.ruoyi.common.utils.uuid.UUID; - -/** - * ID鐢熸垚鍣ㄥ伐鍏风被 - * - * @author ruoyi - */ -public class IdUtils -{ - /** - * 鑾峰彇闅忔満UUID - * - * @return 闅忔満UUID - */ - public static String randomUUID() - { - return UUID.randomUUID().toString(); - } - - /** - * 绠�鍖栫殑UUID锛屽幓鎺変簡妯嚎 - * - * @return 绠�鍖栫殑UUID锛屽幓鎺変簡妯嚎 - */ - public static String simpleUUID() - { - return UUID.randomUUID().toString(true); - } - - /** - * 鑾峰彇闅忔満UUID锛屼娇鐢ㄦ�ц兘鏇村ソ鐨凾hreadLocalRandom鐢熸垚UUID - * - * @return 闅忔満UUID - */ - public static String fastUUID() - { - return UUID.fastUUID().toString(); - } - - /** - * 绠�鍖栫殑UUID锛屽幓鎺変簡妯嚎锛屼娇鐢ㄦ�ц兘鏇村ソ鐨凾hreadLocalRandom鐢熸垚UUID - * - * @return 绠�鍖栫殑UUID锛屽幓鎺変簡妯嚎 - */ - public static String fastSimpleUUID() - { - return UUID.fastUUID().toString(true); - } -} diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/uuid/UUID.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/uuid/UUID.java deleted file mode 100644 index 062d633..0000000 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/uuid/UUID.java +++ /dev/null @@ -1,484 +0,0 @@ -package com.ruoyi.common.utils.uuid; - -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; -import java.util.Random; -import java.util.concurrent.ThreadLocalRandom; -import com.ruoyi.common.exception.UtilException; - -/** - * 鎻愪緵閫氱敤鍞竴璇嗗埆鐮侊紙universally unique identifier锛夛紙UUID锛夊疄鐜� - * - * @author ruoyi - */ -public final class UUID implements java.io.Serializable, Comparable<UUID> -{ - private static final long serialVersionUID = -1185015143654744140L; - - /** - * SecureRandom 鐨勫崟渚� - * - */ - private static class Holder - { - static final SecureRandom numberGenerator = getSecureRandom(); - } - - /** 姝UID鐨勬渶楂�64鏈夋晥浣� */ - private final long mostSigBits; - - /** 姝UID鐨勬渶浣�64鏈夋晥浣� */ - private final long leastSigBits; - - /** - * 绉佹湁鏋勯�� - * - * @param data 鏁版嵁 - */ - private UUID(byte[] data) - { - long msb = 0; - long lsb = 0; - assert data.length == 16 : "data must be 16 bytes in length"; - for (int i = 0; i < 8; i++) - { - msb = (msb << 8) | (data[i] & 0xff); - } - for (int i = 8; i < 16; i++) - { - lsb = (lsb << 8) | (data[i] & 0xff); - } - this.mostSigBits = msb; - this.leastSigBits = lsb; - } - - /** - * 浣跨敤鎸囧畾鐨勬暟鎹瀯閫犳柊鐨� UUID銆� - * - * @param mostSigBits 鐢ㄤ簬 {@code UUID} 鐨勬渶楂樻湁鏁� 64 浣� - * @param leastSigBits 鐢ㄤ簬 {@code UUID} 鐨勬渶浣庢湁鏁� 64 浣� - */ - public UUID(long mostSigBits, long leastSigBits) - { - this.mostSigBits = mostSigBits; - this.leastSigBits = leastSigBits; - } - - /** - * 鑾峰彇绫诲瀷 4锛堜吉闅忔満鐢熸垚鐨勶級UUID 鐨勯潤鎬佸伐鍘傘�� 浣跨敤鍔犲瘑鐨勬湰鍦扮嚎绋嬩吉闅忔満鏁扮敓鎴愬櫒鐢熸垚璇� UUID銆� - * - * @return 闅忔満鐢熸垚鐨� {@code UUID} - */ - public static UUID fastUUID() - { - return randomUUID(false); - } - - /** - * 鑾峰彇绫诲瀷 4锛堜吉闅忔満鐢熸垚鐨勶級UUID 鐨勯潤鎬佸伐鍘傘�� 浣跨敤鍔犲瘑鐨勫己浼殢鏈烘暟鐢熸垚鍣ㄧ敓鎴愯 UUID銆� - * - * @return 闅忔満鐢熸垚鐨� {@code UUID} - */ - public static UUID randomUUID() - { - return randomUUID(true); - } - - /** - * 鑾峰彇绫诲瀷 4锛堜吉闅忔満鐢熸垚鐨勶級UUID 鐨勯潤鎬佸伐鍘傘�� 浣跨敤鍔犲瘑鐨勫己浼殢鏈烘暟鐢熸垚鍣ㄧ敓鎴愯 UUID銆� - * - * @param isSecure 鏄惁浣跨敤{@link SecureRandom}濡傛灉鏄彲浠ヨ幏寰楁洿瀹夊叏鐨勯殢鏈虹爜锛屽惁鍒欏彲浠ュ緱鍒版洿濂界殑鎬ц兘 - * @return 闅忔満鐢熸垚鐨� {@code UUID} - */ - public static UUID randomUUID(boolean isSecure) - { - final Random ng = isSecure ? Holder.numberGenerator : getRandom(); - - byte[] randomBytes = new byte[16]; - ng.nextBytes(randomBytes); - randomBytes[6] &= 0x0f; /* clear version */ - randomBytes[6] |= 0x40; /* set to version 4 */ - randomBytes[8] &= 0x3f; /* clear variant */ - randomBytes[8] |= 0x80; /* set to IETF variant */ - return new UUID(randomBytes); - } - - /** - * 鏍规嵁鎸囧畾鐨勫瓧鑺傛暟缁勮幏鍙栫被鍨� 3锛堝熀浜庡悕绉扮殑锛塙UID 鐨勯潤鎬佸伐鍘傘�� - * - * @param name 鐢ㄤ簬鏋勯�� UUID 鐨勫瓧鑺傛暟缁勩�� - * - * @return 鏍规嵁鎸囧畾鏁扮粍鐢熸垚鐨� {@code UUID} - */ - public static UUID nameUUIDFromBytes(byte[] name) - { - MessageDigest md; - try - { - md = MessageDigest.getInstance("MD5"); - } - catch (NoSuchAlgorithmException nsae) - { - throw new InternalError("MD5 not supported"); - } - byte[] md5Bytes = md.digest(name); - md5Bytes[6] &= 0x0f; /* clear version */ - md5Bytes[6] |= 0x30; /* set to version 3 */ - md5Bytes[8] &= 0x3f; /* clear variant */ - md5Bytes[8] |= 0x80; /* set to IETF variant */ - return new UUID(md5Bytes); - } - - /** - * 鏍规嵁 {@link #toString()} 鏂规硶涓弿杩扮殑瀛楃涓叉爣鍑嗚〃绀哄舰寮忓垱寤簕@code UUID}銆� - * - * @param name 鎸囧畾 {@code UUID} 瀛楃涓� - * @return 鍏锋湁鎸囧畾鍊肩殑 {@code UUID} - * @throws IllegalArgumentException 濡傛灉 name 涓� {@link #toString} 涓弿杩扮殑瀛楃涓茶〃绀哄舰寮忎笉绗︽姏鍑烘寮傚父 - * - */ - public static UUID fromString(String name) - { - String[] components = name.split("-"); - if (components.length != 5) - { - throw new IllegalArgumentException("Invalid UUID string: " + name); - } - for (int i = 0; i < 5; i++) - { - components[i] = "0x" + components[i]; - } - - long mostSigBits = Long.decode(components[0]).longValue(); - mostSigBits <<= 16; - mostSigBits |= Long.decode(components[1]).longValue(); - mostSigBits <<= 16; - mostSigBits |= Long.decode(components[2]).longValue(); - - long leastSigBits = Long.decode(components[3]).longValue(); - leastSigBits <<= 48; - leastSigBits |= Long.decode(components[4]).longValue(); - - return new UUID(mostSigBits, leastSigBits); - } - - /** - * 杩斿洖姝� UUID 鐨� 128 浣嶅�间腑鐨勬渶浣庢湁鏁� 64 浣嶃�� - * - * @return 姝� UUID 鐨� 128 浣嶅�间腑鐨勬渶浣庢湁鏁� 64 浣嶃�� - */ - public long getLeastSignificantBits() - { - return leastSigBits; - } - - /** - * 杩斿洖姝� UUID 鐨� 128 浣嶅�间腑鐨勬渶楂樻湁鏁� 64 浣嶃�� - * - * @return 姝� UUID 鐨� 128 浣嶅�间腑鏈�楂樻湁鏁� 64 浣嶃�� - */ - public long getMostSignificantBits() - { - return mostSigBits; - } - - /** - * 涓庢 {@code UUID} 鐩稿叧鑱旂殑鐗堟湰鍙�. 鐗堟湰鍙锋弿杩版 {@code UUID} 鏄浣曠敓鎴愮殑銆� - * <p> - * 鐗堟湰鍙峰叿鏈変互涓嬪惈鎰�: - * <ul> - * <li>1 鍩轰簬鏃堕棿鐨� UUID - * <li>2 DCE 瀹夊叏 UUID - * <li>3 鍩轰簬鍚嶇О鐨� UUID - * <li>4 闅忔満鐢熸垚鐨� UUID - * </ul> - * - * @return 姝� {@code UUID} 鐨勭増鏈彿 - */ - public int version() - { - // Version is bits masked by 0x000000000000F000 in MS long - return (int) ((mostSigBits >> 12) & 0x0f); - } - - /** - * 涓庢 {@code UUID} 鐩稿叧鑱旂殑鍙樹綋鍙枫�傚彉浣撳彿鎻忚堪 {@code UUID} 鐨勫竷灞�銆� - * <p> - * 鍙樹綋鍙峰叿鏈変互涓嬪惈鎰忥細 - * <ul> - * <li>0 涓� NCS 鍚戝悗鍏煎淇濈暀 - * <li>2 <a href="http://www.ietf.org/rfc/rfc4122.txt">IETF RFC 4122</a>(Leach-Salz), 鐢ㄤ簬姝ょ被 - * <li>6 淇濈暀锛屽井杞悜鍚庡吋瀹� - * <li>7 淇濈暀渚涗互鍚庡畾涔変娇鐢� - * </ul> - * - * @return 姝� {@code UUID} 鐩稿叧鑱旂殑鍙樹綋鍙� - */ - public int variant() - { - // This field is composed of a varying number of bits. - // 0 - - Reserved for NCS backward compatibility - // 1 0 - The IETF aka Leach-Salz variant (used by this class) - // 1 1 0 Reserved, Microsoft backward compatibility - // 1 1 1 Reserved for future definition. - return (int) ((leastSigBits >>> (64 - (leastSigBits >>> 62))) & (leastSigBits >> 63)); - } - - /** - * 涓庢 UUID 鐩稿叧鑱旂殑鏃堕棿鎴冲�笺�� - * - * <p> - * 60 浣嶇殑鏃堕棿鎴冲�兼牴鎹 {@code UUID} 鐨� time_low銆乼ime_mid 鍜� time_hi 瀛楁鏋勯�犮��<br> - * 鎵�寰楀埌鐨勬椂闂存埑浠� 100 姣井绉掍负鍗曚綅锛屼粠 UTC锛堥�氱敤鍗忚皟鏃堕棿锛� 1582 骞� 10 鏈� 15 鏃ラ浂鏃跺紑濮嬨�� - * - * <p> - * 鏃堕棿鎴冲�间粎鍦ㄥ湪鍩轰簬鏃堕棿鐨� UUID锛堝叾 version 绫诲瀷涓� 1锛変腑鎵嶆湁鎰忎箟銆�<br> - * 濡傛灉姝� {@code UUID} 涓嶆槸鍩轰簬鏃堕棿鐨� UUID锛屽垯姝ゆ柟娉曟姏鍑� UnsupportedOperationException銆� - * - * @throws UnsupportedOperationException 濡傛灉姝� {@code UUID} 涓嶆槸 version 涓� 1 鐨� UUID銆� - */ - public long timestamp() throws UnsupportedOperationException - { - checkTimeBase(); - return (mostSigBits & 0x0FFFL) << 48// - | ((mostSigBits >> 16) & 0x0FFFFL) << 32// - | mostSigBits >>> 32; - } - - /** - * 涓庢 UUID 鐩稿叧鑱旂殑鏃堕挓搴忓垪鍊笺�� - * - * <p> - * 14 浣嶇殑鏃堕挓搴忓垪鍊兼牴鎹 UUID 鐨� clock_seq 瀛楁鏋勯�犮�俢lock_seq 瀛楁鐢ㄤ簬淇濊瘉鍦ㄥ熀浜庢椂闂寸殑 UUID 涓殑鏃堕棿鍞竴鎬с�� - * <p> - * {@code clockSequence} 鍊间粎鍦ㄥ熀浜庢椂闂寸殑 UUID锛堝叾 version 绫诲瀷涓� 1锛変腑鎵嶆湁鎰忎箟銆� 濡傛灉姝� UUID 涓嶆槸鍩轰簬鏃堕棿鐨� UUID锛屽垯姝ゆ柟娉曟姏鍑� - * UnsupportedOperationException銆� - * - * @return 姝� {@code UUID} 鐨勬椂閽熷簭鍒� - * - * @throws UnsupportedOperationException 濡傛灉姝� UUID 鐨� version 涓嶄负 1 - */ - public int clockSequence() throws UnsupportedOperationException - { - checkTimeBase(); - return (int) ((leastSigBits & 0x3FFF000000000000L) >>> 48); - } - - /** - * 涓庢 UUID 鐩稿叧鐨勮妭鐐瑰�笺�� - * - * <p> - * 48 浣嶇殑鑺傜偣鍊兼牴鎹 UUID 鐨� node 瀛楁鏋勯�犮�傛瀛楁鏃ㄥ湪鐢ㄤ簬淇濆瓨鏈哄櫒鐨� IEEE 802 鍦板潃锛岃鍦板潃鐢ㄤ簬鐢熸垚姝� UUID 浠ヤ繚璇佺┖闂村敮涓�鎬с�� - * <p> - * 鑺傜偣鍊间粎鍦ㄥ熀浜庢椂闂寸殑 UUID锛堝叾 version 绫诲瀷涓� 1锛変腑鎵嶆湁鎰忎箟銆�<br> - * 濡傛灉姝� UUID 涓嶆槸鍩轰簬鏃堕棿鐨� UUID锛屽垯姝ゆ柟娉曟姏鍑� UnsupportedOperationException銆� - * - * @return 姝� {@code UUID} 鐨勮妭鐐瑰�� - * - * @throws UnsupportedOperationException 濡傛灉姝� UUID 鐨� version 涓嶄负 1 - */ - public long node() throws UnsupportedOperationException - { - checkTimeBase(); - return leastSigBits & 0x0000FFFFFFFFFFFFL; - } - - /** - * 杩斿洖姝@code UUID} 鐨勫瓧绗︿覆琛ㄧ幇褰㈠紡銆� - * - * <p> - * UUID 鐨勫瓧绗︿覆琛ㄧず褰㈠紡鐢辨 BNF 鎻忚堪锛� - * - * <pre> - * {@code - * UUID = <time_low>-<time_mid>-<time_high_and_version>-<variant_and_sequence>-<node> - * time_low = 4*<hexOctet> - * time_mid = 2*<hexOctet> - * time_high_and_version = 2*<hexOctet> - * variant_and_sequence = 2*<hexOctet> - * node = 6*<hexOctet> - * hexOctet = <hexDigit><hexDigit> - * hexDigit = [0-9a-fA-F] - * } - * </pre> - * - * </blockquote> - * - * @return 姝@code UUID} 鐨勫瓧绗︿覆琛ㄧ幇褰㈠紡 - * @see #toString(boolean) - */ - @Override - public String toString() - { - return toString(false); - } - - /** - * 杩斿洖姝@code UUID} 鐨勫瓧绗︿覆琛ㄧ幇褰㈠紡銆� - * - * <p> - * UUID 鐨勫瓧绗︿覆琛ㄧず褰㈠紡鐢辨 BNF 鎻忚堪锛� - * - * <pre> - * {@code - * UUID = <time_low>-<time_mid>-<time_high_and_version>-<variant_and_sequence>-<node> - * time_low = 4*<hexOctet> - * time_mid = 2*<hexOctet> - * time_high_and_version = 2*<hexOctet> - * variant_and_sequence = 2*<hexOctet> - * node = 6*<hexOctet> - * hexOctet = <hexDigit><hexDigit> - * hexDigit = [0-9a-fA-F] - * } - * </pre> - * - * </blockquote> - * - * @param isSimple 鏄惁绠�鍗曟ā寮忥紝绠�鍗曟ā寮忎负涓嶅甫'-'鐨刄UID瀛楃涓� - * @return 姝@code UUID} 鐨勫瓧绗︿覆琛ㄧ幇褰㈠紡 - */ - public String toString(boolean isSimple) - { - final StringBuilder builder = new StringBuilder(isSimple ? 32 : 36); - // time_low - builder.append(digits(mostSigBits >> 32, 8)); - if (false == isSimple) - { - builder.append('-'); - } - // time_mid - builder.append(digits(mostSigBits >> 16, 4)); - if (false == isSimple) - { - builder.append('-'); - } - // time_high_and_version - builder.append(digits(mostSigBits, 4)); - if (false == isSimple) - { - builder.append('-'); - } - // variant_and_sequence - builder.append(digits(leastSigBits >> 48, 4)); - if (false == isSimple) - { - builder.append('-'); - } - // node - builder.append(digits(leastSigBits, 12)); - - return builder.toString(); - } - - /** - * 杩斿洖姝� UUID 鐨勫搱甯岀爜銆� - * - * @return UUID 鐨勫搱甯岀爜鍊笺�� - */ - @Override - public int hashCode() - { - long hilo = mostSigBits ^ leastSigBits; - return ((int) (hilo >> 32)) ^ (int) hilo; - } - - /** - * 灏嗘瀵硅薄涓庢寚瀹氬璞℃瘮杈冦�� - * <p> - * 褰撲笖浠呭綋鍙傛暟涓嶄负 {@code null}銆佽�屾槸涓�涓� UUID 瀵硅薄銆佸叿鏈変笌姝� UUID 鐩稿悓鐨� varriant銆佸寘鍚浉鍚岀殑鍊硷紙姣忎竴浣嶅潎鐩稿悓锛夋椂锛岀粨鏋滄墠涓� {@code true}銆� - * - * @param obj 瑕佷笌涔嬫瘮杈冪殑瀵硅薄 - * - * @return 濡傛灉瀵硅薄鐩稿悓锛屽垯杩斿洖 {@code true}锛涘惁鍒欒繑鍥� {@code false} - */ - @Override - public boolean equals(Object obj) - { - if ((null == obj) || (obj.getClass() != UUID.class)) - { - return false; - } - UUID id = (UUID) obj; - return (mostSigBits == id.mostSigBits && leastSigBits == id.leastSigBits); - } - - // Comparison Operations - - /** - * 灏嗘 UUID 涓庢寚瀹氱殑 UUID 姣旇緝銆� - * - * <p> - * 濡傛灉涓や釜 UUID 涓嶅悓锛屼笖绗竴涓� UUID 鐨勬渶楂樻湁鏁堝瓧娈靛ぇ浜庣浜屼釜 UUID 鐨勫搴斿瓧娈碉紝鍒欑涓�涓� UUID 澶т簬绗簩涓� UUID銆� - * - * @param val 涓庢 UUID 姣旇緝鐨� UUID - * - * @return 鍦ㄦ UUID 灏忎簬銆佺瓑浜庢垨澶т簬 val 鏃讹紝鍒嗗埆杩斿洖 -1銆�0 鎴� 1銆� - * - */ - @Override - public int compareTo(UUID val) - { - // The ordering is intentionally set up so that the UUIDs - // can simply be numerically compared as two numbers - return (this.mostSigBits < val.mostSigBits ? -1 : // - (this.mostSigBits > val.mostSigBits ? 1 : // - (this.leastSigBits < val.leastSigBits ? -1 : // - (this.leastSigBits > val.leastSigBits ? 1 : // - 0)))); - } - - // ------------------------------------------------------------------------------------------------------------------- - // Private method start - /** - * 杩斿洖鎸囧畾鏁板瓧瀵瑰簲鐨刪ex鍊� - * - * @param val 鍊� - * @param digits 浣� - * @return 鍊� - */ - private static String digits(long val, int digits) - { - long hi = 1L << (digits * 4); - return Long.toHexString(hi | (val & (hi - 1))).substring(1); - } - - /** - * 妫�鏌ユ槸鍚︿负time-based鐗堟湰UUID - */ - private void checkTimeBase() - { - if (version() != 1) - { - throw new UnsupportedOperationException("Not a time-based UUID"); - } - } - - /** - * 鑾峰彇{@link SecureRandom}锛岀被鎻愪緵鍔犲瘑鐨勫己闅忔満鏁扮敓鎴愬櫒 (RNG) - * - * @return {@link SecureRandom} - */ - public static SecureRandom getSecureRandom() - { - try - { - return SecureRandom.getInstance("SHA1PRNG"); - } - catch (NoSuchAlgorithmException e) - { - throw new UtilException(e); - } - } - - /** - * 鑾峰彇闅忔満鏁扮敓鎴愬櫒瀵硅薄<br> - * ThreadLocalRandom鏄疛DK 7涔嬪悗鎻愪緵骞跺彂浜х敓闅忔満鏁帮紝鑳藉瑙e喅澶氫釜绾跨▼鍙戠敓鐨勭珵浜変簤澶恒�� - * - * @return {@link ThreadLocalRandom} - */ - public static ThreadLocalRandom getRandom() - { - return ThreadLocalRandom.current(); - } -} diff --git a/ruoyi-framework/pom.xml b/ruoyi-framework/pom.xml index 1100a29..d738880 100644 --- a/ruoyi-framework/pom.xml +++ b/ruoyi-framework/pom.xml @@ -55,18 +55,6 @@ <artifactId>druid-spring-boot-starter</artifactId> </dependency> - <!-- 楠岃瘉鐮� --> - <dependency> - <groupId>com.github.penggle</groupId> - <artifactId>kaptcha</artifactId> - <exclusions> - <exclusion> - <artifactId>javax.servlet-api</artifactId> - <groupId>javax.servlet</groupId> - </exclusion> - </exclusions> - </dependency> - <!-- 鑾峰彇绯荤粺淇℃伅 --> <dependency> <groupId>com.github.oshi</groupId> diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/CaptchaConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/CaptchaConfig.java index 43e78ae..4cd9997 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/CaptchaConfig.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/CaptchaConfig.java @@ -1,83 +1,55 @@ package com.ruoyi.framework.config; -import java.util.Properties; +import java.awt.*; + +import cn.hutool.captcha.*; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import com.google.code.kaptcha.impl.DefaultKaptcha; -import com.google.code.kaptcha.util.Config; -import static com.google.code.kaptcha.Constants.*; /** * 楠岃瘉鐮侀厤缃� - * - * @author ruoyi + * + * @author Lion Li */ @Configuration -public class CaptchaConfig -{ - @Bean(name = "captchaProducer") - public DefaultKaptcha getKaptchaBean() - { - DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); - Properties properties = new Properties(); - // 鏄惁鏈夎竟妗� 榛樿涓簍rue 鎴戜滑鍙互鑷繁璁剧疆yes锛宯o - properties.setProperty(KAPTCHA_BORDER, "yes"); - // 楠岃瘉鐮佹枃鏈瓧绗﹂鑹� 榛樿涓篊olor.BLACK - properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "black"); - // 楠岃瘉鐮佸浘鐗囧搴� 榛樿涓�200 - properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160"); - // 楠岃瘉鐮佸浘鐗囬珮搴� 榛樿涓�50 - properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60"); - // 楠岃瘉鐮佹枃鏈瓧绗﹀ぇ灏� 榛樿涓�40 - properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "38"); - // KAPTCHA_SESSION_KEY - properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCode"); - // 楠岃瘉鐮佹枃鏈瓧绗﹂暱搴� 榛樿涓�5 - properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4"); - // 楠岃瘉鐮佹枃鏈瓧浣撴牱寮� 榛樿涓簄ew Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize) - properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier"); - // 鍥剧墖鏍峰紡 姘寸汗com.google.code.kaptcha.impl.WaterRipple 楸肩溂com.google.code.kaptcha.impl.FishEyeGimpy 闃村奖com.google.code.kaptcha.impl.ShadowGimpy - properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy"); - Config config = new Config(properties); - defaultKaptcha.setConfig(config); - return defaultKaptcha; +public class CaptchaConfig { + + private final int width = 160; + private final int height = 60; + private final Color background = Color.PINK; + private final Font font = new Font("Arial", Font.BOLD, 48); + + /** + * 鍦嗗湀骞叉壈楠岃瘉鐮� + */ + @Bean(name = "CircleCaptcha") + public CircleCaptcha getCircleCaptcha() { + CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(width, height); + captcha.setBackground(background); + captcha.setFont(font); + return captcha; } - @Bean(name = "captchaProducerMath") - public DefaultKaptcha getKaptchaBeanMath() - { - DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); - Properties properties = new Properties(); - // 鏄惁鏈夎竟妗� 榛樿涓簍rue 鎴戜滑鍙互鑷繁璁剧疆yes锛宯o - properties.setProperty(KAPTCHA_BORDER, "yes"); - // 杈规棰滆壊 榛樿涓篊olor.BLACK - properties.setProperty(KAPTCHA_BORDER_COLOR, "105,179,90"); - // 楠岃瘉鐮佹枃鏈瓧绗﹂鑹� 榛樿涓篊olor.BLACK - properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "blue"); - // 楠岃瘉鐮佸浘鐗囧搴� 榛樿涓�200 - properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160"); - // 楠岃瘉鐮佸浘鐗囬珮搴� 榛樿涓�50 - properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60"); - // 楠岃瘉鐮佹枃鏈瓧绗﹀ぇ灏� 榛樿涓�40 - properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "35"); - // KAPTCHA_SESSION_KEY - properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCodeMath"); - // 楠岃瘉鐮佹枃鏈敓鎴愬櫒 - properties.setProperty(KAPTCHA_TEXTPRODUCER_IMPL, "com.ruoyi.framework.config.KaptchaTextCreator"); - // 楠岃瘉鐮佹枃鏈瓧绗﹂棿璺� 榛樿涓�2 - properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "3"); - // 楠岃瘉鐮佹枃鏈瓧绗﹂暱搴� 榛樿涓�5 - properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "6"); - // 楠岃瘉鐮佹枃鏈瓧浣撴牱寮� 榛樿涓簄ew Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize) - properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier"); - // 楠岃瘉鐮佸櫔鐐归鑹� 榛樿涓篊olor.BLACK - properties.setProperty(KAPTCHA_NOISE_COLOR, "white"); - // 骞叉壈瀹炵幇绫� - properties.setProperty(KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.NoNoise"); - // 鍥剧墖鏍峰紡 姘寸汗com.google.code.kaptcha.impl.WaterRipple 楸肩溂com.google.code.kaptcha.impl.FishEyeGimpy 闃村奖com.google.code.kaptcha.impl.ShadowGimpy - properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy"); - Config config = new Config(properties); - defaultKaptcha.setConfig(config); - return defaultKaptcha; + /** + * 绾挎骞叉壈鐨勯獙璇佺爜 + */ + @Bean(name = "LineCaptcha") + public LineCaptcha getLineCaptcha() { + LineCaptcha captcha = CaptchaUtil.createLineCaptcha(width, height); + captcha.setBackground(background); + captcha.setFont(font); + return captcha; } + + /** + * 鎵洸骞叉壈楠岃瘉鐮� + */ + @Bean(name = "ShearCaptcha") + public ShearCaptcha getShearCaptcha() { + ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(width, height); + captcha.setBackground(background); + captcha.setFont(font); + return captcha; + } + } diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/KaptchaTextCreator.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/KaptchaTextCreator.java deleted file mode 100644 index 3e74580..0000000 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/KaptchaTextCreator.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.ruoyi.framework.config; - -import java.util.Random; -import com.google.code.kaptcha.text.impl.DefaultTextCreator; - -/** - * 楠岃瘉鐮佹枃鏈敓鎴愬櫒 - * - * @author ruoyi - */ -public class KaptchaTextCreator extends DefaultTextCreator -{ - private static final String[] CNUMBERS = "0,1,2,3,4,5,6,7,8,9,10".split(","); - - @Override - public String getText() - { - Integer result = 0; - Random random = new Random(); - int x = random.nextInt(10); - int y = random.nextInt(10); - StringBuilder suChinese = new StringBuilder(); - int randomoperands = (int) Math.round(Math.random() * 2); - if (randomoperands == 0) - { - result = x * y; - suChinese.append(CNUMBERS[x]); - suChinese.append("*"); - suChinese.append(CNUMBERS[y]); - } - else if (randomoperands == 1) - { - if (!(x == 0) && y % x == 0) - { - result = y / x; - suChinese.append(CNUMBERS[y]); - suChinese.append("/"); - suChinese.append(CNUMBERS[x]); - } - else - { - result = x + y; - suChinese.append(CNUMBERS[x]); - suChinese.append("+"); - suChinese.append(CNUMBERS[y]); - } - } - else if (randomoperands == 2) - { - if (x >= y) - { - result = x - y; - suChinese.append(CNUMBERS[x]); - suChinese.append("-"); - suChinese.append(CNUMBERS[y]); - } - else - { - result = y - x; - suChinese.append(CNUMBERS[y]); - suChinese.append("-"); - suChinese.append(CNUMBERS[x]); - } - } - else - { - result = x + y; - suChinese.append(CNUMBERS[x]); - suChinese.append("+"); - suChinese.append(CNUMBERS[y]); - } - suChinese.append("=?@" + result); - return suChinese.toString(); - } -} \ No newline at end of file diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java index 0023855..89a81ad 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/TokenService.java @@ -4,6 +4,8 @@ import java.util.Map; import java.util.concurrent.TimeUnit; import javax.servlet.http.HttpServletRequest; + +import cn.hutool.core.util.IdUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @@ -14,7 +16,6 @@ import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.ip.AddressUtils; import com.ruoyi.common.utils.ip.IpUtils; -import com.ruoyi.common.utils.uuid.IdUtils; import eu.bitwalker.useragentutils.UserAgent; import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; @@ -101,7 +102,7 @@ */ public String createToken(LoginUser loginUser) { - String token = IdUtils.fastUUID(); + String token = IdUtil.fastUUID(); loginUser.setToken(token); setUserAgent(loginUser); refreshToken(loginUser); -- Gitblit v1.9.3