作者 shenhailong

添加 修改登录验证码为 数字加减

@@ -9,6 +9,7 @@ package com.agent.controller.system; @@ -9,6 +9,7 @@ package com.agent.controller.system;
9 9
10 import javax.annotation.Resource; 10 import javax.annotation.Resource;
11 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletRequest;
  12 +import javax.servlet.http.HttpSession;
12 13
13 14
14 import com.agent.entity.system.FunctionEntity; 15 import com.agent.entity.system.FunctionEntity;
@@ -152,13 +153,17 @@ public class LoginController { @@ -152,13 +153,17 @@ public class LoginController {
152 @Transactional 153 @Transactional
153 @RequestMapping(value = "/doLogin", method = RequestMethod.POST) 154 @RequestMapping(value = "/doLogin", method = RequestMethod.POST)
154 @ResponseBody 155 @ResponseBody
155 - public ResponseModel doLogin(String loginAccount, String password, String captcha) { 156 + public ResponseModel doLogin(String loginAccount, String password, String captcha, String verifyInput, HttpSession session) {
156 157
157 ResponseModel rm = new ResponseModel(200, "", null); 158 ResponseModel rm = new ResponseModel(200, "", null);
158 // 查询该账号是否被禁用 159 // 查询该账号是否被禁用
159 160
  161 +
  162 + String rand = session.getAttribute("rand").toString();
  163 +
160 UserEntity userEntity = userService.findLoginaccount(loginAccount); 164 UserEntity userEntity = userService.findLoginaccount(loginAccount);
161 165
  166 + if (rand.equals(verifyInput)){
162 if (userEntity.getStatus() != 0){ 167 if (userEntity.getStatus() != 0){
163 168
164 rm.setStatus(201); 169 rm.setStatus(201);
@@ -272,6 +277,11 @@ public class LoginController { @@ -272,6 +277,11 @@ public class LoginController {
272 return rm; 277 return rm;
273 } 278 }
274 } 279 }
  280 + }else {
  281 +
  282 + rm.setStatus(202);
  283 + return rm;
  284 + }
275 } 285 }
276 286
277 287
  1 +package com.agent.controller.system;
  2 +
  3 +import com.agent.util.VerifyCodeUtil;
  4 +import org.springframework.web.bind.annotation.RequestMapping;
  5 +import org.springframework.web.bind.annotation.RestController;
  6 +
  7 +import javax.imageio.ImageIO;
  8 +import javax.servlet.ServletException;
  9 +import javax.servlet.ServletOutputStream;
  10 +import javax.servlet.annotation.WebServlet;
  11 +import javax.servlet.http.HttpServlet;
  12 +import javax.servlet.http.HttpServletRequest;
  13 +import javax.servlet.http.HttpServletResponse;
  14 +import java.awt.*;
  15 +import java.awt.image.BufferedImage;
  16 +import java.io.IOException;
  17 +
  18 +@RestController
  19 +public class VerifyCodeController extends HttpServlet{
  20 +
  21 + /**
  22 + * Constructor of the object.
  23 + */
  24 + public VerifyCodeController() {
  25 + super();
  26 + }
  27 +
  28 + public void destroy() {
  29 + super.destroy();
  30 + }
  31 +
  32 +
  33 + @RequestMapping(value = "/getVerifyCode")
  34 + public void doGet(HttpServletRequest request, HttpServletResponse response)
  35 + throws ServletException, IOException {
  36 +
  37 + VerifyCodeUtil verifyCodeUtil = new VerifyCodeUtil();
  38 + try {
  39 + verifyCodeUtil.handleRequestInternal(request, response);
  40 + } catch (Exception e) {
  41 + e.printStackTrace();
  42 + }
  43 +
  44 + }
  45 +
  46 + public void doPost(HttpServletRequest request, HttpServletResponse response)
  47 + throws ServletException, IOException {
  48 + doGet(request,response);
  49 + }
  50 +
  51 + public void init() throws ServletException {
  52 + // Put your code here
  53 + }
  54 +
  55 +
  56 +
  57 +}
@@ -29,7 +29,7 @@ public class MyShiroController implements Filter { @@ -29,7 +29,7 @@ public class MyShiroController implements Filter {
29 } 29 }
30 30
31 private String publicAction[] = { "/login", "/doLogin", "/manifest/app/send", 31 private String publicAction[] = { "/login", "/doLogin", "/manifest/app/send",
32 - "/receipt/a0608c4054662dd902e1314f7e450e3eaa81c114", "/manifest/f5f5669bbdecefd3dacfaba194647c35" }; 32 + "/receipt/a0608c4054662dd902e1314f7e450e3eaa81c114", "/manifest/f5f5669bbdecefd3dacfaba194647c35", "/getVerifyCode" };
33 private String crossDomain[] = { "/cross/", "tracking/detail" }; 33 private String crossDomain[] = { "/cross/", "tracking/detail" };
34 34
35 // manifest/cross/grid.json 35 // manifest/cross/grid.json
  1 +package com.agent.util;
  2 +
  3 +import org.springframework.web.servlet.ModelAndView;
  4 +import org.springframework.web.servlet.mvc.AbstractController;
  5 +
  6 +import javax.imageio.ImageIO;
  7 +import javax.servlet.http.HttpServletRequest;
  8 +import javax.servlet.http.HttpServletResponse;
  9 +import javax.servlet.http.HttpSession;
  10 +import java.awt.*;
  11 +import java.awt.image.BufferedImage;
  12 +import java.util.Random;
  13 +
  14 +public class VerifyCodeUtil extends AbstractController{
  15 +
  16 +
  17 + @Override
  18 + public ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
  19 +
  20 + try {
  21 +
  22 + response.setContentType("image/jpeg");
  23 + response.setHeader("Pragma", "No-cache");
  24 + response.setHeader("Cache-Control", "no-cache");
  25 + response.setDateHeader("Expires", 0);
  26 + // 在内存中创建图象
  27 + int width = 59, height = 20;
  28 + BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  29 +
  30 + // 获取图形上下文
  31 + Graphics g = image.getGraphics();
  32 + // 生成随机类
  33 + Random random = new Random();
  34 + // 设定背景色
  35 + g.setColor(getRandColor(200, 250));
  36 + g.fillRect(0, 0, width, height);
  37 + // 设定字体
  38 + g.setFont(new Font("Arial", Font.PLAIN, 18));
  39 + // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
  40 + g.setColor(getRandColor(160, 200));
  41 + for (int i = 0; i < 155; i++) {
  42 + int x = random.nextInt(width);
  43 + int y = random.nextInt(height);
  44 + int xl = random.nextInt(12);
  45 + int yl = random.nextInt(12);
  46 + g.drawLine(x, y, x + xl, y + yl);
  47 + }
  48 + // 取随机产生的认证码(4位数字)
  49 + int sRand = 0;
  50 + // 是加法还是减法
  51 + int math = random.nextInt(2);
  52 + // 加法
  53 + if (math == 0) {
  54 + // 第一个数据
  55 + int rand = random.nextInt(10);
  56 + if (rand == 0) {
  57 + rand = 1;
  58 + }
  59 + g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
  60 + g.drawString("" + rand, 13 * 0 + 6, 16);
  61 + //
  62 + int rand1 = random.nextInt(10);
  63 + g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
  64 + g.drawString("" + rand1, 13 * 1 + 6, 16);
  65 + // +号
  66 + g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
  67 + g.drawString("+", 13 * 2 + 6, 16);
  68 + // 第二个数据
  69 + int rand2 = random.nextInt(10);
  70 + g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
  71 + g.drawString("" + rand2, 13 * 3 + 6, 16);
  72 + sRand = rand * 10 + rand1 + rand2;
  73 + } else {
  74 + // 减法
  75 + // 第一个数据
  76 + int rand = random.nextInt(10);
  77 + if (rand == 0) {
  78 + rand = 1;
  79 + }
  80 + g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
  81 + g.drawString("" + rand, 13 * 0 + 6, 16);
  82 + //
  83 + int rand1 = random.nextInt(10);
  84 + g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
  85 + g.drawString("" + rand1, 13 * 1 + 6, 16);
  86 + // -号
  87 + g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
  88 + g.drawString("-", 13 * 2 + 6 + 3, 16);
  89 + // 第二个数据
  90 + int rand2 = random.nextInt(10);
  91 + g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
  92 + g.drawString("" + rand2, 13 * 3 + 6, 16);
  93 + sRand = rand * 10 + rand1 - rand2;
  94 + }
  95 + synchronized (this) {
  96 + HttpSession session = request.getSession();
  97 + session.setAttribute("rand", "" + sRand);
  98 + System.out.println(sRand);
  99 + }
  100 + ImageIO.write(image, "jpg", response.getOutputStream());
  101 +
  102 + } catch (Exception e) {
  103 + // logger.error(e.getLocalizedMessage(), e.fillInStackTrace());
  104 + }
  105 + return null;
  106 + }
  107 +
  108 +
  109 + /*
  110 + * 获得颜色
  111 + */
  112 + private Color getRandColor(int fc, int bc) {
  113 +
  114 + Random random = new Random();
  115 +
  116 + if (fc > 255)
  117 + fc = 255;
  118 + if (bc > 255)
  119 + bc = 255;
  120 + int r = fc + random.nextInt(bc - fc - 16);//110+7
  121 + int g = fc + random.nextInt(bc - fc - 14);//110+9
  122 + int b = fc + random.nextInt(bc - fc - 18);//110+5
  123 + return new Color(r, g, b);
  124 + }
  125 +
  126 +}
@@ -20,12 +20,18 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request. @@ -20,12 +20,18 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.
20 <div style="font-size:16px;color:white;margin-left:510px;">&nbsp;郑州机场航空物流信息平台</div> 20 <div style="font-size:16px;color:white;margin-left:510px;">&nbsp;郑州机场航空物流信息平台</div>
21 </div> 21 </div>
22 <div class="login_box"> 22 <div class="login_box">
  23 + <div>
23 <input class="admin" type="text" placeholder="请输入账号" id="userName" maxlength="30" /> 24 <input class="admin" type="text" placeholder="请输入账号" id="userName" maxlength="30" />
24 - <input class="passWord" type="password" placeholder="请输入密码" id="password" onblur="regularValue()" maxlength="30" /> 25 + <input class="passWord" style="margin-bottom: 10px" type="password" placeholder="请输入密码" id="password" onblur="regularValue()" maxlength="30" /><span id="showStrength"></span>
  26 + </div>
  27 +
  28 +
  29 + <div style="margin-top: 10px;">
  30 + <input class="verifyInputs" style="float: left; width: 191px; height: 26px; line-height: 35px; margin-left: 0px; padding: 5px;" name="verifyInput" id="verifyInput" placeholder="请输入验证码">
25 31
  32 + <img class="verifyCodes" style="float: left; width: 202px; height: 37px; line-height: 35px; margin-left: 11px; padding: 5px; margin-top: -5px;" onclick="changeCode()" src="getVerifyCode">
26 33
27 - <%--<input type="text" name="valida" id="valida" οnkeydοwn="doEnter();">--%>  
28 - <%--<span><img src="<%=basePath %>verify/code" id="imgValida" οnclick="this.src='<%=basePath %>verify/code?a='+Math.random()" class="imgValida"></span>--%> 34 + </div>
29 35
30 <input class="submit" type="button" onclick="doLogin()"/> 36 <input class="submit" type="button" onclick="doLogin()"/>
31 37
@@ -62,6 +68,7 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request. @@ -62,6 +68,7 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.
62 function doLogin(){ 68 function doLogin(){
63 var userName = $("#userName").val(); 69 var userName = $("#userName").val();
64 var password = $("#password").val(); 70 var password = $("#password").val();
  71 + var verifyInput = $("#verifyInput").val();
65 72
66 if(userName==''){ 73 if(userName==''){
67 $("#userName").focus(); 74 $("#userName").focus();
@@ -78,13 +85,19 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request. @@ -78,13 +85,19 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.
78 }); 85 });
79 return false; 86 return false;
80 }else{ 87 }else{
81 - jQuery.post("<%=basePath %>doLogin",{loginAccount:userName,password:password},function(result){ 88 + jQuery.post("<%=basePath %>doLogin",{loginAccount:userName, password:password, verifyInput:verifyInput},function(result){
82 89
83 if (result.status == 201){ 90 if (result.status == 201){
84 $("#userName").focus(); 91 $("#userName").focus();
85 layer.tips('用户名已被禁用请联系管理员', '#userName', { 92 layer.tips('用户名已被禁用请联系管理员', '#userName', {
86 tips: [1, '#0FA6D8'] //还可配置颜色 93 tips: [1, '#0FA6D8'] //还可配置颜色
87 }); 94 });
  95 + } else if(result.status == 202){
  96 +
  97 + $("#verifyInput").focus();
  98 + layer.tips('验证码错误', '#verifyInput', {
  99 + tips: [1, '#0FA6D8'] //还可配置颜色
  100 + });
88 } else{ 101 } else{
89 if(result.status == 200){ 102 if(result.status == 200){
90 sessionStorage.removeItem("menuid"); 103 sessionStorage.removeItem("menuid");
@@ -116,22 +129,33 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request. @@ -116,22 +129,33 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.
116 } 129 }
117 130
118 function regularValue() { 131 function regularValue() {
119 - var passwords = $("#newpassword").val(); 132 + var passwords = $("#password").val();
120 133
121 var reg = /^(?![A-Za-z0-9]+$)(?![a-z0-9\W]+$)(?![A-Za-z\W]+$)(?![A-Z0-9\W]+$)[a-zA-Z0-9\W]{8,}$/; 134 var reg = /^(?![A-Za-z0-9]+$)(?![a-z0-9\W]+$)(?![A-Za-z\W]+$)(?![A-Z0-9\W]+$)[a-zA-Z0-9\W]{8,}$/;
122 135
123 var re = new RegExp(reg); 136 var re = new RegExp(reg);
124 137
125 if (re.test(passwords)){ 138 if (re.test(passwords)){
  139 + //符合规则  
  140 +// showStrength.innerHTML = "".fontcolor("green");
  141 +// document.getElementById("password").className = "icon ticker";
126 return true; 142 return true;
127 }else { 143 }else {
128 //不符合规则   144 //不符合规则  
129 - layer.tips('您的密码过于简单请及时修改', '#userName', { 145 + layer.tips('您的密码过于简单,请及时修改', '#password', {
130 tips: [1, '#0FA6D8'] //还可配置颜色 146 tips: [1, '#0FA6D8'] //还可配置颜色
131 }); 147 });
132 return false; 148 return false;
133 } 149 }
134 } 150 }
135 151
  152 +
  153 + function changeCode() {
  154 +
  155 + var src = " getVerifyCode?"+new Date().getTime(); //加时间戳,防止浏览器利用缓存
  156 +
  157 + $('.verifyCodes').attr("src",src);
  158 + }
  159 +
136 </script> 160 </script>
137 </html> 161 </html>
@@ -62,6 +62,8 @@ div,form,img,ul,li,input,p,ul,ol,li,form{margin:0px;padding:0px;border:0px; list @@ -62,6 +62,8 @@ div,form,img,ul,li,input,p,ul,ol,li,form{margin:0px;padding:0px;border:0px; list
62 /*border-radius: 5px; 62 /*border-radius: 5px;
63 behavior: url(css/ie-css3.htc);*/ 63 behavior: url(css/ie-css3.htc);*/
64 } 64 }
  65 +
  66 +
65 #login .login_box .passWord{ 67 #login .login_box .passWord{
66 float: left; 68 float: left;
67 width: 167px; 69 width: 167px;
@@ -70,9 +72,14 @@ div,form,img,ul,li,input,p,ul,ol,li,form{margin:0px;padding:0px;border:0px; list @@ -70,9 +72,14 @@ div,form,img,ul,li,input,p,ul,ol,li,form{margin:0px;padding:0px;border:0px; list
70 margin-left: 15px; 72 margin-left: 15px;
71 background: url(../img/icon_password.jpg) no-repeat 10px center #FFFFFF; 73 background: url(../img/icon_password.jpg) no-repeat 10px center #FFFFFF;
72 padding-left: 35px; 74 padding-left: 35px;
  75 +
73 /*border-radius: 5px; 76 /*border-radius: 5px;
74 behavior: url(css/ie-css3.htc);*/ 77 behavior: url(css/ie-css3.htc);*/
75 } 78 }
  79 +
  80 +
  81 +
  82 +
76 #login .login_box .submit{ 83 #login .login_box .submit{
77 float: left; 84 float: left;
78 margin-left: 15px; 85 margin-left: 15px;