1. 准备配置
1.1 配置 SecurityManager
在 web.xml 中配置
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="cacheManager" ref="cacheManager"/>
<property name="authenticator" ref="authenticator"/>
<!-- 配置多个 Realm -->
<property name="realms">
<list>
<ref bean="jdbcRealm"/>
<ref bean="secondRealm"/>
</list>
</property>
</bean>
1.2 配置缓存 CacheManager
配置缓存后, 认证一次后再次访问需授权页面时, 读取缓存即可
配置 CacheManager 需要加入 ehcache 的 jar 包及配置文件
在 web.xml 中配置
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
</bean>
1.3 配置认证相关信息
在 web.xml 中配置
<bean id="authenticator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator">
<!-- 配置认证策略 -->
<property name="authenticationStrategy">
<bean class="org.apache.shiro.authc.pam.AllSuccessfulStrategy"/>
</property>
</bean>
1.4 配置 Realm
直接配置实现了 org.apache.shiro.realm.Realm 接口的 bean
在 web.xml 中配置
<bean id="jdbcRealm" class="com.tc.shiro.realms.ShiroRealm">
<property name="credentialsMatcher">
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<!-- 加密算法名字 -->
<property name="hashAlgorithmName" value="MD5"/>
<!-- 加密次数 -->
<property name="hashIterations" value="1024"/>
</bean>
</property>
</bean>
1.5 配置 LifecycleBeanPostProcessor
可以自动的来调用配置在 Spring IOC 容器中 Shiro Bean 的生命周期方法
在 web.xml 中配置
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
1.6 启用 IOC 容器中的使用 shiro 的注解
必须在配置了 LifecycleBeanPostProcessor 之后才可以使用
在 web.xml 中配置
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
1.7 配置 shiroFilter
在 web.xml 中添加
<