zoukankan      html  css  js  c++  java
  • Spring shiro学习(二)

    shiro 默认自带的过滤器如下:

    Filter NameClass
    anon org.apache.shiro.web.filter.authc.AnonymousFilter
    authc org.apache.shiro.web.filter.authc.FormAuthenticationFilter
    authcBasic org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter
    logout org.apache.shiro.web.filter.authc.LogoutFilter
    noSessionCreation org.apache.shiro.web.filter.session.NoSessionCreationFilter
    perms org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter
    port org.apache.shiro.web.filter.authz.PortFilter
    rest org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter
    roles org.apache.shiro.web.filter.authz.RolesAuthorizationFilter
    ssl org.apache.shiro.web.filter.authz.SslFilter
    user org.apache.shiro.web.filter.authc.UserFilter

    我们平常用的就是 anon:任何人都可以访问;authc:必须登录才能访问,不包含rememberme ;user:登录用户才可以访问,包含rememberme ;perms:指定过滤规则,这个一般是扩展使用,不会使用原生的,例如springrain扩展的为frameperms.

    filterChainDefinitions 就是指定过滤规则的,一般是把公共配置使用配置文件,例如 js  css img 这些资源文件是不拦截的,业务相关的url配置到数据库,有过滤器查询数据库进行权限判断.

    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
            <property name="securityManager" ref="securityManager"/>
            <property name="loginUrl" value="${zheng.upms.sso.server.url}"/>
            <property name="successUrl" value="${zheng.upms.successUrl}"/>
            <property name="unauthorizedUrl" value="${zheng.upms.unauthorizedUrl}"/>
            <property name="filters">
                <util:map>
                    <entry key="authc" value-ref="upmsAuthenticationFilter"/>
                </util:map>
            </property>
            <property name="filterChainDefinitions">
                <value>
                    /manage/** = upmsSessionForceLogout,authc
                    /manage/index = user
                    /druid/** = user
                    /swagger-ui.html = user
                    /resources/** = anon
                    /** = anon
                </value>
            </property>
        </bean>
    filterChainDefinitions是定义拦截规则

    拦截器的优先级是:从上至下,从左到右,如果有匹配的拦截器就会阻断并返回,例如 访问 /manage/index 第二个拦截器 user符合,就返回true了,不再往下匹配了.

    最后一句是 /**=anon 意思就是除了上面的那些,其他的所有都要经过 anon.任何人都可以访问

     重写了authc、upmsSessionForceLogout

    <!-- 重写authc过滤器 -->
        <bean id="upmsAuthenticationFilter" class="com.zheng.upms.client.shiro.filter.UpmsAuthenticationFilter"/>
    
        <!-- 强制退出会话过滤器 -->
        <bean id="upmsSessionForceLogout" class="com.zheng.upms.client.shiro.filter.UpmsSessionForceLogoutFilter"/>

     securityManager作为安全管理器配置了sessionManager、realms、rememberMeManager

    <!-- 安全管理器 -->
        <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
            <property name="realms">
                <list><ref bean="upmsRealm"/></list>
            </property>
            <property name="sessionManager" ref="sessionManager"/>
            <property name="rememberMeManager" ref="rememberMeManager"/>
        </bean>

    realms配置

    <!-- realm实现,继承自AuthorizingRealm -->
        <bean id="upmsRealm" class="com.zheng.upms.client.shiro.realm.UpmsRealm"></bean>

     sessionManager配置了sessionDAO、sessionIdCookie、sessionListeners、sessionFactory

    <!-- 会话管理器 -->
        <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
            <!-- 全局session超时时间 -->
            <property name="globalSessionTimeout" value="${zheng.upms.session.timeout}"/>
            <!-- sessionDAO -->
            <property name="sessionDAO" ref="sessionDAO"/>
            <property name="sessionIdCookieEnabled" value="true"/>
            <property name="sessionIdCookie" ref="sessionIdCookie"/>
            <property name="sessionValidationSchedulerEnabled" value="false"/>
            <property name="sessionListeners">
                <list><ref bean="sessionListener"/></list>
            </property>
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>

     sessionDAO配置重写

    <!-- 会话DAO,可重写,持久化session -->
        <bean id="sessionDAO" class="com.zheng.upms.client.shiro.session.UpmsSessionDao"/>

     sessionIdCookie配置

    <!-- 会话Cookie模板 -->
        <bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
            <!-- 不会暴露给客户端 -->
            <property name="httpOnly" value="true"/>
            <!-- 设置Cookie的过期时间,秒为单位,默认-1表示关闭浏览器时过期Cookie -->
            <property name="maxAge" value="-1"/>
            <!-- Cookie名称 -->
            <property name="name" value="${zheng.upms.session.id}"/>
        </bean>
  • 相关阅读:
    javascript Date类的扩展
    软件工程师好了歌 (转)
    您可能不知道的.Net2.0小技巧
    您未必知道的Js技巧
    复活吧,架构师!
    技巧系列文章
    不要使用paddingtop控制内容开始的位置
    JQuery Offset实验与应用(转载)
    2008最佳Windows应用程序
    精选15个国外CSS框架
  • 原文地址:https://www.cnblogs.com/growingpains/p/7208499.html
Copyright © 2011-2022 走看看