Shiro 学习笔记(一) -- Shiro 全部功能解析

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 中添加

<
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值