当前位置: 首页 > 知识库问答 >
问题:

使用spring的Shiro给出“Shiro INI配置不是找不到,就是发现为空/未配置。”

易自珍
2023-03-14

我能够使用shiro.ini和spring运行shiro,但我想使用shiro注释,所以我尝试使用没有ini文件的Shiro-。但这让我很难过,错误:

ConfigurationException:未找到Shiro INI配置或发现为空/未配置。在org.apache.shiro.web.env.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.environmentLoader.createEnvironmentLoader.java:226)在ds.inlineExecutorService.execute(inlineExecutorService.java:75)在java.util.concurrent.AbstractExecutorService.Submit(未知源)在org.apache.catalina.core.containerbase.startinternal(containerbase.953)在org.apache.catalina.core.StandardHost.startinternal(standardHost.java:872)在org.apache.catalina.core.StandardHost.startinternal(standardHost.java:873)在在org.apache.catalina.standardService.startinternal(StandardService.java:422)在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.183)在org.apache.catalina.core.standardServer.startinternal(StandardServer.java:793)在org.apache.catalina.util.LifecycleBase.start(LifecycleBase.183)在org.apache.catalina.start.catalina.start(Catalina.java:655)在

2017年04月09日4:15:32 PM org.apache.catalina.core.StandardContext启动内部

代码:spring配置:

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();
    }



}

Web配置:

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

芮化
2023-03-14

看看1.4+版本,spring集成已经更新(针对spring和spring boot)。

但是对于您的特定问题,看起来您正在尝试使用EnvironmentLoaderListener。在使用spring时,您将需要让spring处理装载您的组件的生命周期。

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

 类似资料:
  • 我在Spring Boot中有一个控制器映射的问题。在我添加了Spring-JPA依赖项之后,我的映射都不再起作用了。 删除spring-boot-starter-data-jpa依赖项有助于解决问题,但JPA的使用是非常严格的 控制器类位于包含main方法的类下的子包中 应用程序启动并不抛出异常。与数据库的连接也正常工作 应用程序.属性: 提前感谢您的帮助! 更新

  • 问题内容: 如文档所述,您可以使用可选参数调用webdriver.FirefoxProfile()来指向浏览器要使用的特定配置文件的目录。我注意到运行此命令花了很长时间,因此当我查看代码时,似乎正在复制指定的配置文件问题是,复制配置文件需要很长时间(大约> 30分钟,没有耐心等待它完成。) 我正在使用用户脚本和selenium的混合为我做一些自动化,因此每次想测试我的代码时都要设置一个新的配置文件

  • 问题内容: 我尝试基于使用Hibernate内存数据库配置Spring Data : 但是我一遍又一遍地得到: org.hibernate.cfg.Environment。HHH000206:找不到hibernate.properties 问题是我不想在文件中指定它,就像我以前没有Spring Data一样,我想在配置中设置它,就像我基于的答案一样。我想念什么吗?预先感谢您的帮助。 问题答案: 那

  • 我正在用Spring(无Spring Boot)构建一个Java客户端,并且必须使用Gson。 我如何告诉Spring使用Gson而不是Jackson? 为了澄清,我使用了反应式WebClient: Pojo应该是这样的: 服务器中的Json是这样的:

  • 所以,在我的工作中,我一直试图开始从Maven到Gradle的迁移,但我现在遇到了一个我似乎无法把头绕过去的严重问题。 我基本上只想为我的测试运行一些简单的liquibase迁移,为此我旋转了两个测试容器。一个用于rabbitmqexchange,一个用于postgres DB。 我使用这里描述的一个小变通方法设置了postgres容器:使用Kotlin和Testcontainers测试Sprin

  • 最近,我决定学习如何使用log4j2记录器。我下载了所需的jar文件,创建了库,xml编译文件,并尝试使用它。不幸的是,我在console(Eclipse)中得到了这样的语句: 这是我的测试类代码: 和我的xml配置文件: 我还尝试使用不带标记的xml,以及包规范和各种文件夹/包目录,但没有帮助。现在我的文件直接位于Eclipse的project文件夹中。