Shiro会话管理和加密
Shiro使用缓存需要做哪些配置
shiro认证缓存是提高web系统性能的一个重要部分,不是经常变动的东西都可以缓存起来,这样就可以减少数据库的查询次数,提高系统的性能,在这里我主要是使用Ehcache来实现。
shiro配置文件配置缓存
<!--
安全管理器
shiro中提供了对认证和授权的缓存,shiro是默认开始授权缓存而关闭认证缓存的
在SecurityManager中需要cacheManager这个参数
-->
<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>
<!--relam配置-->
<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>
<!--通过session会话来管理缓存,开启sessionIdCookieEnabled-->
<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 {
//将token转换成userNamePasswordToken
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安全认证缓存不仅可以让我们方便的管理用户,还可以提高系统的性能减小对数据库的访问。
系统密码加密的必要性及常见实现方式
Shiro中如何进行密码验证
看下官方文档都有,SimpleAuthenticationInfo(Object principal, Object credentials, String realmName),这里principal用于标识用户,比如用户账号,credentials用于验证,比如密码(这里可以是类似md5加密后的)。
比如用户jim,密码是123。 登录时会以UsernamePasswordToken(jim,123)的传入,realm中有个CredentialsMatcher(可配置,可以对password进行加密)。 根据这个Token和SimpleAuthenticationInfo的credentials做对比,就能进行验证。