SecCodeController.java 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package com.zhcs.dt.controller.system.secCode;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics2D;
  5. import java.awt.font.FontRenderContext;
  6. import java.awt.geom.Rectangle2D;
  7. import java.awt.image.BufferedImage;
  8. import java.io.ByteArrayOutputStream;
  9. import java.io.IOException;
  10. import java.util.Random;
  11. import javax.imageio.ImageIO;
  12. import javax.servlet.ServletOutputStream;
  13. import javax.servlet.http.HttpServletResponse;
  14. import org.apache.shiro.SecurityUtils;
  15. import org.apache.shiro.session.Session;
  16. import org.apache.shiro.subject.Subject;
  17. import org.springframework.stereotype.Controller;
  18. import org.springframework.web.bind.annotation.RequestMapping;
  19. import com.zhcs.dt.util.Const;
  20. /**
  21. * 类名称:登录验证码
  22. * 类描述:
  23. * 作者单位: FHqq313596790
  24. * 联系方式:
  25. * @version
  26. */
  27. @Controller
  28. @RequestMapping("/code")
  29. public class SecCodeController {
  30. @RequestMapping
  31. public void generate(HttpServletResponse response){
  32. ByteArrayOutputStream output = new ByteArrayOutputStream();
  33. String code = drawImg(output);
  34. Subject currentUser = SecurityUtils.getSubject();
  35. Session session = currentUser.getSession();
  36. session.setAttribute(Const.SESSION_SECURITY_CODE, code);
  37. try {
  38. ServletOutputStream out = response.getOutputStream();
  39. output.writeTo(out);
  40. out.close();
  41. } catch (IOException e) {
  42. //e.printStackTrace();
  43. }
  44. }
  45. private String drawImg(ByteArrayOutputStream output){
  46. String code = "";
  47. for(int i=0; i<4; i++){
  48. code += randomChar();
  49. }
  50. int width = 70;
  51. int height = 25;
  52. BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR);
  53. Font font = new Font("Times New Roman",Font.PLAIN,20);
  54. Graphics2D g = bi.createGraphics();
  55. g.setFont(font);
  56. Color color = new Color(66,2,82);
  57. g.setColor(color);
  58. g.setBackground(new Color(226,226,240));
  59. g.clearRect(0, 0, width, height);
  60. FontRenderContext context = g.getFontRenderContext();
  61. Rectangle2D bounds = font.getStringBounds(code, context);
  62. double x = (width - bounds.getWidth()) / 2;
  63. double y = (height - bounds.getHeight()) / 2;
  64. double ascent = bounds.getY();
  65. double baseY = y - ascent;
  66. g.drawString(code, (int)x, (int)baseY);
  67. g.dispose();
  68. try {
  69. ImageIO.write(bi, "jpg", output);
  70. } catch (IOException e) {
  71. //e.printStackTrace();
  72. }
  73. return code;
  74. }
  75. private char randomChar(){
  76. Random r = new Random();
  77. String s = "ABCDEFGHJKLMNPRSTUVWXYZ0123456789";
  78. return s.charAt(r.nextInt(s.length()));
  79. }
  80. }