diff --git a/src/main/java/com/lq/cms/web/LoginController.java b/src/main/java/com/lq/cms/web/LoginController.java index 3c90b64..97591d4 100644 --- a/src/main/java/com/lq/cms/web/LoginController.java +++ b/src/main/java/com/lq/cms/web/LoginController.java @@ -30,6 +30,7 @@ import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Map; import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; /**管理后台登陆页 * Created by qi on 2017/7/16. @@ -121,7 +122,7 @@ public class LoginController extends BaseController{ public void getAuthCode(HttpServletRequest request, HttpServletResponse response,HttpSession session)throws IOException{ int width=63; int height=37; - Random random=new Random(); + ThreadLocalRandom threadLocalRandom=ThreadLocalRandom.current(); //设置response头部信息 //禁止缓存 response.setHeader("Pragma","No-cache"); @@ -138,18 +139,18 @@ public class LoginController extends BaseController{ //绘制干扰线 for (int i=0;i<40;i++){ g.setColor(getRandColor(130,200)); - int x=random.nextInt(width); - int y=random.nextInt(height); - int x1=random.nextInt(12); - int y1=random.nextInt(12); + int x=threadLocalRandom.nextInt(width); + int y=threadLocalRandom.nextInt(height); + int x1=threadLocalRandom.nextInt(12); + int y1=threadLocalRandom.nextInt(12); g.drawLine(x,y,x+x1,y+y1); } //绘制字符 String strCode=""; for (int i=0;i<4;i++){ - String rand=String.valueOf(random.nextInt(10)); + String rand=String.valueOf(threadLocalRandom.nextInt(10)); strCode=strCode+rand; - g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110))); + g.setColor(new Color(20+threadLocalRandom.nextInt(110),20+threadLocalRandom.nextInt(110),20+threadLocalRandom.nextInt(110))); g.drawString(rand,13*i+6,28); } //将字符保存的session中用于前端的验证 diff --git a/src/main/java/com/lq/code/util/StringUtil.java b/src/main/java/com/lq/code/util/StringUtil.java index e502971..8cea1e9 100644 --- a/src/main/java/com/lq/code/util/StringUtil.java +++ b/src/main/java/com/lq/code/util/StringUtil.java @@ -2,6 +2,7 @@ package com.lq.code.util; import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; /** * Created by qi on 2017/8/13. @@ -55,8 +56,9 @@ public class StringUtil { * @return */ public static char randomChar(int startInt,int range){ - Random random=new Random(); - int randomNum=random.nextInt(range-1); + // Random random=new Random(); + ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current(); + int randomNum=threadLocalRandom.nextInt(range-1); int randomInt=randomNum+startInt; return (char)randomInt ; } @@ -94,8 +96,10 @@ public class StringUtil { public String randomString(int length,String needRule){ - Random random=new Random(); - random.nextInt(100); +// Random random=new Random(); + //jdk 1.7 以后用来取代Random,性能会有提升 + ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current(); + threadLocalRandom.nextInt(100); char a1='a'; int i=a1; diff --git a/src/main/resources/spring/spring-shiro.xml b/src/main/resources/spring/spring-shiro.xml index 3f7e66c..275f374 100644 --- a/src/main/resources/spring/spring-shiro.xml +++ b/src/main/resources/spring/spring-shiro.xml @@ -120,9 +120,10 @@ - - - + + + +