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

ShiroFilter的工作原理

配置ShiroFilter需要注意的问题(Shiro_DelegatingFilterProxy)_第1张图片

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

配置ShiroFilter需要注意的问题(Shiro_DelegatingFilterProxy)_第2张图片

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

applicationContext.xml中的id必须和web.xml文件中配置的DelegatingFilterProxy的 一致
若不一致,则会抛出:NoSuchBeanDefinitionException. 因为 Shiro 会来 IOC 容器中查找和 名字对应的 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"/>
        
        
        <property name="filterChainDefinitions">
            <value>
                /login.jsp = anon
                
                # everything else requires authentication:
                /user.jsp = authc
            value>
        property>
    bean>

web.xml:

    
    
    <filter>
        <filter-name>shiroFilterfilter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class>
        <init-param>
            <param-name>targetFilterLifecycleparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>

    <filter-mapping>
        <filter-name>shiroFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

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

    
    
    <filter>
        <filter-name>shiroFilterfilter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxyfilter-class>
        <init-param>
            <param-name>targetFilterLifecycleparam-name>
            <param-value>trueparam-value>
        init-param>
        <init-param>
            <param-name>targetBeanNameparam-name>
            <param-value>abcparam-value>
        init-param>
    filter>

    <filter-mapping>
        <filter-name>shiroFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

applicationContext.xml:

    
    <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"/>
        
        
        <property name="filterChainDefinitions">
            <value>
                /login.jsp = anon
                
                # everything else requires authentication:
                /user.jsp = authc
            value>
        property>
    bean>

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

转载于:https://www.cnblogs.com/116970u/p/11169699.html

你可能感兴趣的:(配置ShiroFilter需要注意的问题(Shiro_DelegatingFilterProxy))