大家好我正在尝试使用shiro和spring mvc 3构建登录表单 我收到了以下错误
2015-03-06 17:42:05,097 DEBUG [org.apache.shiro.spring.LifecycleBeanPostProcessor] - Initializing bean [cacheManager]...
2015-03-06 17:42:05,098 DEBUG [org.apache.shiro.cache.ehcache.EhCacheManager] - cacheManager property not set. Constructing CacheManager instance...
2015-03-06 17:42:05,102 DEBUG [org.apache.shiro.io.ResourceUtils] - Opening resource from class path [org/apache/shiro/cache/ehcache/ehcache.xml]
2015-03-06 17:42:05,148 TRACE [org.apache.shiro.cache.ehcache.EhCacheManager] - instantiated Ehcache CacheManager instance.
2015-03-06 17:42:05,148 DEBUG [org.apache.shiro.cache.ehcache.EhCacheManager] - implicit cacheManager created successfully.
2015-03-06 17:42:05,195 DEBUG [org.apache.shiro.spring.LifecycleBeanPostProcessor] - Destroying bean [cacheManager]...
2015-03-06 17:42:05,201 ERROR [org.springframework.web.context.ContextLoader] - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shiroFilter' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'securityManager' while setting bean property 'securityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'jdbcRealm' while setting bean property 'realm'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdbcRealm' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [oracle.jdbc.OracleDriver]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1325)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:722)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:410)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4701)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5204)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5199)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:662)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'jdbcRealm' while setting bean property 'realm'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdbcRealm' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [oracle.jdbc.OracleDriver]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1325)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
... 22 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdbcRealm' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [oracle.jdbc.OracleDriver]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1325)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
... 32 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [oracle.jdbc.OracleDriver]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1361)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
... 42 more
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [oracle.jdbc.OracleDriver]
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:102)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1358)
... 50 more
application context.xml
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd“>
<!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" /> <property
name="url" value="jdbc:hsqldb:mem:shiro-spring" /> <property name="username"
value="sa" /> </bean> -->
<!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/> <property
name="url" value="jdbc:oracle:thin:@192.168.0.180:1521:ORCL"/> <property
name="username" value="DEVINTEGWALLET_V3"/> <property name="password" value="DEVINTEGWALLET_V3"/>
</bean> -->
<!-- Populates the sample database with sample users and roles. -->
<bean id="bootstrapDataPopulator" class="org.apache.shiro.samples.spring.BootstrapDataPopulator">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="oracle.jdbc.OracleDriver" p:url="jdbc:oracle:thin:@192.168.0.180:1521:ORCL"
p:username="DEVINTEGWALLET_V3" p:password="DEVINTEGWALLET_V3" />
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="cacheManager" ref="cacheManager" />
<!-- Single realm app. If you have multiple realms, use the 'realms' property
instead. -->
<property name="sessionMode" value="native" />
<property name="realm" ref="jdbcRealm" />
</bean>
<!-- Let's use some enterprise caching support for better performance. You
can replace this with any enterprise caching framework implementation that
you like (Terracotta+Ehcache, Coherence, GigaSpaces, etc -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<!-- Set a net.sf.ehcache.CacheManager instance here if you already have
one. If not, a new one will be creaed with a default config: <property name="cacheManager"
ref="ehCacheManager"/> -->
<!-- If you don't have a pre-built net.sf.ehcache.CacheManager instance
to inject, but you want a specific Ehcache configuration to be used, specify
that here. If you don't, a default will be used.: <property name="cacheManagerConfigFile"
value="classpath:some/path/to/ehcache.xml"/> -->
</bean>
<!-- Used by the SecurityManager to access security data (users, roles,
etc). Many other realm implementations can be used too (PropertiesRealm,
LdapRealm, etc. -->
<bean id="jdbcRealm"
class="org.apache.shiro.samples.spring.realm.SaltAwareJdbcRealm">
<property name="name" value="jdbcRealm" />
<property name="dataSource" ref="dataSource" />
<property name="credentialsMatcher">
<!-- The 'bootstrapDataPopulator' Sha256 hashes the password (using the
username as the salt) then base64 encodes it: -->
<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<property name="hashAlgorithmName" value="SHA-256" />
<!-- true means hex encoded, false means base64 encoded -->
<property name="storedCredentialsHexEncoded" value="false" />
</bean>
</property>
</bean>
<!-- ========================================================= Shiro Spring-specific
integration ========================================================= -->
<!-- Post processor that automatically invokes init() and destroy() methods
for Spring-configured Shiro objects so you don't have to 1) specify an init-method
and destroy-method attributes for every bean definition and 2) even know
which Shiro objects require these methods to be called. -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
<!-- Enable Shiro Annotations for Spring-configured beans. Only run after
the lifecycleBeanProcessor has run: -->
<bean
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor" />
<bean
class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager" />
</bean>
<!-- Secure Spring remoting: Ensure any Spring Remoting method invocations
can be associated with a Subject for security checks. -->
<bean id="secureRemoteInvocationExecutor"
class="org.apache.shiro.spring.remoting.SecureRemoteInvocationExecutor">
<property name="securityManager" ref="securityManager" />
</bean>
<!-- Define the Shiro Filter here (as a FactoryBean) instead of directly
in web.xml - web.xml uses the DelegatingFilterProxy to access this bean.
This allows us to wire things with more control as well utilize nice Spring
things such as PropertiesPlaceholderConfigurer and abstract beans or anything
else we might need: -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<property name="loginUrl" value="/login" />
<property name="successUrl" value="/Done" />
<property name="unauthorizedUrl" value="/unauthorized" />
<!-- The 'filters' property is not necessary since any declared javax.servlet.Filter
bean defined will be automatically acquired and available via its beanName
in chain definitions, but you can perform overrides or parent/child consolidated
configuration here if you like: -->
<!-- <property name="filters"> <util:map> <entry key="aName" value-ref="someFilterPojo"/>
</util:map> </property> -->
<property name="filterChainDefinitions">
<value>
/favicon.ico = anon
/logo.png = anon
/shiro.css = anon
/s/login =
anon
# allow WebStart to pull the jars for the swing app:
/*.jar =
anon
# everything else requires authentication:
/** = authc
</value>
</property>
</bean>
LoginCommand.java
*package org.apache.shiro.samples.spring.web;
public class LoginCommand {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public LoginCommand(String username, String password) {
this.username = username;
this.password = password;
}
@Override
public String toString() {
return "LoginCommand [username=" + username + ", password=" + password
+ "]";
}
public void setPassword(String password) {
this.password = password;
}
}*
LoginController.java
package org.apache.shiro.samples.spring.web;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.naming.AuthenticationException;
import javax.validation.Valid;
@Controller("/login")
public class LoginController {
@RequestMapping("/onsubmit")
public String onSubmit(@Valid LoginCommand command, BindingResult result) {
UsernamePasswordToken token = new UsernamePasswordToken(
command.getUsername(), command.getPassword());
try {
SecurityUtils.getSubject().login(token);
} catch (Exception e) {
e.printStackTrace();
return "login";
}
if (result.hasErrors()) {
return "login";
} else {
return "success";
}
}
}
BootstrapDataPopulator.java
package org.apache.shiro.samples.spring;
import javax.sql.DataSource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.shiro.crypto.hash.Sha256Hash;
public class BootstrapDataPopulator implements InitializingBean {
private static final String CREATE_TABLES = "create table users (\n"
+ " username varchar(255) primary key,\n"
+ " password varchar(255) not null\n" + ");\n" + "\n"
+ "create table roles (\n"
+ " role_name varchar(255) primary key\n" + ");\n" + "\n"
+ "create table user_roles (\n"
+ " username varchar(255) not null,\n"
+ " role_name varchar(255) not null,\n"
+ " constraint user_roles_uq unique ( username, role_name )\n"
+ ");\n" + "\n" + "create table roles_permissions (\n"
+ " role_name varchar(255) not null,\n"
+ " permission varchar(255) not null,\n"
+ " primary key (role_name, permission)\n" + ");";
private static final Logger log = LoggerFactory
.getLogger(BootstrapDataPopulator.class);
protected DataSource dataSource = null;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void afterPropertiesSet() throws Exception {
// because we're using an in-memory hsqldb for the sample app, a new one
// will be created each time the
// app starts, so create the tables and insert the 2 sample users on
// bootstrap:
JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource);
jdbcTemplate.execute(CREATE_TABLES);
// password is 'user1' SHA hashed and base64 encoded:
// The first argument to the hash constructor is the actual value to be
// hased. The 2nd is the
// salt. In this simple demo scenario, the username and the password are
// the same, but to clarify the
// distinction, you would see this in practice:
// new Sha256Hash( <password>, <cryptographically strong randomly
// generated salt> (not the username!) )
String query = "insert into users values ('user1', '"
+ new Sha256Hash("user1", "user1").toBase64() + "' )";
jdbcTemplate.execute(query);
log.debug("Created user1.");
// password is 'user2' SHA hashed and base64 encoded:
query = "insert into users values ( 'user2', '"
+ new Sha256Hash("user2", "user2").toBase64() + "' )";
jdbcTemplate.execute(query);
log.debug("Created user2.");
query = "insert into roles values ( 'role1' )";
jdbcTemplate.execute(query);
log.debug("Created role1");
query = "insert into roles values ( 'role2' )";
jdbcTemplate.execute(query);
log.debug("Created role2");
query = "insert into roles_permissions values ( 'role1', 'permission1')";
jdbcTemplate.execute(query);
log.debug("Created permission 1 for role 1");
query = "insert into roles_permissions values ( 'role1', 'permission2')";
jdbcTemplate.execute(query);
log.debug("Created permission 2 for role 1");
query = "insert into roles_permissions values ( 'role2', 'permission1')";
jdbcTemplate.execute(query);
log.debug("Created permission 1 for role 2");
query = "insert into user_roles values ( 'user1', 'role1' )";
jdbcTemplate.execute(query);
query = "insert into user_roles values ( 'user1', 'role2' )";
jdbcTemplate.execute(query);
log.debug("Assigned user1 roles role1 and role2");
query = "insert into user_roles values ( 'user2', 'role2' )";
jdbcTemplate.execute(query);
log.debug("Assigned user2 role role2");
}
}
的login.jsp
<%@ include file="include.jsp"%>
<html>
<div id="contentBox">
<form:form method="POST" commandName="LoginCommand" action="login/onsubmit">
<table>
<tr>
<td>Login Name :</td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td>Login Password :</td>
<td><form:input path="password" /></td>
</tr>
<input type="submit" />
</table>
</form:form>
<p>Try logging in with username/passwords: user1/user1 and
user2/user2.</p>
</div>
</body>
</html>
logoutController.java
package org.apache.shiro.samples.spring.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class LogoutController {
@RequestMapping("/logout")
protected String logout(HttpServletRequest request,
HttpServletResponse response) throws Exception {
SecurityUtils.getSubject().logout();
return "login";
}
}
请帮我解决错误。谢谢
答案 0 :(得分:0)
信息很清楚:
Could not load JDBC driver class [oracle.jdbc.OracleDriver]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1361)
你错过了类路径中的oracle驱动程序。下载并将其添加到您的类路径。
答案 1 :(得分:0)
首先仔细检查在Classpath
上加载的JDBCDriver然后 在BootstrapDataPopulator.java中,afterPropertiesSet方法太长了
你可以使用类似于flyway的dbmigration
并写入弹簧测试
喜欢
@RunWith(SpringJunit4classRunner.class)
@Contextconfiguratino
public class Test{}
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/testing.html
之后,你可以更清楚地了解原因