gpt4 book ai didi

java - Shiro 使用 spring 给出 "Shiro INI configuration was either not found or discovered to be empty/unconfigured."

转载 作者:行者123 更新时间:2023-11-30 06:48:17 26 4
gpt4 key购买 nike

我能够使用 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) 在 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) 在 java.util.concurrent.FutureTask.run(来源未知) 在 org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) 在 java.util.concurrent.AbstractExecutorService.submit(来源未知) 在 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) 在 java.util.concurrent.FutureTask.run(来源未知) 在 org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) 在 java.util.concurrent.AbstractExecutorService.submit(来源未知) 在 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) 在 org.apache.catalina.core.StandardService.startInternal(StandardService.java:422) 在 org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) 在 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) 在 sun.reflect.NativeMethodAccessorImpl.invoke0( native 方法) 在 sun.reflect.NativeMethodAccessorImpl.invoke(来源未知) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(来源未知) 在 java.lang.reflect.Method.invoke(来源未知) 在 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配置:

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.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

关于java - Shiro 使用 spring 给出 "Shiro INI configuration was either not found or discovered to be empty/unconfigured.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43305899/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com