SSM整合Shiro(三)
Shiro+Redis实现Session共享
引入jar包
<!-- Shiro Redis start -->
<dependency>
<groupId>org.crazycake</groupId>
<artifactId>shiro-redis</artifactId>
<version>3.2.3</version>
</dependency>
<!-- Shiro Redis end -->
自己实现ByteSource接口!!!
这是一道送命题!!!
在集成Redis之前,Realm的认证方法中设置加密的盐时,用的Shiro提供的实现类SimpleByteSource它没有实现Serializable接口
,在集成Redis后序列化时会报错,所以需要自己仿照SimpleByteSource实现ByteSource接口,再实现Serializable。
package com.demo.utils;
import java.io.File;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Arrays;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.codec.CodecSupport;
import org.apache.shiro.codec.Hex;
import org.apache.shiro.util.ByteSource;
public class ShiroMySimpleByteSource implements ByteSource, Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private final byte[] bytes;
private String cachedHex;
private String cachedBase64;
public ShiroMySimpleByteSource(byte[] bytes) {
this.bytes = bytes;
}
public ShiroMySimpleByteSource(char[] chars) {
this.bytes = CodecSupport.toBytes(chars);
}
public ShiroMySimpleByteSource(String string) {
this.bytes = CodecSupport.toBytes(string);
}
public ShiroMySimpleByteSource(ByteSource source) {
this.bytes = source.getBytes();
}
public ShiroMySimpleByteSource(File file) {
this.bytes = new BytesHelper().getBytes(file);
}
public ShiroMySimpleByteSource(InputStream stream) {
this.bytes = new BytesHelper().getBytes(stream);
}
public static boolean isCompatible(Object o) {
return o instanceof byte[] || o instanceof char[] || o instanceof String || o instanceof ByteSource || o instanceof File || o instanceof InputStream;
}
public byte[] getBytes() {
return this.bytes;
}
public boolean isEmpty() {
return this.bytes == null || this.bytes.length == 0;
}
public String toHex() {
if (this.cachedHex == null) {
this.cachedHex = Hex.encodeToString(getBytes())