JAVA 배경 인증 코드 생 성
4396 단어 JAVA
package com.zbmes.common.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class CaptchaUtils {
private static int width = 90;// width
private static int height = 30;// height
private static int codeCount = 4;//
private static int xx = 15;
private static int fontHeight = 20;//
private static int codeY = 24;//
private static char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6',
'7', '8', '9' };
/**
* map code codePic BufferedImage
*
* @return
*/
public static Map generateCodeAndPic() {
// buffer
BufferedImage buffImg = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// Graphics2D gd = buffImg.createGraphics();
// Graphics2D gd = (Graphics2D) buffImg.getGraphics();
Graphics gd = buffImg.getGraphics();
//
Random random = new Random();
//
gd.setColor(Color.WHITE);
gd.fillRect(0, 0, width, height);
// , 。
Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
// 。
gd.setFont(font);
// 。
gd.setColor(Color.BLACK);
gd.drawRect(0, 0, width - 1, height - 1);
// 20 , 。
gd.setColor(Color.BLACK);
for (int i = 0; i < 20; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
gd.drawLine(x, y, x + xl, y + yl);
}
// randomCode , 。
StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0;
// codeCount 。
for (int i = 0; i < codeCount; i++) {
// 。
String code = String.valueOf(codeSequence[random.nextInt(36)]);
// , 。
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);
// 。
gd.setColor(new Color(red, green, blue));
gd.drawString(code, (i + 1) * xx, codeY);
// 。
randomCode.append(code);
}
Map map = new HashMap();
//
map.put("code", randomCode);
// BufferedImage
map.put("codePic", buffImg);
return map;
}
/*
* public static void main(String[] args) throws Exception { //
* OutputStream out = new
* FileOutputStream("G://img"+System.currentTimeMillis()+".jpg");
* Map map = CaptchaUtils.generateCodeAndPic();
* ImageIO.write((RenderedImage) map.get("codePic"), "jpeg", out);
* System.out.println(" :"+map.get("code")); }
*/
}
인증 코드 를 session 에 저장 하고 로그 인 할 때 체크 섬 을 꺼 냅 니 다.
@RequestMapping("/captcha")
@ResponseBody
public void doGet(HttpServletRequest req, HttpServletResponse resp){
//
Map codeMap = CaptchaUtils.generateCodeAndPic();
// Session 。
HttpSession session = req.getSession();
session.setAttribute("captcha", codeMap.get("code").toString());
// 。
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", -1);
resp.setContentType("image/jpeg");
// Servlet 。
ServletOutputStream sos;
try {
sos = resp.getOutputStream();
ImageIO.write((RenderedImage) codeMap.get("codePic"), "jpeg", sos);
sos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JAVA 객체 작성 및 제거 방법정적 공장 방법 정적 공장 방법의 장점 를 반환할 수 있습니다. 정적 공장 방법의 단점 류 공유되거나 보호된 구조기를 포함하지 않으면 이불류화할 수 없음 여러 개의 구조기 파라미터를 만났을 때 구축기를 고려해야 한다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.