shiro在分布式项目中实现单点登录

本文介绍了如何在分布式项目中使用Shiro实现单点登录功能。通过引入Shiro的相关依赖,并配置RedisManager、RedisSessionDAO以及SerializeUtils等工具类,实现了session的持久化存储。同时,详细展示了ShiroConfig的配置过程,包括设置Redis连接信息和session管理策略。
摘要由CSDN通过智能技术生成

shiro在分布式项目中实现单点登录

Shiro的单点登录

使用shiro需要的依赖

org.crazycake shiro-redis 3.1.0
     <dependency>
        <groupId>org.apache.shiro</groupId>
        <artifactId>shiro-core</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.shiro</groupId>
        <artifactId>shiro-spring</artifactId>
        <version>1.3.2</version>
    </dependency>
    在使用shiro的单点登录时,需要使用到redismanager和redissessiondao 两个对于redis和session操作的两个类,这两个类可以使用org.crazycake包下的,也可以使用自定义的,下面讲解自定义的写法

redismanager:

package com.tt.springboottest.util;

import java.util.Set;

import org.springframework.beans.factory.annotation.Value;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisManager {
@Value("${spring.redis.host}")
private String host = “127.0.0.1”;

@Value("${spring.redis.port}")
private int port = 6379;

// 0 - never expire
private int expire = 0;

// timeout for jedis try to connect to redis server, not expire time! In
// milliseconds
@Value("${spring.redis.timeout}")
private int timeout = 0;

@Value("${spring.redis.password}")
private String password = "";

private static JedisPool jedisPool = null;

public RedisManager() {

}

/**
 * 初始化方法
 */
public void init() {
	if (jedisPool == null) {
		if (password != null && !"".equals(password)) {
			jedisPool = new JedisPool(new JedisPoolConfig(), host, port, timeout, password);
		} else if (timeout != 0) {
			jedisPool = new JedisPool(new JedisPoolConfig(), host, port, timeout);
		} else {
			jedisPool = new JedisPool(new JedisPoolConfig(), host, port);
		}

	}
}

/**
 * get value from redis
 *
 * @param key
 * @return
 */
public byte[] get(byte[] key) {
	byte[] value = null;
	Jedis jedis = jedisPool.getResource();
	try {
		value = jedis.get(key);
	} finally {
		if (jedis != null) {
			jedis.close();
		}
	}
	return value;
}

/**
 * set
 *
 * @param key
 * @param value
 * @return
 */
public byte[] set(byte[] key, byte[] value) {
	Jedis jedis = jedisPool.getResource();
	try {
		jedis.set(key, value);
		if (this.expire != 0) {
			jedis.expire(key, this.expire);
		}
	} finally {
		if (jedis != null) {
			jedis.close();
		}
	}
	return value;
}

/**
 * set
 *
 * @param key
 * @param value
 * @param expire
 * @return
 */
public byte[] set(byte[] key, byte[] value, int expire) {
	Jedis jedis = jedisPool.getResource();
	try {
		jedis.set(key, value);
		if (expire != 0) {
			jedis.expire(key, expire);
		}
	} finally {
		if (jedis != null) {
			jedis.close();
		}
	}
	return value;
}

/**
 * del
 *
 * @param key
 */
public void del(byte[] key) {
	Jedis jedis = jedisPool.getResource();
	try {
		jedis.del(key);
	} finally {
		if (jedis != null) {
			jedis.close();
		}
	}
}

/**
 * flush
 */
public void flushDB() {
	Jedis jedis = jedisPool.getResource();
	try {
		jedis.flushDB();
	} finally {
		if (jedis != null) {
			jedis.close();
		}
	}
}

/**
 * size
 */
public Long dbSize() {
	Long dbSize = 0L;
	Jedis jedis = jedisPool.getResource();
	try {
		dbSize = jedis.dbSize();
	} finally {
		if (jedis != null) {
			jedis.close();
		}
	}
	return dbSize;
}

/**
 * keys
 *
 * @param regex
 * @return
 */
public Set<byte[]> keys(String pattern) {
	Set<byte[]> keys = null;
	Jedis jedis = jedisPool.getResource();
	try {
		keys = jedis.keys(pattern.getBytes());
	} finally {
		if (jedis != null) {
			jedis.close();
		}
	}
	return keys;
}

public String getHost() {
	return host;
}

public void setHost(String host) {
	this.host = host;
}

public int getPort() {
	return port;
}

public void setPort(int port) {
	this.port = port;
}

public int getExpire() {
	return expire;
}

public void setExpire(int expire) {
	this.expire = expire;
}

public int getTimeout() {
	return timeout;
}

public void setTimeout(int timeout) {
	this.timeout = timeout;
}

public String getPassword() {
	return password;
}

public void setPassword(String password) {
	this.password = password;
}

}

RedisSessionDAO
package com.tt.springboottest.util;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RedisSessionDAO extends AbstractSessionDAO {

private static Logger logger = LoggerFactory.getLogger(RedisSessionDAO.class);
/**
 * shiro-redis的session对象前缀
 */
private RedisManager redisManager;

/**
 * The Redis key prefix for the sessions
 */
private St
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值