|
@@ -0,0 +1,150 @@
|
|
|
+package com.xt.js.gkaq.web.ctl.auth;
|
|
|
+
|
|
|
+import java.awt.Color;
|
|
|
+import java.awt.Font;
|
|
|
+import java.awt.Graphics2D;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.Random;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
+
|
|
|
+import com.xt.js.gkaq.common.BaseCtl;
|
|
|
+import com.xt.js.gkaq.common.GlobalData;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 控制器
|
|
|
+ *
|
|
|
+ * @author
|
|
|
+ */
|
|
|
+@Controller
|
|
|
+@RequestMapping(value = "/authCtl")
|
|
|
+public class AuthenticateController extends BaseCtl {
|
|
|
+
|
|
|
+ private Font font = new Font("Arial", Font.PLAIN, 18);
|
|
|
+ private int width = 60;
|
|
|
+ private int height = 20;
|
|
|
+
|
|
|
+ @RequestMapping(value = "checkcode")
|
|
|
+ @ResponseBody
|
|
|
+ public void checkcode(@RequestParam(value="checkcode",required=false) String checkcode,
|
|
|
+ HttpServletResponse response, HttpServletRequest request) {
|
|
|
+ try {
|
|
|
+ response.reset();
|
|
|
+ // 响应类型,即MIME类型
|
|
|
+ response.setContentType("image/jpeg");
|
|
|
+
|
|
|
+ // 不缓存
|
|
|
+ response.setHeader("Pragma", "No-cache");
|
|
|
+ response.setHeader("Cache-Control", "no-cache");
|
|
|
+ response.setDateHeader("Expires", 0);
|
|
|
+
|
|
|
+ BufferedImage image = new BufferedImage(width, height,
|
|
|
+ BufferedImage.TYPE_INT_RGB);
|
|
|
+ Graphics2D g = (Graphics2D) image.getGraphics();
|
|
|
+ Random random = new Random();
|
|
|
+ g.setColor(getRandColor(200, 250));
|
|
|
+ g.fillRect(0, 0, width, height);
|
|
|
+ g.setFont(font);
|
|
|
+ g.setColor(getRandColor(160, 230));
|
|
|
+
|
|
|
+ // 生成随机干扰线
|
|
|
+ for (int i = 0; i < 3; i++) {
|
|
|
+ g.setColor(getRandColor(100, 200));
|
|
|
+ int x = random.nextInt(5);
|
|
|
+ int y = random.nextInt(height);
|
|
|
+ int xl = width - random.nextInt(5);
|
|
|
+ int yl = random.nextInt(height);
|
|
|
+ g.drawLine(x, y, xl, yl);
|
|
|
+ // System.out.println("(" + x + "," + y + ")-(" + xl + "," + yl
|
|
|
+ // + ")");
|
|
|
+ }
|
|
|
+ // 生成随机字符
|
|
|
+ String sRand = "";
|
|
|
+ for (int i = 0; i < 4; i++) {
|
|
|
+ String rand = String.valueOf(random.nextInt(10));
|
|
|
+ sRand += rand;
|
|
|
+ }
|
|
|
+ for (int i = 0; i < sRand.length(); i++) {
|
|
|
+ String rand = sRand.substring(i, i + 1);
|
|
|
+ g.setColor(new Color(20 + random.nextInt(110), 20 + random
|
|
|
+ .nextInt(110), 20 + random.nextInt(110)));
|
|
|
+ g.drawString(rand, 13 * i + 6, 16);
|
|
|
+ }
|
|
|
+ // 将随机验证码放入session
|
|
|
+ request.getSession().setAttribute(
|
|
|
+ GlobalData.USER_SESSION_CHECK_CODE, checkcode);
|
|
|
+ // 关闭画笔
|
|
|
+ g.dispose();
|
|
|
+ // 写出
|
|
|
+ ImageIO.write(image, "JPEG", response.getOutputStream());
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成随机颜色
|
|
|
+ *
|
|
|
+ * @param fc
|
|
|
+ * @param bc
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Color getRandColor(int fc, int bc) {
|
|
|
+ Random random = new Random();
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用户手册下载
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @param response
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "downloadYhsc", method = RequestMethod.GET)
|
|
|
+ @ResponseBody
|
|
|
+ public void downloadYhsc(HttpServletResponse response,
|
|
|
+ HttpServletRequest request) throws Exception {
|
|
|
+ String filename = "";
|
|
|
+ String filepath = "";
|
|
|
+ filename = "江苏省港口安全监管与应急管理系统用户手册";
|
|
|
+ filepath = "/files/yhsc.pdf";
|
|
|
+ if (request.getHeader("USER-AGENT").toLowerCase().indexOf("firefox") > 0) { // 火狐浏览器
|
|
|
+ filename = new String(filename.getBytes("UTF-8"), "iso-8859-1");
|
|
|
+ } else {// ie浏览器
|
|
|
+ filename = URLEncoder.encode(filename, "UTF-8");
|
|
|
+ }
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ response.setContentType("multipart/form-data");
|
|
|
+ response.setHeader("Content-Disposition", "attachment;fileName="
|
|
|
+ + filename + ".pdf");
|
|
|
+ try {
|
|
|
+ OutputStream out = response.getOutputStream();
|
|
|
+ out.write(IOUtils.toByteArray(this.getClass().getResourceAsStream(
|
|
|
+ filepath)));
|
|
|
+ out.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|