瀏覽代碼

登录验证码

sangwenwei 10 月之前
父節點
當前提交
e6cd436cf0

+ 235 - 0
src/main/java/com/jeeplus/modules/sys/utils/VerifyCodeUtils.java

@@ -0,0 +1,235 @@
+package com.jeeplus.modules.sys.utils;
+
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.geom.AffineTransform;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Arrays;
+import java.util.Random;
+
+/**
+ * 生成图片流
+ * @Description: Created by zcqshine on 2017/5/18.
+ */
+public class VerifyCodeUtils {
+    private static final String VERIFY_CODES = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
+    private static Random random = new Random();
+
+    /**
+     * 使用系统默认字符源生成验证码
+     * @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.trim().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 outputFile
+     * @param verifySize
+     * @return
+     */
+    public static String outputVerifyImage(int w, int h, File outputFile, int verifySize) throws IOException{
+        String verifyCode = generateVerifyCode(verifySize);
+        outputImage(w,h,outputFile,verifyCode);
+        return verifyCode;
+    }
+
+    /**
+     * 生成指定验证码图像文件
+     * @param w
+     * @param h
+     * @param outputFile
+     * @param verifyCode
+     * @throws IOException
+     */
+    public static void outputImage(int w, int h, File outputFile, String verifyCode) throws IOException{
+        if (outputFile == null){
+            return;
+        }
+        File dir = outputFile.getParentFile();
+        if(!dir.exists()){
+            dir.mkdirs();
+        }
+
+        try {
+            outputFile.createNewFile();
+            FileOutputStream fos = new FileOutputStream(outputFile);
+            outputImage(w, h, fos, verifyCode);
+            fos.close();
+        } catch (IOException e) {
+            throw e;
+        }
+    }
+
+    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;// 噪声率
+        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);
+            }
+        }
+    }
+}
+
+

+ 35 - 1
src/main/java/com/jeeplus/modules/sys/web/LoginController.java

@@ -3,6 +3,8 @@
  */
 package com.jeeplus.modules.sys.web;
 
+import cn.hutool.captcha.CaptchaUtil;
+import cn.hutool.captcha.LineCaptcha;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.google.common.collect.Lists;
@@ -41,6 +43,7 @@ import com.jeeplus.modules.sys.service.OfficeService;
 import com.jeeplus.modules.sys.service.SystemService;
 import com.jeeplus.modules.sys.utils.DictUtils;
 import com.jeeplus.modules.sys.utils.UserUtils;
+import com.jeeplus.modules.sys.utils.VerifyCodeUtils;
 import com.jeeplus.modules.sysuseroffice.entity.Useroffice;
 import com.jeeplus.modules.sysuseroffice.service.UserofficeService;
 import com.jeeplus.modules.szCenterservice.service.szCloud.SzFlowRequest;
@@ -58,6 +61,7 @@ import org.apache.shiro.authz.annotation.RequiresPermissions;
 import org.apache.shiro.web.util.SavedRequest;
 import org.apache.shiro.web.util.WebUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.PathVariable;
@@ -69,6 +73,7 @@ import redis.clients.jedis.Jedis;
 import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
 import java.io.IOException;
 import java.text.SimpleDateFormat;
 import java.util.*;
@@ -241,6 +246,35 @@ public class LoginController extends BaseController{
 	}
 
 	/**
+	 * 生成验证码
+	 * @param request
+	 * @param response
+	 */
+	@RequestMapping(value = "getVerifyCode")
+	public void getVerifyCode(HttpServletRequest request,HttpServletResponse response){
+		response.setHeader("Pragma","No-cache");
+		response.setHeader("Cache-Control", "no-cache");
+		response.setDateHeader("Expires", 0);
+		response.setContentType("image/jpeg");
+
+		//生成随机字符串
+		String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
+		//存入Session, 此处可以根据自己的需求
+		HttpSession session = request.getSession();
+		session.setAttribute("validateCode",verifyCode);
+		JedisUtils.getResource().set(ValidateCodeServlet.VALIDATE_CODE, verifyCode);
+		//生成图片
+		int w = 100, h = 34;
+		try {
+			//将图片写入到 response 的输出流即可将图片返回到客户端了
+			VerifyCodeUtils.outputImage(w, h , response.getOutputStream(), verifyCode);
+		} catch (IOException e) {
+			logger.error("生成验证码失败, Cause by: {}", e.getMessage(), e);
+		}
+	}
+
+
+	/**
 	 * 管理登录
 	 * @throws IOException
 	 */
@@ -423,7 +457,7 @@ public class LoginController extends BaseController{
 		if (clean){
 			loginFailMap.remove(useruame);
 		}
-		return loginFailNum >= 10;
+		return loginFailNum >= 5;
 	}
 
 

