使用spring的Shiro给出了“未找到Shiro INI配置或发现它是空的/未配置的。”

时间:2017-04-09 10:56:33

标签: java spring spring-mvc shiro

我能够使用shiro.ini和spring运行shiro但是我想使用shiro注释,所以我试图去没有ini文件的shiro-spring。但这给了我很多时间, 错误:

org.apache.shiro.config.ConfigurationException:未找到或未发现Shiro INI配置为空/未配置。     在org.apache.shiro.web.env.IniWebEnvironment.init(IniWebEnvironment.java:87)     在org.apache.shiro.util.LifecycleUtils.init(LifecycleUtils.java:45)     在org.apache.shiro.util.LifecycleUtils.init(LifecycleUtils.java:40)     在org.apache.shiro.web.env.EnvironmentLoader.createEnvironment(EnvironmentLoader.java:226)     在org.apache.shiro.web.env.EnvironmentLoader.initEnvironment(EnvironmentLoader.java:138)     在org.apache.shiro.web.env.EnvironmentLoaderListener.contextInitialized(EnvironmentLoaderListener.java:58)     在org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4742)     at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5206)     在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)     在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1439)     在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1429)     at java.util.concurrent.FutureTask.run(Unknown Source)     在org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)     at java.util.concurrent.AbstractExecutorService.submit(Unknown Source)     在org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:953)     在org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:872)     在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)     在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1439)     在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1429)     at java.util.concurrent.FutureTask.run(Unknown Source)     在org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)     at java.util.concurrent.AbstractExecutorService.submit(Unknown Source)     在org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:953)     在org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)     在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)     at org.apache.catalina.core.StandardService.startInternal(StandardService.java:422)     在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)     at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:793)     在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)     在org.apache.catalina.startup.Catalina.start(Catalina.java:655)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)     at java.lang.reflect.Method.invoke(Unknown Source)     在org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:355)     在org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:495)

2017年4月9日下午4:15:32 org.apache.catalina.core.StandardContext startInternal

代码:Spring config:

package com.studentshare.config;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.Filter;

import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.realm.jdbc.JdbcRealm;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.studentshare")
public class AppConfig {

    @Bean
    public JdbcRealm myRealm() {
        JdbcRealm jdbcRealm = new JdbcRealm();
        jdbcRealm.setAuthenticationQuery("select password from unishare.users where user_name = ?");
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setServerName("localhost");
        dataSource.setUser("root");
        dataSource.setPassword("root");
        dataSource.setDatabaseName("unishare");

        jdbcRealm.setDataSource(dataSource);
        jdbcRealm.setCredentialsMatcher(new HashedCredentialsMatcher());
        return jdbcRealm;
    }

    @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    public DefaultWebSecurityManager securityManager(@Autowired JdbcRealm myRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(myRealm);
        return securityManager;
    }

    @Bean
    public BasicHttpAuthenticationFilter myAuthBasic(){
        return new BasicHttpAuthenticationFilter();
    }
    @Bean
    public ShiroFilterFactoryBean ShiroFilter(@Autowired DefaultWebSecurityManager securityManager,@Autowired BasicHttpAuthenticationFilter myAuthBasic) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);

        Map<String, Filter> filters = new HashMap<>();
        filters.put("myAuthcBasic", myAuthBasic);
        shiroFilter.setFilters(filters);

        /*Map<String, String> filterChainDefinitionMap = new HashMap<>();
        filterChainDefinitionMap.put("/", "authcBasic");*/
        shiroFilter.setFilterChainDefinitions("/ = myAuthcBasic");//p(filterChainDefinitionMap);
        return shiroFilter;
    }

    @DependsOn("lifecycleBeanPostProcessor")
    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        return new DefaultAdvisorAutoProxyCreator();
    }



}

网络配置:

package com.studentshare.config;

import java.util.EnumSet;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.apache.shiro.web.env.EnvironmentLoaderListener;
import org.apache.shiro.web.servlet.ShiroFilter;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class[] getRootConfigClasses() {
        return new Class[] { AppConfig.class };
    }

    @Override
    protected Class[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected Filter[] getServletFilters() {
        return new Filter[] { new ShiroFilter() };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        servletContext.addListener(EnvironmentLoaderListener.class);
        EnumSet<DispatcherType> shiroDispatchers = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD,
                DispatcherType.INCLUDE, DispatcherType.ERROR);

        FilterRegistration shiroFilter = servletContext.addFilter("ShiroFilter", DelegatingFilterProxy.class);
        shiroFilter.setInitParameter("targetFilterLifecycle", "true");
        shiroFilter.addMappingForUrlPatterns(shiroDispatchers, false,
                "/*");
    }

}

1 个答案:

答案 0 :(得分:1)

看一下1.4+版本,Spring集成已经更新(适用于Spring和Spring Boot)。

但是对于您的特定问题,您似乎正在尝试使用EnvironmentLoaderListener。使用Spring时,您需要让Spring处理加载组件的生命周期。

有关示例,请参阅:https://github.com/apache/shiro/blob/1.3.x/samples/spring-hibernate/src/main/webapp/WEB-INF/web.xml