JAVA Web应用验证码显示无效问题

大量的JAVA Web应用采用打包成war包,在tomcat容器运行的部署方式。登录界面常常采用验证码校验方式过滤机器登录攻击。但是在linux操作系统环境下部署的服务时常遇到验证码无法显示的问题。

这类JAVA应用通常采用AWT绘图生成验证码,调用java.awt.*包。AWT会调用操作系统本地窗口资源绘图,linux如果没有进到X Windows,AWT就无法绘图,并在后台报错(报错信息通常类似于java.awt.AWTError: Can't connect to X11 window server using ':0' as the value of the DISPLAY variable.)。Windows系统对此就支持得很好。

java.awt.Color;
java.awt.Font;
java.awt.Graphics2D;
java.awt.image.BufferedImage;

等程序用到的包都无法正常使用。后台报错信息如下:

java.awt.AWTError: Can't connect to X11 window server using ':0' as the value of the DISPLAY variable.
	at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
	at sun.awt.X11GraphicsEnvironment.access$200(X11GraphicsEnvironment.java:65)
	at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:115)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.awt.X11GraphicsEnvironment.(X11GraphicsEnvironment.java:74)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:264)
	at java.awt.GraphicsEnvironment.createGE(GraphicsEnvironment.java:103)
	at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:82)
	at java.awt.image.BufferedImage.createGraphics(BufferedImage.java:1181)
	at com.iflytek.audit.common.util.CaptchaUtil.createCaptcha(CaptchaUtil.java:105)
	at com.iflytek.audit.controller.Login.captcha(Login.java:134)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
     ……………………………………………………..
java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:264)
	at java.awt.GraphicsEnvironment.createGE(GraphicsEnvironment.java:103)
	at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:82)
	at java.awt.image.BufferedImage.createGraphics(BufferedImage.java:1181)
	at com.iflytek.audit.common.util.CaptchaUtil.createCaptcha(CaptchaUtil.java:105)
	at com.iflytek.audit.controller.Login.captcha(Login.java:134)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)

      解决方法主要有以下几种(tomcat官方推荐第四种方式)。

1、修改JVM参数,增加

      JAVA_OPTS=”-Djava.awt.headless=true”

2、修改tomcat的catalina.sh脚本,在脚本文件内加入:

      CATALINA_OPTS=”$CATALINA_OPTS -Djava.awt.headless=true”

3、修改tomcat启动脚本startup.sh,在脚本文件内加入:

      export CATALINA_OPTS=”-Djava.awt.headless=true”

4、CATALINA_BASE/bin目录下创建setenv.sh脚本,在脚本文件内加入:

     CATALINA_OPTS=”-Djava.awt.headless=true”

 

注:

启动tomcat容器执行startup.sh脚本,脚本会执行语句

EXECUTABLE=catalina.sh,

从而执行catalina.sh脚本,catalina.sh脚本推荐将用户自定义的环境变量设置于setenv.sh脚本中

# Environment Variable Prerequisites

#   Do not set the variables in this script. Instead put them into a script

#   setenv.sh in CATALINA_BASE/bin to keep your customizations separate.

 

你可能感兴趣的:(JAVA Web应用验证码显示无效问题)