一、文章前言
shiro认证缓存是提高web系统性能的一个重要部分,不是经常变动的东西都可以缓存起来,这样就可以减少数据库的查询次数,提高系统的性能,在这里我主要是使用Ehcache来实现。
二、shiro配置文件配置缓存
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="cacheManager" ref="cacheManager"/>
<property name="realm" ref="jdbcRealm"/>
<property name="sessionManager" ref="sessionManager"/>
</bean>
<bean id="jdbcRealm" class="com.IFox.shiro.bean.ShiroRealm">
<property name="cachingEnabled" value="true"/>
<property name="authenticationCachingEnabled" value="true"/>
<property name="authenticationCacheName" value="authenticationCache"/>
<property name="authorizationCachingEnabled" value="true"/>
<property name="authorizationCacheName" value="authorizationCache"/>
</bean>
<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<property name="sessionIdCookieEnabled" value="true"/>
</bean>
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
</bean>
三、Ehcache配置
<?xml version="1.1" encoding="UTF-8"?>
<ehcache name="shirocache">
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="2000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"/>
<cache name="passwordRetryCache"
maxElementsInMemory="2000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="10"
overflowToDisk="false">
</cache>
<cache name="authorizationCache"
maxElementsInMemory="2000"
eternal="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="10"
overflowToDisk="false">
</cache>
<cache name="authenticationCache"
maxElementsInMemory="2000"
eternal="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="10"
overflowToDisk="false">
</cache>
<cache name="shiro-activeSessionCache"
maxElementsInMemory="2000"
eternal="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="10"
overflowToDisk="false">
</cache>
</ehcache>
四、认证授权Relam使用
public class ShiroRealm extends AuthenticatingRealm{
/**
* 1、 doGetAuthenticationInfo 获取认证消息 如果数据库中没有数据 返回null 如果正确 返回封装的对象
* 2、 AuthenticationInfo 可以使用SimpleAuthenticationInfo 实现类 封装给你正确的用户名和密码
* 3、 token参数 就是我们要认证的token
*
* */
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
SimpleAuthenticationInfo info = null;
String userName = token.getUsername();
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql:///ifoxMember";
Connection connection = DriverManager.getConnection(url,"mysql","mysqladmin");
String sql = "select * from shiro_User where userName = ?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, token.getUsername());
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
Object principal = userName;
Object credentials = resultSet.getString(3);
String relamName = this.getName();
info = new SimpleAuthenticationInfo(principal,credentials,relamName);
} else {
throw new AuthenticationException();
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return info;
}
五、总结
使用shiro安全认证缓存不仅可以让我们方便的管理用户,还可以提高系统的性能减小对数据库的访问。