shiro入门配置

基础配置

使用shiro的步骤
1. 导入JAR包
2. web.xml配置

    <!-- Shiro配置 -->    
    <filter>    
        <filter-name>shiroFilter</filter-name>    
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>    
    </filter>    
    <filter-mapping>    
        <filter-name>shiroFilter</filter-name>    
        <url-pattern>/*</url-pattern>    
    </filter-mapping>
  1. applicationContext.xml配置
    <!-- ehcache 的配置 -->
    <cache:annotation-driven cache-manager="cacheManager" />
    <bean id="cacheManagerFactory"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation">
            <value>classpath:ehcache.xml</value>
        </property>
    </bean>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="cacheManagerFactory" />
    </bean>

    <!-- 自定义Realm实现 -->
    <bean id="myRealm" class="com.lbs.login.myRealm">
        <!-- <property name="cacheManager" ref="shiroEhcacheManager" /> -->
    </bean>
    <!-- 用户授权/认证信息Cache, 采用EhCache 缓存 -->
    <bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <property name="cacheManagerConfigFile" value="classpath:ehcache.xml" />
        <!-- <property name="cacheManager" ref="cacheManager" /> -->
    </bean>
    <!-- securityManager -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="myRealm" />
        <property name="cacheManager" ref="shiroEhcacheManager" />
    </bean>

    <!-- Shiro生命周期处理器,保证实现了Shiro内部lifecycle函数的bean执行 -->    
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

    <!-- Shiro Filter 拦截器相关配置 -->
    <!--shiro过滤器配置,bean的id值须与web中的filter-name的值相同 -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!-- securityManager -->
        <property name="securityManager" ref="securityManager" />
        <!-- 登录路径 -->
        <property name="loginUrl" value="/login.jsp" />
        <!-- 登录成功后跳转路径 -->
        <property name="successUrl" value="/manager/index.jsp" />
        <!-- 授权失败跳转路径 -->
        <property name="unauthorizedUrl" value="/login.jsp" />
        <!-- 过滤链定义 -->
        <property name="filterChainDefinitions">
            <value>
                /login.jsp* = anon
                /logout* = anon
                /*.js = anon
                /*.css = anon
                /manager/** = authc,roles["管理员"]<!-- 表示需要认证的链接 -->
                /index.jsp = authc
            </value>
        </property>
    </bean>


  1. 建立myRealm.java,重写以下两种方法

doGetAuthorizationInfo()方法可以理解为是权限验证
doGetAuthenticationInfo(AuthenticationToken token)理解为登陆验证。

shiro 授权

Shiro支持三种方式实现授权过程:
- 编码实现
- 注解实现
- JSP Taglig实现

角色授权

当需要验证用户是否拥有某个角色时,可以调用Subject实例的hasRole*方法验证。

Subject currentUser = SecurityUtils.getSubject();
if (currentUser.hasRole("administrator")) {
    //show the admin button
} else {
    //don't show the button?  Grey it out?
}

具体见[shiro入门]一文,原文不定时更新(http://chen1218chen.github.io/2016/03/30/shiro%E5%85%A5%E9%97%A8/)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值