??公司的公共服務(wù)-通知中心要添加登錄驗權(quán)功能,在考慮到以后的擴(kuò)展性和集群部署的特性,選擇了shiro+redis 的方案,現(xiàn)在就跟隨我看看一個適用于集群的shiro是如何搭建的。
shiro配置
- 導(dǎo)包
- 修改web.xml
- 添加shiro-spring.xml 文件
shiro相關(guān)代碼
- 自定義Realm
- RedisCache實現(xiàn)
Start
配置
-
導(dǎo)入shiro相關(guān)jar包,這里使用maven來管理
image.png -
修改原有的web.xml文件,shiro接管Servlet過濾器
image.png
3.工程使用的是spring,這里添加shiro-spirng.xml 完成shiro核心功能的配置和依賴。
- 在原有application.xml 文件中添加(文件名自定義)
<import resource="/security-context.xml" />
- 創(chuàng)建shiro-spring.xml 文件(我這里命名為security-context)
- 添加憑證匹配器(密碼使用MD5加密驗證,散列次數(shù)=1)
<!-- 憑證匹配器 密碼MD5加密驗證 -->
<bean id="credentialsMatcher"
class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<property name="hashAlgorithmName" value="md5"/>
<property name="hashIterations" value="1"/>
</bean>
- 添加自定義realm(安全數(shù)據(jù)源),并開啟緩存和注入憑證匹配器
<!--自定義Realm-->
<bean id="noticeRealm" class="com.lingyun.security.relam.simpleRealm">
<!--啟用緩存,默認(rèn)關(guān)閉-->
<property name="cachingEnabled" value="true"/>
<!--啟用身份驗證緩存,即緩存AuthenticationInfo,默認(rèn)false-->
<property name="authenticationCachingEnabled" value="true"/>
<!-- 配置密碼匹配器 -->
<property name="credentialsMatcher" ref="credentialsMatcher"/>
</bean>
- RedisCache相關(guān)類注冊,這里使用了Git開源的shiro+redis緩存實現(xiàn)(https://github.com/alexxiyang/shiro-redis),做了本地化改造
<!--redis池管理器 -->
<bean id="shardedJedisPoolManager"
class="com.lingyun.security.rediscache.ShardedJedisPoolManager">
<property name="expire" value="1800"/><!-- 秒 -->
</bean>
<!--Session緩存DAO組件-->
<bean id="redisSessionDAO" class="com.lingyun.security.rediscache.RedisSessionDAO">
<property name="redisManager" ref="shardedJedisPoolManager"/>
</bean>
<!--shiro(redis)緩存管理-->
<bean id="redisCacheManager" class="com.lingyun.notice.service.web.security.rediscache.RedisCacheManager">
<property name="redisManager" ref="shardedJedisPoolManager"/>
</bean>
- 添加SimpleCookie,修改默認(rèn)sessionID。默認(rèn)sessionID 為JSESSIONID 會和容器名沖突, 如JETTY, TOMCAT 等,導(dǎo)致session失效
<!--修改默認(rèn)sessionID 為notice-center-->
<bean id="simpleCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
<constructor-arg name="name" value="notice-center"/>
<property name="path" value="/"/>
</bean>
- 添加會話管理器,
<!-- (session)會話管理器 -->
<bean id="sessionManager" class="com.lingyun.web.security.session.SimpleWebSessionManager">
<!--session緩存-->
<property name="sessionDAO" ref="redisSessionDAO"/>
<!--修改默認(rèn)Cookie-->
<property name="sessionIdCookie" ref="simpleCookie"/>
<!-- session的失效時長,單位毫秒 -->
<property name="globalSessionTimeout" value="600000"/>
<!-- 刪除失效的session -->
<property name="deleteInvalidSessions" value="true"/>
<!--隱藏url中的JSESSIONID -->
<property name="sessionIdUrlRewritingEnabled" value="false"/>
</bean>
- 配置shiroWeb過濾器,實現(xiàn)url自定義攔截
<!-- Shiro 的Web過濾器 -->
<bean id="securityFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="/login.html"/>
<property name="unauthorizedUrl" value="/login.html"/>
<property name="filterChainDefinitions">
<value>
/static/** = anon
/doLogin = anon
/error = anon
/send* = anon
/** = user
</value>
</property>
</bean>
- 開啟rememberMe功能
<!-- rememberMeManager管理器,寫cookie,取出cookie生成用戶信息 -->
<bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager">
<property name="cookie" ref="rememberMeCookie"/>
</bean>
<!-- 記住我cookie -->
<bean id="rememberMeCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
<!-- rememberMe是cookie的名字 -->
<constructor-arg value="notice-cookie"/>
<!-- 記住我cookie生效時間30天 -->
<property name="maxAge" value="2592000"/>
</bean>
- 最后注冊安全管理器,注入相關(guān)管理器
<!-- securityManager安全管理器 -->
<bean id="securityManager" class="com.lingyun.security.SimpleWebSecurityManager">
<property name="realm" ref="noticeRealm"/>
<!-- 注入緩存管理器 -->
<property name="cacheManager" ref="redisCacheManager"/>
<!-- 注入session管理器 -->
<property name="sessionManager" ref="sessionManager"/>
<!-- 記住我 -->
<property name="rememberMeManager" ref="rememberMeManager"/>
</bean>
??到這里shiro相關(guān)的配置及改造就結(jié)束了,接下來是一些和業(yè)務(wù)綁定的代碼和數(shù)據(jù)庫相關(guān)
continue
代碼編寫及改造
-
創(chuàng)建用戶DTO和Table,這里要注意UserDTO一定要實現(xiàn)序列化接口,redis緩存時會序列化對象
image.png
2.自定義Realm實現(xiàn),要注意因為使用了憑證匹配器,認(rèn)證時不用再做密碼驗證,將數(shù)據(jù)源中的密碼傳入即可,另外因?qū)崿F(xiàn)了redis緩存,要傳入UserDTO實例
/**
* 認(rèn)證
*
* @param token
* @return
* @throws AuthenticationException
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String username = (String) token.getPrincipal();
NoticeUserDTO user = noticeUserInfoService.findUserIFByName(username);
if (user == null || user.getDataState() == 0) {
throw new UnknownAccountException(); //用戶為空
}
//如果身份認(rèn)證驗證成功,返回一個AuthenticationInfo實現(xiàn);
return new SimpleAuthenticationInfo(user, user.getPassword(), getName());
}


