spring-boot-shiro 客户过滤器不起作用

Posted

技术标签:

【中文标题】spring-boot-shiro 客户过滤器不起作用【英文标题】:spring-boot-shiro customer filter not working 【发布时间】:2018-06-07 01:37:50 【问题描述】:

全部

spring-boot 运行错误: 没有名称为“userAuthorizationFilter”的过滤器可应用于可用过滤器池中的链 [/**]。确保具有该名称/路径的过滤器首先使用 addFilter 方法注册。

  <dependency>
        <groupId>org.apache.shiro</groupId>
        <artifactId>shiro-spring-boot-web-starter</artifactId>
        <version>1.4.0</version>
  </dependency>

shiroConfig.java:

@Configuration
public class ShiroConfig

@Bean
public ShiroFilterChainDefinition shiroFilterChainDefinition() 
    DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();

    chainDefinition.addPathDefinition("/css/**", DefaultFilter.anon.name());
    chainDefinition.addPathDefinition("/js/**", DefaultFilter.anon.name());
    chainDefinition.addPathDefinition("/images/**", DefaultFilter.anon.name());
    chainDefinition.addPathDefinition("/favicon.ico", DefaultFilter.anon.name());


    chainDefinition.addPathDefinition("/logout", DefaultFilter.logout.name());
    chainDefinition.addPathDefinition("/login", DefaultFilter.anon.name());

    chainDefinition.addPathDefinition("/**", "userAuthorizationFilter");
    return chainDefinition;


@Bean(name = "userAuthorizationFilter")
public UserAuthorizationFilter userAuthorizationFilter()
    return new UserAuthorizationFilter();



@Bean
public UserRealm userRealm() 
    UserRealm userRealm = new UserRealm();
    return userRealm;


@Bean
public DefaultWebSecurityManager securityManager() 
    DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
    manager.setRealm(userRealm());
    return manager;

[mvn spring-boot:run]错误日志:

Caused by: java.lang.IllegalArgumentException: There is no filter with name 'userAuthorizationFilter' to apply to chain [/**] in the pool of available Filters.  Ensure a filter with that name/path has first been registered with the addFilter method(s).
at org.apache.shiro.web.filter.mgt.DefaultFilterChainManager.addToChain(DefaultFilterChainManager.java:265)
at org.apache.shiro.web.filter.mgt.DefaultFilterChainManager.createChain(DefaultFilterChainManager.java:148)
at org.apache.shiro.spring.web.ShiroFilterFactoryBean.createFilterChainManager(ShiroFilterFactoryBean.java:397)
at org.apache.shiro.spring.web.ShiroFilterFactoryBean.createInstance(ShiroFilterFactoryBean.java:437)
at org.apache.shiro.spring.web.ShiroFilterFactoryBean.getObject(ShiroFilterFactoryBean.java:343)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168)
... 43 common frames omitted

我是否缺少配置的重要部分?

任何帮助将不胜感激。 谢谢。

【问题讨论】:

【参考方案1】:

您似乎没有为自己的过滤器“userAuthorizationFilter”分配别名;

你应该在 ShiroFilterFactoryBean 初始化中定义别名。

@Bean(name = "shiroFilter")
public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager securityManager, UserAuthorizationFilter  authorizationFilter) 

    ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
    shiroFilterFactoryBean.setSecurityManager(securityManager);
    //...........................some other codes
    HashMap<String, Filter> map = new HashMap<>();
    map.put("userAuthorizationFilter",authorizationFilter);
    shiroFilterFactoryBean.setFilters(map);

    //...........................some other codes
    return shiroFilterFactoryBean;

【讨论】:

以上是关于spring-boot-shiro 客户过滤器不起作用的主要内容,如果未能解决你的问题,请参考以下文章

如果dataBound用于设置选定值,则Kendo UI Dropdownlist客户端过滤器不起作用

将参数传递给 AJAX 以绑定 DataTable 不起作用

获取布尔选项值

React Apollo GraphQL 搜索/过滤器

为啥这个(Django)“过滤器”功能不起作用?

DateField 的 Django 模型过滤器不起作用