zoukankan      html  css  js  c++  java
  • Shiro 分析

    1 Shiro 常见抛出异常

    UnknownAccountException(账号不存在)
    IncorrectCredentialsException(密码不存在)
    DisabledAccountException(帐号被禁用)
    LockedAccountException(帐号被锁定)
    ExcessiveAttemptsException(登录失败次数过多)
    ExpiredCredentialsException(凭证过期)等

    2 Shiro 标签

    <shiro:authenticated> 登录之后
    <shiro:notAuthenticated> 不在登录状态时
    <shiro:guest> 用户在没有RememberMe时
    <shiro:user> 用户在RememberMe时
    <shiro:hasAnyRoles name="abc,123" > 在有abc或者123角色时
    <shiro:hasRole name="abc"> 拥有角色abc
    <shiro:lacksRole name="abc"> 没有角色abc
    <shiro:hasPermission name="abc"> 拥有权限abc
    <shiro:lacksPermission name="abc"> 没有权限abc
    <shiro:principal> 显示用户登录名


    3 Shiro 过滤器链中的几种内置过滤器

    --(1)认证过滤器
    anon              匿名过滤器
    authc             需要认证
    authcBasic     表示httpBasic认证  
    user               表示必须存在用户,当登入操作时不做检查
    
    --(2)授权过滤器
    roles            /admins/user/**=authc,roles[admin] (必须认证过,并拥有admin 角色)
    perms
    port
    rest
    ssl 

    --(3) shiro 登录退出
    shiro 内置的logout 过滤器(先配置logout,再在过滤器链中配置)
    <bean id="logout" class="org.apache.shiro.web.filter.authc.LogoutFilter"> 
            <property name="redirectUrl" value="/loginform" /> 
    </bean> 
    filterChainDefinitions 配置 /logout = logout 

    --(4) 关于登录过滤器的配置
    通常将登录请求和使用的资源(js.css等)放开设置为anon,将其他的所有资源/** 设置为authc

    --(5)关于Session 失效的问题
    每次请求都会经过过滤器链的过滤,对于authc的资源,如果登录失效,自动返回到默认登录首页 配置的loginUrl 中;
    或者可以自定义个session 拦截的过滤器放入shiro 的过滤器链

    Realm
      1 认证 (自定义 与数据库的交互,将正确的user 新返回)

      return new SimpleAuthenticationInfo(userBean.getLoginName(),
    userBean.getPassword(), userBean.getRealName());  父类中对比authenticationInfo 与  token 的信息

      2 授权

    4 配置(与spring 整合)
      1 web.xml 配置
      

    <!-- shiro 配置 -->
        <filter>
            <filter-name>shiroFilter</filter-name>
            <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
            <init-param>  
                <param-name>targetFilterLifecycle</param-name>  
                <param-value>true</param-value>  
            </init-param> 
        </filter>
        <filter-mapping>
            <filter-name>shiroFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>

    2 spring-shiro.xml 文件配置
     

    <!-- ID名称对应web.xml 中filter 的名称 -->
        <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
            <property name="loginUrl" value="/" />
            <property name="successUrl" value="/system/layout/main.jsp" />
            <property name="unauthorizedUrl" value="/error/session-timeout.jsp" />
            <property name="securityManager" ref="securityManager" />
            <property name="filterChainDefinitionMap" ref="chainDefinitionSectionMetaSource"/>
        </bean>
        
        <bean id="chainDefinitionSectionMetaSource"
            class="com.fencer.framework.shiro.ChainDefinitionSectionMetaSource">
            <property name="authService" ref="authService"/>
         <!-- 定义过滤器链,加入shiro 的内置过滤器验证--> <property name="filterChainDefinitions"> <value> /system/layout/main.jsp = authc /system/manager-layout/main.jsp = authc </value> </property> </bean> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="sessionMode" value="http"/> <property name="realm" ref="customShiroRealm" /> </bean> <bean id="customShiroRealm" class="shiro.ShiroRealm"> <!-- 缓存 --> <!-- <property name="cacheManager" ref="shiroCacheManager" /> --> <!-- authService 用于查询用户 --> <property name="authService" ref="authService"/> </bean> <!-- <bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <property name="cacheManager" ref="defaultCacheManager" /> </bean> --> <!-- <bean id="defaultCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation"> <value>/resource/ehcache.xml</value> </property> </bean> --> <!-- Shiro生命周期处理器--> <!-- <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" /> --> <!-- 相当于调用SecurityUtils.setSecurityManager(securityManager) --> <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"/> <property name="arguments" ref="securityManager"/> </bean>



  • 相关阅读:
    webstorm
    数据库中的内连接和外连接
    JVM加载class文件的原理机制
    内部类
    getString()方法与getObject()方法的区别
    Class.forName的作用
    JDBC事务处理
    合并两个排序的链表
    链表中倒数第k个结点
    反转链表
  • 原文地址:https://www.cnblogs.com/leonkobe/p/5687260.html
Copyright © 2011-2022 走看看