国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 幫助中心 > 正文

Spring Security實現驗證碼登錄功能

2024-07-09 22:43:09
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了Spring Security實現驗證碼登錄功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

在spring security實現登錄注銷功能的基礎上進行開發。

1、添加生成驗證碼的控制器。

(1)、生成驗證碼

/**   * 引入 Security 配置屬性類   */  @Autowired  private SecurityProperties securityProperties;  @Override  public ImageCode createCode(HttpServletRequest request ) {    //如果請求中有 width 參數,則用請求中的,否則用 配置屬性中的    int width = ServletRequestUtils.getIntParameter(request,"width",securityProperties.getWidth());    //高度(寬度)    int height = ServletRequestUtils.getIntParameter(request,"height",securityProperties.getHeight());    //圖片驗證碼字符個數    int length = securityProperties.getLength();    //過期時間    int expireIn = securityProperties.getExpireIn();    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);    Graphics g = image.getGraphics();    Random random = new Random();    g.setColor(getRandColor(200, 250));    g.fillRect(0, 0, width, height);    g.setFont(new Font("Times New Roman", Font.ITALIC, 20));    g.setColor(getRandColor(160, 200));    for (int i = 0; i < 155; i++) {      int x = random.nextInt(width);      int y = random.nextInt(height);      int xl = random.nextInt(12);      int yl = random.nextInt(12);      g.drawLine(x, y, x + xl, y + yl);    }    String sRand = "";    for (int i = 0; i < length; i++) {      String rand = String.valueOf(random.nextInt(10));      sRand += rand;      g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));      g.drawString(rand, 13 * i + 6, 16);    }    g.dispose();    return new ImageCode(image, sRand, expireIn);  }  /**   * 生成隨機背景條紋   */  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);  }

(2)、驗證碼控制器

public static final String SESSION_KEY = "SESSION_KEY_IMAGE_CODE";  @Autowired  private ValidateCodeGenerator imageCodeGenerator;  /**   * Session 對象   */  private SessionStrategy sessionStrategy = new HttpSessionSessionStrategy();  @GetMapping("/code/image")  public void createCode(HttpServletRequest request, HttpServletResponse response) throws IOException {    ImageCode imageCode = imageCodeGenerator.createCode(request);    //將隨機數 放到Session中    sessionStrategy.setAttribute(new ServletWebRequest(request),SESSION_KEY,imageCode);    request.getSession().setAttribute(SESSION_KEY,imageCode);    //寫給response 響應    response.setHeader("Cache-Control", "no-store, no-cache");    response.setContentType("image/jpeg");    ImageIO.write(imageCode.getImage(),"JPEG",response.getOutputStream());  }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乐业县| 谷城县| 安图县| 探索| 威宁| 正蓝旗| 建湖县| 大港区| 蒲城县| 光泽县| 从江县| 万全县| 容城县| 论坛| 宁阳县| 中超| 大连市| 冀州市| 景洪市| 瑞丽市| 安龙县| 涟水县| 通道| 石首市| 洛浦县| 临颍县| 克什克腾旗| 封丘县| 盐城市| 安图县| 克东县| 黄龙县| 漾濞| 五常市| 上杭县| 高安市| 沽源县| 丽江市| 泰安市| 屯门区| 怀来县|