shiro redis怎么用
-
使用Shiro和Redis可以实现用户会话的管理和权限控制。下面是具体的使用方法:
- 引入依赖
首先,在项目的pom.xml文件中添加Shiro和Redis的依赖:
<dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.8.1</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.11.2</version> </dependency>- 配置Shiro的会话管理器
在Shiro的配置文件中,配置Redis作为会话管理器:
<bean id="redisManager" class="org.crazycake.shiro.RedisManager"> <property name="host" value="localhost" /> <property name="port" value="6379" /> <property name="timeout" value="2000" /> </bean> <bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO"> <property name="activeSessionsCacheName" value="shiro-activeSessionCache" /> </bean> <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager"> <property name="sessionDAO" ref="sessionDAO" /> <property name="sessionValidationSchedulerEnabled" value="true" /> <property name="sessionValidationInterval" value="1800000" /> <property name="globalSessionTimeout" value="1800000" /> <property name="deleteInvalidSessions" value="true" /> <property name="sessionValidationScheduler" ref="sessionValidationScheduler" /> </bean> <bean id="sessionValidationScheduler" class="org.apache.shiro.session.mgt.quartz.QuartzSessionValidationScheduler"> <property name="sessionManager" ref="sessionManager" /> <property name="enabled" value="true" /> </bean> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" /> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor" /> <bean class="org.apache.shiro.web.filter.authc.FormAuthenticationFilter"> <property name="usernameParam" value="username" /> <property name="passwordParam" value="password" /> </bean> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="sessionManager" ref="sessionManager" /> <property name="rememberMeManager" ref="cookieRememberMeManager" /> </bean>这里配置的RedisManager指定了Redis的连接信息,sessionDAO用于将会话存储在Redis中,sessionManager用于管理会话。
- 配置Shiro的权限控制
可以在Shiro的配置文件中配置相应的权限控制规则,例如:
<bean id="shiroRealm" class="com.example.shiro.ShiroRealm" /> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" /> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="sessionManager" ref="sessionManager" /> <property name="rememberMeManager" ref="cookieRememberMeManager" /> <property name="realm" ref="shiroRealm" /> </bean> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor" /> <bean id="authorizationAttributeSourceAdvisor" class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager" /> </bean>这里配置了ShiroRealm用于验证用户的身份和授权,securityManager用于管理安全事务,AuthorizationAttributeSourceAdvisor用于注入权限控制的相关配置。
- 编写ShiroRealm
在自定义的ShiroRealm中,可以实现具体的身份验证和授权逻辑。例如:
public class ShiroRealm extends AuthorizingRealm { @Autowired private UserService userService; @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { String username = (String) token.getPrincipal(); User user = userService.findUserByUsername(username); if (user == null) { throw new UnknownAccountException("用户不存在"); } SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(username, user.getPassword(), getName()); return authenticationInfo; } @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { String username = (String) principals.getPrimaryPrincipal(); User user = userService.findUserByUsername(username); SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); Set<String> roles = user.getRoles(); Set<String> permissions = user.getPermissions(); authorizationInfo.setRoles(roles); authorizationInfo.setStringPermissions(permissions); return authorizationInfo; } }这里通过实现doGetAuthenticationInfo方法进行身份验证,doGetAuthorizationInfo方法进行授权,可以根据具体的业务逻辑进行实现。
通过以上步骤,就可以使用Shiro和Redis来管理用户会话和实现权限控制了。希望对你有帮助!
2年前 - 引入依赖
-
使用Shiro Redis作为缓存存储可以提供更高效的访问速度和可扩展性。下面是使用Shiro Redis的一些步骤和注意事项:
-
首先,确保你已经正确地配置了Redis和Shiro。你需要安装Redis服务器并配置好密码、端口和主机等信息。
-
在你的项目中添加Shiro Redis的依赖。例如,在Maven项目中,将以下依赖项添加到pom.xml文件中:
<dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-redis</artifactId> <version>1.7.1</version> </dependency>- 在你的Shiro配置文件中,进行相关的配置。例如,添加以下配置到你的shiro.ini或shiro.yml文件中:
cacheManager = org.apache.shiro.cache.CacheManager securityManager.cacheManager = $cacheManager redisManager = org.crazycake.shiro.RedisManager redisManager.host = localhost redisManager.port = 6379 redisManager.password = your_password cacheManager = org.apache.shiro.cache.CacheManager cacheManager.redisManager = $redisManager这里需要注意的是,在
redisManager.host、redisManager.port和redisManager.password中填写你实际的Redis配置信息。- 接下来,你可以在你的代码中使用Shiro的缓存功能了。例如,可以在Realm的实现类中使用缓存:
public class MyRealm extends AuthorizingRealm { @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { // 从缓存中获取用户信息 String username = (String) token.getPrincipal(); SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(username, password, getName()); authenticationInfo.setCredentialsSalt(ByteSource.Util.bytes(salt)); return authenticationInfo; } @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { // 从缓存中获取用户认证信息 String username = (String) token.getPrincipal(); Object credentials = redisCacheManager.get(username); if (credentials != null) { return new SimpleAuthenticationInfo(username, credentials, getName()); } else { throw new AuthenticationException("User not found in cache"); } } }- 最后,运行你的项目,并观察Redis缓存的使用情况。你可以使用Redis的命令行或GUI工具来查看缓存中的数据。
需要注意的是,使用Shiro Redis作为缓存存储会增加一些额外的配置和性能开销。在配置Redis时,考虑到安全性和性能问题,需要选择适当的密码和服务器配置。此外,还需要注意在使用缓存时保证数据的一致性和及时更新缓存的机制。在高并发环境中,还可能需要对缓存的压力和Redis服务器的性能进行优化。
2年前 -
-
使用Shiro和Redis的结合可以实现更高效和可靠的身份验证与授权管理。下面是Shiro Redis的使用方法和操作流程:
- 引入依赖
首先,在项目的pom.xml文件中添加Shiro和Redis的相关依赖。例如:
<dependencies> <!-- Shiro --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.8.0</version> </dependency> <!-- Redis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.8.1</version> </dependency> </dependencies>- 配置Shiro
在Shiro的配置文件(比如shiro.ini或shiro.xml)中配置RedisRealm和RedisCacheManager。示例如下:
<bean id="redisManager" class="org.crazycake.shiro.RedisManager"> <property name="host" value="{redis_host}"/> <property name="port" value="{redis_port}"/> <property name="password" value="{redis_password}"/> <property name="timeout" value="{redis_timeout}"/> </bean> <bean id="redisCacheManager" class="org.crazycake.shiro.RedisCacheManager"> <property name="redisManager" ref="redisManager"/> </bean> <bean id="redisSessionDAO" class="org.crazycake.shiro.RedisSessionDAO"> <property name="redisManager" ref="redisManager"/> </bean> <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager"> <property name="sessionDAO" ref="redisSessionDAO"/> </bean> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="cacheManager" ref="redisCacheManager"/> <property name="sessionManager" ref="sessionManager"/> <!-- 其他配置 --> </bean>其中,{redis_host}、{redis_port}、{redis_password}、{redis_timeout}为Redis的相关信息,可以根据具体情况进行配置。
- 编写Realm
自定义一个继承自AuthorizingRealm的RedisRealm,重写其中的授权和认证方法。示例如下:
public class RedisRealm extends AuthorizingRealm { private RedisManager redisManager; public void setRedisManager(RedisManager redisManager) { this.redisManager = redisManager; } @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { // 省略授权逻辑 } @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { // 省略认证逻辑 } }- 配置ShiroFilterFactoryBean
在Spring的配置文件中配置ShiroFilterFactoryBean,并设置securityManager等属性。示例如下:
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <property name="loginUrl" value="/login.html"/> <property name="successUrl" value="/index.html"/> <property name="unauthorizedUrl" value="/unauthorized.html"/> <property name="filterChainDefinitions"> <value> /login.html = anon /logout = logout /** = authc </value> </property> </bean>其中,/login.html为登录页面的URL,/index.html为登录成功后的URL,/unauthorized.html为无权限访问时的URL。
- 使用Redis进行缓存和Session管理
通过配置RedisCacheManager和RedisSessionDAO,可以将Shiro的缓存和Session存储在Redis中,提高系统的性能和可扩展性。
至此,Shiro Redis的使用配置就完成了。在具体业务代码中,可以通过Subject对象进行认证和授权操作,Redis中的数据将用于缓存和存储用户信息、权限信息等。
需要注意的是,使用Shiro Redis需要确认Redis服务已经启动,并且配置文件和代码中的相关配置与实际环境相匹配。
以上是Shiro Redis的基本使用方法和操作流程,根据实际需求可以进行更进一步的配置和扩展。
2年前 - 引入依赖