010 动态URL过滤

一 .概述

  在上一节之中,我们使用配置文件的形式进行URL过滤链的配置.但是我们发现有两个问题

  (1)使用配置文件会发生配置在一个位置太麻烦了,尤其是多人使用同一个配置文件的时候.

  (2)过滤器链无法动态配置,写在配置文件太死板了.


 二 .问题的解决

public void setFilterChainDefinitionMap(Map<String, String> filterChainDefinitionMap) 
 public void setFilterChainDefinitions(String definitions) {

我们在配置ShiroFilter的时候使用的是第二个方式,其实就是按照一个字符串的形式进行配置.

同时,我们也可以使用上面的方式.

其结构就是一个Map.

我们使用这个方式来解决一下问题.

public class MapURLSupply implements FactoryBean<Map<String, String>> {

    @Override
    public Map<String, String> getObject() throws Exception {
        Map<String, String> map = new LinkedHashMap<String, String>();
        map.put("/authc", "authc");
        return map;
    }

    // 返回类型
    @Override
    public Class<?> getObjectType() {
        return Map.class;
    }

    // 确定是单例的模式
    @Override
    public boolean isSingleton() {
        return true;
    }

}

我们创建了一个Map提供URL过滤器的Bean,这个Bean实现了FactoryBean接口,以后我们使用spring就能过去这个Bean,并且修改这个Bean.

  另外我们也可以读取数据库完成这样的操作,这里就是简单的演示一下.

扫描二维码关注公众号,回复: 925838 查看本文章
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"></property>
        <!-- 配置登录页面 -->
        <property name="loginUrl" value="/login"></property>
        <!-- 配置未授权页面的路径 -->
        <property name="unauthorizedUrl" value="/unan.jsp"></property>
        <!-- 配置登录成功的页面 -->
        <property name="successUrl" value="/WEB-INF/success.jsp"></property>
        <!-- 配置过滤器链
            这是shiro的web配置的核心
         -->
        <!--  <property name="filterChainDefinitions">
             <value>
                 /login=anon
                 /authc=authc
                 /user=roles[admin]
                 /user/add=perms[admin:add]
                 /log1=log[123]
                 /log2=log[234]
             </value>
         </property> -->
         <property name="filterChainDefinitionMap" ref="map"></property>
         <property name="filters">
             <map>
                 <entry key="log" value-ref="logFilter"></entry>
             </map>
         </property>
    </bean>

上面是配置文件.我们使用这个属性完成注入.

当我们访问authc的时候,就被重定向到了登录页面.

  说明我们的配置是成功的了.


猜你喜欢

转载自www.cnblogs.com/trekxu/p/9058197.html
今日推荐