+ 16 - 0
src/main/java/com/jeeplus/modules/sys/web/RegisterController.java

@@ -361,6 +361,22 @@ public class RegisterController extends BaseController {
 		}*/
 	}
 
+	/**
+	 * 验证验证码是否正确
+	 * @param request
+	 * @param validateCode
+	 * @return
+	 */
+	@ResponseBody
+	@RequestMapping(value = "/checkValidateCode")
+	public String validateLoginName(HttpServletRequest request, @RequestParam String validateCode) {
+		String verifyCode = (String) request.getSession().getAttribute("validateCode");
+		if (verifyCode == null || !validateCode.equals(verifyCode)) {
+			return "false";
+		}
+		return "true";
+	}
+
 
 	/**
 	 * web端ajax验证手机验证码是否正确

+ 38 - 0
src/main/java/com/jeeplus/modules/workclientinfo/service/WorkClientInfoAllService.java

@@ -855,4 +855,42 @@ public class WorkClientInfoAllService extends CrudService<WorkClientInfoDao, Wor
         return work;
 
     }
+
+    /**
+     * 根据用户编码查询用户信息
+     * @param name
+     * @return
+     */
+    public WorkClientInfo getByName(String name) {
+        if(StringUtils.isBlank(name)){
+            return new WorkClientInfo();
+        }
+        WorkClientInfo workClientInfo = dao.getByName(name);
+        //获取一个客户所关联的联系人信息
+        if(workClientInfo != null){
+            workClientInfo.setWorkClientLinkmanList(workClientLinkmanDao.findList(new WorkClientLinkman(workClientInfo)));
+            //获取一个客户关联的银行信息
+            workClientInfo.setWorkClientBankList(workClientBankDao.findList(new WorkClientBank(workClientInfo)));
+            //获取附件信息
+            WorkClientAttachment workClientAttachment = new WorkClientAttachment();
+            workClientAttachment.setAttachmentId(workClientInfo.getId());
+            workClientAttachment.setAttachmentFlag(VarStr.attachmentFlag[0]);
+            workClientInfo.setWorkAttachments(workattachmentService.getAttachmentList(workClientAttachment));
+
+            //多个联系人
+            List<WorkClientLinkman> linkman = workClientLinkmanDao.findByClientId(workClientInfo.getId());
+//        workClientInfo.setWorkClientLinkman(linkman);
+            workClientInfo.setWorkClientLinkmanList(linkman);
+            WorkClientAttachment attchment = new WorkClientAttachment();
+            attchment.setAttachmentId(workClientInfo.getId());
+            List<WorkClientAttachment> attachments = workattachmentService.getAttachmentList(attchment);
+            workClientInfo.setWorkAttachments(attachments);
+
+            //添加当前文件服务器类型
+            workClientInfo.setUploadMode(uploadMode);
+            //数据处理(如果为阿里云文件服务器,则对查看的路径进行处理)
+            workattachmentService.clientAttachmentManageOnUrl(workClientInfo.getWorkAttachments());
+        }
+        return workClientInfo;
+    }
 }

+ 30 - 0
src/main/java/com/jeeplus/modules/workclientinfo/web/WorkClientInfoAllController.java

@@ -34,9 +34,13 @@ import com.jeeplus.modules.workreimbursement.utils.VarStr;
 import org.apache.shiro.authz.annotation.Logical;
 import org.apache.shiro.authz.annotation.RequiresPermissions;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.client.RestTemplate;
 import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.multipart.commons.CommonsMultipartFile;
 import org.springframework.web.servlet.mvc.support.RedirectAttributes;
@@ -61,6 +65,8 @@ public class WorkClientInfoAllController extends BaseController {
 	private WorkClientInfoAllService workClientInfoService;
 	@Autowired
 	private SysImportInfoService sysImportInfoService;
+	@Autowired
+	private RestTemplate restTemplate;
 
 
 	@Autowired
@@ -217,10 +223,34 @@ public class WorkClientInfoAllController extends BaseController {
 		}else{//新增表单保存
 			workClientInfoService.save(workClientInfo);//保存
 		}
+		//将新增的这条数据发送到cloud项目中,实现数据同步
+		WorkClientInfo clientInfo = workClientInfoService.getByName(workClientInfo.getName());
+		sendDataToCloud(clientInfo);
 		addMessage(redirectAttributes, "保存客户管理成功");
 		return "redirect:"+Global.getAdminPath()+"/workclientinfo/workClientInfoAll/?repage";
 	}
 
