在Apache Shiro中初始化异常错误

时间:2014-02-04 05:46:44

标签: java mysql jdbc shiro

运行测试时出现以下错误。我不明白,因为我是shiro的新手。请帮帮我

enter code herejava.lang.ExceptionInInitializerError
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:534)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:195)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:244)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:241)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.apache.shiro.config.ConfigurationException: Unable to instantiate class [javax.sql.DataSource] for object named 'ds'.  Please ensure you've specified the fully qualified class name correctly.
at org.apache.shiro.config.ReflectionBuilder.createNewInstance(ReflectionBuilder.java:151)
at org.apache.shiro.config.ReflectionBuilder.buildObjects(ReflectionBuilder.java:119)
at org.apache.shiro.config.IniSecurityManagerFactory.buildInstances(IniSecurityManagerFactory.java:161)
at org.apache.shiro.config.IniSecurityManagerFactory.createSecurityManager(IniSecurityManagerFactory.java:124)
at org.apache.shiro.config.IniSecurityManagerFactory.createSecurityManager(IniSecurityManagerFactory.java:102)
at org.apache.shiro.config.IniSecurityManagerFactory.createInstance(IniSecurityManagerFactory.java:88)
at org.apache.shiro.config.IniSecurityManagerFactory.createInstance(IniSecurityManagerFactory.java:46)
at org.apache.shiro.config.IniFactorySupport.createInstance(IniFactorySupport.java:123)
at com.advi.test.shiro.TestShiro.<clinit>(TestShiro.java:23)
... 22 more
Caused by: org.apache.shiro.util.InstantiationException: Unable to instantiate class [javax.sql.DataSource]
at org.apache.shiro.util.ClassUtils.newInstance(ClassUtils.java:179)
at org.apache.shiro.util.ClassUtils.newInstance(ClassUtils.java:164)
at org.apache.shiro.config.ReflectionBuilder.createNewInstance(ReflectionBuilder.java:144)
... 30 more
Caused by: java.lang.InstantiationException: javax.sql.DataSource
at java.lang.Class.newInstance(Class.java:359)
at org.apache.shiro.util.ClassUtils.newInstance(ClassUtils.java:177)
... 32 more

这是shiro.ini文件。请让我知道我做了什么错误

#myRealm = MyRealm
#myRealm.credentialsMatcher = $myRealmCredentialsMatcher


ps = org.apache.shiro.authc.credential.DefaultPasswordService
pm = org.apache.shiro.authc.credential.PasswordMatcher
pm.passwordService = $ps

aa = org.apache.shiro.authc.credential.AllowAllCredentialsMatcher
sm = org.apache.shiro.authc.credential.SimpleCredentialsMatcher

#jof = org.apache.shiro.jndi.JndiObjectFactory
#jof.resourceName = jdbc/UserDB
#jof.requiredType = javax.sql.DataSource
#jof.resourceRef = true
#; Note factories are automatically invoked via getInstance(),
#;   see org.apache.shiro.authc.config.ReflectionBuilder::resolveReference
#jdbcRealm.dataSource = $jof

ds = javax.sql.DataSource
ds.url = jdbc:mysql://192.168.1.111:3306/Test
ds.user = dbuser
ds.password = dbuser

jdbcRealm = org.apache.shiro.realm.jdbc.JdbcRealm
#realm.permissionsLookupEnabled = true
jdbcRealm.credentialsMatcher = $pm
jdbcRealm.dataSource = $ds

securityManager.realms = $jdbcRealm


jdbcRealm = org.apache.shiro.realm.jdbc.JdbcRealm

#ds = javax.sql.DataSource
#ds.url = jdbc:mysql://192.168.1.111:3306/Test
#ds.user = dbuser
#ds.password = dbuser
#jdbcRealm.dataSource = $ds

# Configure JDBC realm SQL queries.
jdbcRealm.authenticationQuery = select pass from users where name = ?;
sha256Matcher = org.apache.shiro.authc.credential.HashedCredentialsMatcher
sha256Matcher.hashAlgorithmName=SHA-256
# enable matcher in iniRealm (object responsible for authentication)
iniRealm.credentialsMatcher = $sha256Matcher

它说它无法启动java.sql.datasource ..我应该怎么做才能启动

1 个答案:

答案 0 :(得分:2)

在MySQL使用的情况下,您需要指定实例化的具体DataSource:

com.mysql.jdbc.jdbc2.optional.MysqlDataSource

com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource

另请参阅:https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html

而不是接口javax.sql.DataSource - 无法构造,异常告诉你:

  

引起:org.apache.shiro.config.ConfigurationException:无法执行   实例化类[javax.sql.DataSource]

您还需要在类路径中包含MySQL Connector JAR文件