SSM整合Shiro(三)

本文详细介绍了如何在SSM项目中整合Shiro并利用Redis进行Session共享。主要内容包括引入相关jar包,自定义实现ByteSource接口以解决序列化问题,修改UserIdRealm.java和application-shiro.xml配置文件,以及解决登录重定向时路径解析的问题。通过两个Tomcat实例和Nginx的配合,展示了Session共享的效果。
摘要由CSDN通过智能技术生成

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())
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值