【问题标题】:Initialize Exception Error in Apache Shiro在 Apache Shiro 中初始化异常错误
【发布时间】:2018-05-10 19:40:01
【问题描述】:

运行测试时出现以下错误。我不明白这一点,因为我是 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..我应该怎么做才能启动它

【问题讨论】:

    标签: java mysql jdbc shiro


    【解决方案1】:

    您需要指定实例化的具体数据源,以防 MySQL 使用:

    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 连接器 JAR 文件

    【讨论】:

    • 感谢您的回答,但即使我使用上述两个数据源代码也会出现相同的错误。而且我有 MYSQL 连接器 jar 文件
    • 是的..但我通过使用this 并参考shiro-user.582556.n2.nabble.com/… 解决了错误
    猜你喜欢
    • 1970-01-01
    • 2012-02-01
    • 1970-01-01
    • 2012-03-29
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    • 2021-06-30
    相关资源
    最近更新 更多