【问题标题】:Apache Shiro with JdbcRealm and JndiObjectFactory throws IllegalArgumentException带有 JdbcRealm 和 JndiObjectFactory 的 Apache Shiro 抛出 IllegalArgumentException
【发布时间】:2012-08-03 08:19:40
【问题描述】:

我正在尝试在 GlassFish v3.1.2 应用服务器上的 JavaEE6 Web 应用程序中使用 Apache Shiro (v1.2)。

在 GlassFish 中,我设置了一个数据源 (jdbc/myds),我已对其进行测试以通过 JPA2 工作。

我想使用相同的数据源来验证使用 Shiro JDBC 领域的用户。

我在 shiro.ini 中有以下内容:

# DataSource config
ds                = org.apache.shiro.jndi.JndiObjectFactory 
ds.requiredType   = javax.sql.DataSource
ds.resourceName   = jdbc/myds

# JDBC realm config
jdbcRealm = org.apache.shiro.realm.jdbc.JdbcRealm
jdbcRealm.permissionsLookupEnabled = false
jdbcRealm.authenticationQuery = SELECT password FROM user_account WHERE email = ?
jdbcRealm.userRolesQuery = SELECT role_name FROM user_group_membership WHERE email = ?
jdbcRealm.dataSource = $ds

当我部署应用程序时,我得到以下异常(总结):

FINE: Encountered object reference '$ds'.  Looking up object with id 'ds'
FINEST: Applying property [dataSource] value [org.apache.shiro.jndi.JndiObjectFactory@2ca061] on object of type [org.apache.shiro.realm.jdbc.JdbcRealm]
SEVERE: Unable to start Filter: [Unable to set property 'dataSource' with value [$ds] on object of type org.apache.shiro.realm.jdbc.JdbcRealm.  If '$ds' is a reference to another (previously defined) object, prefix it with '$' to indicate that the referenced object should be used as the actual value.  For example, $$ds].
org.apache.shiro.config.ConfigurationException: Unable to set property 'dataSource' with value [$ds] on object of type org.apache.shiro.realm.jdbc.JdbcRealm.  If '$ds' is a reference to another (previously defined) object, prefix it with '$' to indicate that the referenced object should be used as the actual value.  For example, $$ds
at org.apache.shiro.config.ReflectionBuilder.applyProperty(ReflectionBuilder.java:373)
at org.apache.shiro.config.ReflectionBuilder.applySingleProperty(ReflectionBuilder.java:198)
at org.apache.shiro.config.ReflectionBuilder.applyProperty(ReflectionBuilder.java:159)
at org.apache.shiro.config.ReflectionBuilder.buildObjects(ReflectionBuilder.java:119)
...
...
Caused by: java.lang.IllegalArgumentException: Cannot invokeorg.apache.shiro.realm.jdbc.JdbcRealm.setDataSource on bean class 'class org.apache.shiro.realm.jdbc.JdbcRealm' - argument type mismatch - had objects of type "org.apache.shiro.jndi.JndiObjectFactory" but expected signature "javax.sql.DataSource"
at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2235)
at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2151)
at org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1957)
...

如果我定义一个实际的 MySQL 数据源,它可以正常工作,例如

ds = com.mysql.jdbc.jdbc2.optional.MysqlDataSource 
ds.serverName = localhost
ds.user = root
ds.password = root
ds.databaseName = mydb

非常感谢任何帮助。

【问题讨论】:

    标签: java-ee-6 shiro


    【解决方案1】:

    这看起来很可能是类路径问题。堆栈跟踪引用了在 shiro 1.1.0 中有意义的 ReflectionBuilder 的行号。但是,它们在 shiro 1.2.0 中没有意义。但是,由于 JndiObjectFactory 是 shiro 1.2.0 中的新功能,并且两个类都在 shiro-core 中,因此您的类路径中似乎有两个 jar。

    您应该首先查看您的 war 文件,确保两个 jar 都不在其中。如果是,您可以通过删除 1.1.0 版本来修复您的 war 文件。如果没有,我会开始查看您是否以某种方式通过 glassfish 基础架构继承了旧版本的 shiro。

    【讨论】:

    • 没错!升级到 1.2.0(虽然是 Maven)后,我没有进行干净的构建,所以旧的 JAR 仍然存在。谢谢!
    • 仅供参考 - 1.2.1 刚刚发布,如果您要升级,我建议您直接升级。它包括几个关键的错误修正。
    • 谢谢 - 我升级到 1.2.1 - 没有注意到 1.2.0 是否有任何问题,但它运行良好。
    猜你喜欢
    • 2015-10-02
    • 2018-04-18
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多