+	private void sendDataToCloud(WorkClientInfo workClientInfo) {
+		String pathFlagStr = Global.getConfig("SZ_PATH_flag");
+		boolean pathFlag = Boolean.parseBoolean(pathFlagStr);
+		if(pathFlag) {
+			String path = Global.getConfig("SZ_PATH");
+			WorkClientInfo info = new WorkClientInfo();
+			info.setId(workClientInfo.getId());
+			info.setName(workClientInfo.getName());
+			info.setHasUscc(workClientInfo.getHasUscc());
+			info.setUscCode(workClientInfo.getUscCode());
+			info.setRegisterAddress(workClientInfo.getRegisterAddress());
+			info.setTelephone(workClientInfo.getTelephone());
+			info.setCompanyIndustry(workClientInfo.getCompanyIndustry());
+			info.setWorkClientBankList(workClientInfo.getWorkClientBankList());
+			HttpHeaders headers = new HttpHeaders();
+			String url = path + "/ccpmData/data/saveClient";
+			HttpEntity<WorkClientInfo> httpEntity = new HttpEntity<>(info, headers);
+			restTemplate.exchange(url, HttpMethod.POST, httpEntity, WorkClientInfo.class);
+		}
+	}
+
 	/**
 	 * 保存客户和联系人类型
 	 */

+ 39 - 7
src/main/webapp/webpage/modules/sys/sysLogin.jsp

@@ -70,15 +70,23 @@
             $("#loginForm").validate({
                 rules: {
                     validateCode: {remote: "${pageContext.request.contextPath}/servlet/validateCodeServlet"}
+                    <%--validateCode: {remote: "${ctx}/sys/register/checkValidateCode"}--%>
                 },
                 messages: {
-                    username: {required: "请填写用户名."},password: {required: "请填写密码."},
-                    validateCode: {remote: "验证码不正确.", required: "请填写验证码."}
+                    // username: {required: "请填写用户名."},password: {required: "请填写密码."},
+                    validateCode: {remote: "验证码不正确!", required: "请填写验证码!"}
                 },
-                errorLabelContainer: "#messageBox",
+                // errorLabelContainer: "#messageBox",
                 errorPlacement: function(error, element) {
-                    error.appendTo($("#loginError").parent());
-                }
+                    // error.appendTo($("#errorMsg"));
+					$("#errorMsg").text(error[0].innerText)
+					$("#loginButton").attr("disabled", true);
+                },
+				success: function(error, element){
+                	console.log(error)
+					$("#errorMsg").text(error[0].innerText)
+					$("#loginButton").attr("disabled", false);
+				}
             });
 
             $('#loginButton').click(function () {
@@ -466,6 +474,7 @@
 	</script>
 
 	<style>
+
 		body,
 		html {
 			margin: 0;
@@ -531,6 +540,9 @@
 			color: red;
 			font-size: 12px;
 		}
+		.changeCode:hover{
+			color: red!important;
+		}
 	</style>
 	<%--<link id="layuicss-skincodecss" rel="stylesheet" href="./登录界面_files/code.css" media="all"></head>--%>
 <body class="login-layout " style="overflow:auto;">
@@ -556,7 +568,7 @@
 									<span>用户登录</span>
 								</div>
 								<div class="widget-body bound">
-									<div class="widget-main bound" style="height:300px;">
+									<div class="widget-main bound" style="height:350px;">
 										<form id="loginForm" class="form-signin" action="${ctx}/login" method="post" novalidate="novalidate">
 											<fieldset>
 												<div style="height:50px"></div>
@@ -574,6 +586,19 @@
 															</span>
 													<span id="span" class="help-inline"></span>
 												</label>
+												<c:if test="${isValidateCodeLogin}">
+													<label class="block clearfix">
+													<span class=" input-icon input-icon-right" style="display: flex">
+														<input type="text" style="width: 200px" id="validateCode" name="validateCode" placeholder="验证码">
+															<a style="text-decoration:none;" href="javascript:void(0);" rel="external nofollow" onclick="VerificationCode()">
+																<img style="margin-left: 5px" id="randCodeImage" alt="验证码" src="/getVerifyCode" width="100"/>
+																<span class="changeCode" style="font-size: 12px;font-weight: 200;color: dodgerblue">验证码刷新</span>
+															</a>
+													</span >
+														<span id="errorMsg" class="help-inline"></span>
+													</label>
+												</c:if>
+
 
 
 												<!-- 										<button type="button" id="getCompanyBtn" style="vertical-align:middle " class="dropdown" value="获取企业">我的企业</button>
@@ -941,7 +966,14 @@
 
 
 	});
-
+	/**
+	 *验证码刷新
+	 */
+	function VerificationCode(){
+		var rad = Math.floor(Math.random() * Math.pow(10, 8));
+		console.log("2312321")
+		$("#randCodeImage").attr("src", "/getVerifyCode?uuid="+rad);
+	}
 
 	<c:if test="${not empty message}">
         $("#span").empty();