java实现验证码功能及上传服务器报错问题

场景:服务器上需要一个登录验证码功能

前端:extjs 后端:java 服务器:linux

先看后端生成验证码代码

controller代码

	/*
	 *获取图片验证码
	 */
	@GetMapping("/getMessage")
//	@CrossOrigin
	@NoSessionValidate
	public void getMessage(HttpServletResponse response, HttpServletRequest request){
		response.setHeader("Pragma","No-cache");
		response.setHeader("Cache-Control","No-cache");
		response.setDateHeader("Expires",0);
		response.setContentType("image/jpeg");
		String ip=request.getRemoteAddr();
		BufferedImage image= GetMessage.getMsg(request,ip);
		try {
			ImageIO.setUseCache(false);
			ImageIO.write(image,"JPG",response.getOutputStream());
			response.getOutputStream().flush();
			response.getOutputStream().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

service代码

package com.hand.hcf.app.blf.profile.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

//生成验证码,并将验证码存入session中,以ip为key
public class GetMessage {
    public static BufferedImage getMsg(HttpServletRequest request,String ip){
        int width = 60 , height = 30;
        //创建一个图像,宽60 高30
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        Random random = new Random();
        g.setColor(getRandomColor(200,250));
        g.fillRect(0,0,width,height);
        g.setFont(new Font("Times New Roman",Font.PLAIN,18));
        g.setColor(getRandomColor(160,200));
        //干扰线生成
        for (int i = 0; i < 10; 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 strCode = "";
        for (int i = 0; i < 4; i++) {
            String strNumber = String.valueOf(random.nextInt(10));
            strCode = strCode+strNumber;
            //设置字体颜色
            g.drawString(strNumber,13*i+6,20);
        }
//        System.out.println("当前ip"+ip);
//        System.out.println("当前验证码"+strCode);
        request.getSession().setAttribute(ip,strCode);
        HttpSession session = request.getSession();
        session.setAttribute("strCode",strCode);
        g.dispose();
        return image;
    }


    /**
     * 随机获取颜色的方法
     * @return
     */
    public static Color getRandomColor (int fc , int bc){
        Random random = new Random();
        Color reandomColor = null;
        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);
        reandomColor = new Color(r,g,b);
        return reandomColor;
    }
}

前端
html

 

js
为了防止服务器缓存,点击图片但是不进行刷新验证码,连接后面加上随机参数,但是后端不对其进行处理

    //验证码

    function takeCode() {
    	//为了防止服务器缓存,点击图片但是不进行刷新验证码,连接后面加上随机参数,但是后端不对其进行处理
        document.getElementById("strCodeImg").src = "/api/login/getMessage?r="+Math.random();
    }

成品
java实现验证码功能及上传服务器报错问题_第1张图片

但是部署到服务器上发生了报错,先看错误(由于报错太长,就列举了最重要的错误)

Caused by: java.lang.reflect.InvocationTargetException: null
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) ~[na:na]
	at java.desktop/sun.font.FontManagerFactory$1.run(FontManagerFactory.java:84) ~[na:na]
	... 130 common frames omitted
Caused by: java.lang.NullPointerException: null
	at java.desktop/sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1262) ~[na:na]
	at java.desktop/sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:225) ~[na:na]
	at java.desktop/sun.awt.FontConfiguration.init(FontConfiguration.java:107) ~[na:na]
	at java.desktop/sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:719) ~[na:na]
	at java.desktop/sun.font.SunFontManager$2.run(SunFontManager.java:379) ~[na:na]
	at java.base/java.security.AccessController.doPrivileged(Native Method) ~[na:na]
	at java.desktop/sun.font.SunFontManager.(SunFontManager.java:324) ~[na:na]
	at java.desktop/sun.awt.FcFontManager.(FcFontManager.java:35) ~[na:na]
	at java.desktop/sun.awt.X11FontManager.(X11FontManager.java:56) ~[na:na]
	... 135 common frames omitted

这个是字体问题,只需要在服务器上运行一下命令即可

yum install fontconfig
yum install -y fontconfig
yum install -y urw-fonts

你可能感兴趣的:(小功能,java,服务器,servlet)