使用 ThreadLocalRandom 代替 Random 在jdk 1.7 建议替代。(性能有所提升)

修复 修改密码不能立即生效的问题
master
星期八 6 years ago
parent 12bb3a48c3
commit 180f894d48
  1. 15
      src/main/java/com/lq/cms/web/LoginController.java
  2. 12
      src/main/java/com/lq/code/util/StringUtil.java
  3. 7
      src/main/resources/spring/spring-shiro.xml

@ -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中用于前端的验证

@ -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;

@ -120,9 +120,10 @@
<!--自定义授权和认证的Realm-->
<bean id="myRealm" class="com.lq.code.interceptor.shiro.ShiroRealm">
<!-- 启动认证缓存-->
<property name="authenticationCachingEnabled" value="true"/>
<property name="authenticationCacheName" value="authenticationCache"/>
<!-- 启动认证缓存 默认false-->
<property name="authenticationCachingEnabled" value="false"/>
<!-- 启动授权缓存 -->
<property name="authorizationCachingEnabled" value="false"/>
</bean>

Loading…
Cancel
Save