CodecException: Md5Hash implementation only supports conversion to byte[] if the source is of type[]

问题

Shiro 的md5+salt 加密时出现的错误.

Exception in thread "main" org.apache.shiro.codec.CodecException: The org.apache.shiro.crypto.hash.Md5Hash implementation only supports conversion to byte[] if the source is of type byte[], char[], String, org.apache.shiro.util.ByteSource File or InputStream.  The instance provided as a method argument is of type [java.util.UUID].  If you would like to convert this argument type to a byte[], you can 1) convert the argument to one of the supported types yourself and then use that as the method argument or 2) subclass org.apache.shiro.crypto.hash.Md5Hashand override the objectToBytes(Object o) method.
	at org.apache.shiro.codec.CodecSupport.objectToBytes(CodecSupport.java:308)
	at org.apache.shiro.codec.CodecSupport.toBytes(CodecSupport.java:212)
	at org.apache.shiro.crypto.hash.SimpleHash.toByteSource(SimpleHash.java:223)
	at org.apache.shiro.crypto.hash.SimpleHash.convertSaltToBytes(SimpleHash.java:206)
	at org.apache.shiro.crypto.hash.SimpleHash.<init>(SimpleHash.java:174)
	at org.apache.shiro.crypto.hash.SimpleHash.<init>(SimpleHash.java:139)
	at org.apache.shiro.crypto.hash.Md5Hash.<init>(Md5Hash.java:48)
	at com.wei.TestShiroMD5.main(TestShiroMD5.java:31)

原因

错误代码如下

// 使用md5
Md5Hash md5Hash = new Md5Hash("123");
System.out.println(md5Hash.toHex());  //202cb962ac59075b964b07152d234b70
UUID uuid = UUID.randomUUID();
Md5Hash md5Hash2 = new Md5Hash("123", uuid);
System.out.println(uuid);
System.out.println(md5Hash2.toHex());

本例中 传递了UUID 对象,作为Md5Hash的第二个参数.

解决

修改成如下代码即可:

Md5Hashmd5Hash = new Md5Hash("123");
System.out.println(md5Hash.toHex());  //202cb962ac59075b964b07152d234b70
String uuid = UUID.randomUUID().toString().replace("-", "");
Md5Hash md5Hash2 = new Md5Hash("123", uuid);
System.out.println(uuid);
System.out.println(md5Hash2.toHex());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nickkkkkkkkk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值