gpt4 book ai didi

java - 使用 JNDI 配置 Tomcat 8 和 Shiro

转载 作者:行者123 更新时间:2023-11-28 23:20:23 26 4
gpt4 key购买 nike

我想配置 Apache Shiro 使用 JNDI 来验证用户。我配置了shiro.ini

[main]
shiro.loginUrl = /authentication/login.xhtml
dataSource = org.apache.shiro.jndi.JndiObjectFactory
dataSource.requiredType = javax.sql.DataSource
dataSource.resourceName = jdbc/sqliteDB
dataSource.resourceRef = true
jdbcRealm = com.web.authentication.JdbcRealm
jdbcRealm.dataSource = $dataSource
jdbcRealm.permissionsLookupEnabled = true
securityManager.realm = $jdbcRealm

我在 WEB-INF/web.xml 中添加了这些行:

<resource-ref>
<description>postgreSQL Datasource example</description>
<res-ref-name>jdbc/sqliteDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

Tomcat/conf/context.xml

<Server port="8009" shutdown="SHUTDOWN">
..................
<Resource auth="Container"
driverClassName="org.sqlite.JDBC"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
maxIdle="2" maxTotal="60"
name="java:comp/env/jdbc/sqliteDB"
password=""
type="javax.sql.DataSource"
url="jdbc:sqlite:/C:/sqlite/test.sqlite"
username=""
validationQuery="select sqlite_version();"/>
.......................
</Host>
</Engine>
</Service>
</Server>

但是当我部署包时出现错误:

 java.lang.IllegalStateException: Unable to look up javax.sql.DataSource with jndi name 'jdbc/sqliteDB'.
at org.apache.shiro.jndi.JndiObjectFactory.getInstance(JndiObjectFactory.java:44)
at org.apache.shiro.config.ReflectionBuilder.resolveReference(ReflectionBuilder.java:417)
Caused by: javax.naming.NameNotFoundException: Name [jdbc/sqliteDB] is not bound in this Context. Unable to find [jdbc].
at org.apache.naming.NamingContext.lookup(NamingContext.java:816)
at org.apache.naming.NamingContext.lookup(NamingContext.java:173)

你能给我一些建议,我该如何解决这个问题?

最佳答案

请尝试将 context.xml 中的 name="java:comp/env/jdbc/sqliteDB" 行更改为 name="jdbc/sqliteDB"

关于java - 使用 JNDI 配置 Tomcat 8 和 Shiro,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44613848/

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