• 配置ShiroFilter需要注意的问题(Shiro_DelegatingFilterProxy)


    ShiroFilter的工作原理

    ShiroFilter:DelegatingFilterProxy作用是自动到Spring 容器查找名字为shiroFilter(filter-name)的bean并把所有Filter 的操作委托给它。

    问题讲解:为什么applicationContext.xml中的id必须和web.xml文件中配置的DelegatingFilterProxy的 <filter-name> 一致?

    applicationContext.xml中的id必须和web.xml文件中配置的DelegatingFilterProxy的 <filter-name> 一致
    若不一致,则会抛出:NoSuchBeanDefinitionException. 因为 Shiro 会来 IOC 容器中查找和 <filter-name> 名字对应的 filter bean.

        <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
            <property name="securityManager" ref="securityManager"/>
            <property name="loginUrl" value="/login.jsp"/>
            <property name="successUrl" value="/list.jsp"/>
            <property name="unauthorizedUrl" value="/unauthorized.jsp"/>
            
            <!-- 
                配置哪些页面需要受保护
                以及访问这些页面需要的权限
                1). anon 可以被匿名访问
                2). authc 必须认证(即登录)后才可以访问的页面
             -->
            <property name="filterChainDefinitions">
                <value>
                    /login.jsp = anon
                    
                    # everything else requires authentication:
                    /user.jsp = authc
                </value>
            </property>
        </bean>

    web.xml:

        <!-- Shiro Filter is defined in the spring application context: -->
        <!-- 
        1. 配置  Shiro 的 shiroFilter.  
        2. DelegatingFilterProxy 实际上是 Filter 的一个代理对象. 默认情况下, Spring 会到 IOC 容器中查找和 
        <filter-name> 对应的 filter bean. 也可以通过 targetBeanName 的初始化参数来配置 filter bean 的 id. 
        -->
        <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>

    若想修改shiroFilter名称,即自定义,web.xml需要添加如下内容:

        <!-- Shiro Filter is defined in the spring application context: -->
        <!-- 
        1. 配置  Shiro 的 shiroFilter.  
        2. DelegatingFilterProxy 实际上是 Filter 的一个代理对象. 默认情况下, Spring 会到 IOC 容器中查找和 
        <filter-name> 对应的 filter bean. 也可以通过 targetBeanName 的初始化参数来配置 filter bean 的 id. 
        -->
        <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>
            <init-param>
                <param-name>targetBeanName</param-name>
                <param-value>abc</param-value>
            </init-param>
        </filter>
    
        <filter-mapping>
            <filter-name>shiroFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>

    applicationContext.xml:

        <!-- 
        6.配置ShiroFilter
        6.1 id必须和web.xml文件中配置的DelegatingFilterProxy的 <filter-name> 一致
            若不一致,则会抛出:NoSuchBeanDefinitionException. 因为 Shiro 会来 IOC 容器中查找和 <filter-name> 名字对应的 filter bean.
         -->
        <bean id="abc" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
            <property name="securityManager" ref="securityManager"/>
            <property name="loginUrl" value="/login.jsp"/>
            <property name="successUrl" value="/list.jsp"/>
            <property name="unauthorizedUrl" value="/unauthorized.jsp"/>
            
            <!-- 
                配置哪些页面需要受保护
                以及访问这些页面需要的权限
                1). anon 可以被匿名访问
                2). authc 必须认证(即登录)后才可以访问的页面
             -->
            <property name="filterChainDefinitions">
                <value>
                    /login.jsp = anon
                    
                    # everything else requires authentication:
                    /user.jsp = authc
                </value>
            </property>
        </bean>

    这样,就自定义了名称(不推荐,第一种默认即可)

  • 相关阅读:
    asp.net的Context.Cache缓存过期策略
    sql语句执行时算术运算导致溢出。
    sqlserver进行join的方式选择
    Apollo配置中心
    sqlserver的left join优化
    iis设置上传文件大小限制
    Android中的颜色值
    Network authentication method and device for implementing the same
    MongoDB GridFS
    MongoDB 正则表达式
  • 原文地址:https://www.cnblogs.com/116970u/p/11169699.html
Copyright © 2020-2023  润新知