解决若依ruoyi项目部署到服务器验证码接口报错的问题

这里写目录标题

ruoyi-vue项目部署到服务器验证码接口报500的解决方案

问题回顾

最近手上有个项目是基于ruoyi-vue开发的,本地运行的好好地,一部署到服务器上,运行成功,一访问就发现验证码接口报错!!!就有了如下画面:

测试:验证码怎么不显示?报错了呀!

你:😳纳尼?我本地好好的呀,什么也没改怎么报错了呢!!!

运维:包是你打包给我的呀!

你:😭你把服务器项目错误日志发我看下吧!

报错信息

验证码接口返回错误响应如下:

json 复制代码
{
    "msg": null,
    "code": 500
}

服务器项目错误日志如下:

log 复制代码
[XNIO-1 task-1] ERROR c.h.f.w.e.GlobalExceptionHandler - [handleRuntimeException,93] - 请求地址'/captchaImage',发生未知异常.
java.lang.NullPointerException: null
	at sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1264)
	at sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:219)
	at sun.awt.FontConfiguration.init(FontConfiguration.java:107)
	at sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:774)
	at sun.font.SunFontManager$2.run(SunFontManager.java:441)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.font.SunFontManager.<init>(SunFontManager.java:386)
	at sun.awt.FcFontManager.<init>(FcFontManager.java:35)
	at sun.awt.X11FontManager.<init>(X11FontManager.java:57)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at java.lang.Class.newInstance(Class.java:442)
	at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:83)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:74)
	at java.awt.Font.getFont2D(Font.java:491)
	at java.awt.Font.access$000(Font.java:224)
	at java.awt.Font$FontAccessImpl.getFont2D(Font.java:228)
	at sun.font.FontUtilities.getFont2D(FontUtilities.java:200)
	at sun.font.StandardGlyphVector.initFontData(StandardGlyphVector.java:1126)
	at sun.font.StandardGlyphVector.init(StandardGlyphVector.java:1115)
	at sun.font.StandardGlyphVector.<init>(StandardGlyphVector.java:167)
	at java.awt.Font.createGlyphVector(Font.java:2549)
	at com.google.code.kaptcha.text.impl.DefaultWordRenderer.renderWord(DefaultWordRenderer.java:67)
	at com.google.code.kaptcha.impl.DefaultKaptcha.createImage(DefaultKaptcha.java:43)

分析和解决方案

根据报错信息,可以看出发生了NullPointerException异常,具体原因可能是在创建验证码图片时出现了空指针异常。报错信息中涉及到了字体管理相关的代码,所以极可能是在获取字体信息时出现了问题。

我们看到报错信息,可以看出问题出现在获取字体版本信息时:

log 复制代码
	at sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1264)

所以我们可以很明显的确定,服务器环境缺少相关字体文件或字体配置文件,导致无法获取字体版本信息;解决方法也很简单,缺少字体我们把字体安装上不就完事了,立马开干!

解决方案

这里我们操作的服务器是Centos7.9

我们先安装下字体库

bash 复制代码
# 更新软件包列表
sudo yum update
# 安装fontconfig字体库
sudo yum install fontconfig
# 安装ttf-dejavu字体
sudo yum install dejavu-sans-fonts

安装完成,我们查看下已经安装的字体库列表

bash 复制代码
fc-list

通过上面的命令我们可以看到刚安装上的DejaVu相关字体

安装完字体后,我们需要重启下项目服务,然后访问服务的验证码接口/captchaImage

响应结果:

json 复制代码
{
    "msg": "操作成功",
    "img": "/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAA8AKADASIAAhEBAxEB/8QAHwAAAQUBAQEB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VsVV1f/kCX/8A17Sf+gmvOl8LCXws5Ky/48rf/rmv8qsiuZi1q5iiSNUiIRQoyD2/GpP7fuv+ecP/AHyf8ayjWjZGcasbI6UU4VzP/CQ3f/POD/vk/wCNL/wkV3/zzg/75P8AjVe2iP20Tp9oPWm/Z0JzgVzf/CSXn/PKD/vk/wCNL/wkt5/zyg/75P8AjR7aIe2idUiBRxUorkf+Envf+eVv/wB8t/jS/wDCUXv/ADyt/wDvlv8AGj20Q9tE68U7aDXH/wDCVX3/ADyt/wDvlv8AGl/4Su+/55W3/fLf40e2iHtonWNaoxyVFTRwqnQVx3/CW3//ADxtv++W/wAaX/hL9Q/5423/AHy3/wAVR7aIe2idsBTwK4f/AITDUP8Anja/98t/8VS/8JlqP/PG1/75b/4qj20Q9tE7oCnbQa4T/hM9R/542v8A3w3/AMVS/wDCa6l/zwtP++G/+Ko9tEPbRO3NshOcCp40CjiuC/4TbUv+eFp/3w3/AMVS/wDCcan/AM8LT/vhv/iqPbRD20T0EVV1f/kB6h/17Sf+gmuK/wCE51P/AJ4Wn/fDf/FVHc+M9RurWa3eG1CSoyMVVsgEY4+aplWjZilVjZn/2Q==",
    "code": 200,
    "captchaEnabled": true,
    "uuid": "39ca65c4404a4bd8ab10f6671d4506a1"
}

到这里,完美解决!!!

相关推荐
武子康4 小时前
Java-01 深入浅出 MyBatis - MyBatis 概念 ORM映射关系 常见ORM 详细发展历史
java·数据库·sql·spring·mybatis·springboot·springcloud
chusheng18404 小时前
基于Java Web 的家乡特色菜推荐系统
java·开发语言·前端·springboot·家乡特色菜推荐系统·家乡特色菜推荐
sun_star1chen5 小时前
Springboot3.3.5 启动流程之 tomcat启动流程介绍
java·spring boot·tomcat·springboot
请叫我头头哥1 天前
SpringBoot进阶教程(八十三)Kaptcha
springboot
遇见你真好。2 天前
java根据时区转换获取时间的方法
java·springboot
NiNg_1_2342 天前
SpringSecurity入门
后端·spring·springboot·springsecurity
遇见你真好。2 天前
自定义注解进行数据脱敏
java·springboot
尚学教辅学习资料3 天前
基于微信小程序的电商平台+LW示例参考
java·微信小程序·小程序·毕业设计·springboot·电商平台