spring-boot-shiro客户过滤器无法正常工作

时间:2017-12-26 06:27:38

标签: java spring spring-boot shiro

所有

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 个答案:

答案 0 :(得分:2)

您似乎没有为自己的过滤器“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;
}