公司的公共服务-通知中心要添加登录验权功能,在考虑到以后的扩展性和集群部署的特性,选择了shiro+redis 的方案,现在就跟随我看看一个适用于集群的shiro是如何搭建的。
shiro配置
导包
修改web.xml
添加shiro-spring.xml 文件
shiro相关代码
自定义Realm
RedisCache实现
Start
配置
导入shiro相关jar包,这里使用maven来管理
image.png
修改原有的web.xml文件,shiro接管Servlet过滤器
image.png
3.工程使用的是spring,这里添加shiro-spirng.xml 完成shiro核心功能的配置和依赖。
在原有application.xml 文件中添加(文件名自定义)
创建shiro-spring.xml 文件(我这里命名为security-context)
添加凭证匹配器(密码使用MD5加密验证,散列次数=1)
class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
添加自定义realm(安全数据源),并开启缓存和注入凭证匹配器
RedisCache相关类注册,这里使用了Git开源的shiro+redis缓存实现(https://github.com/alexxiyang/shiro-redis),做了本地化改