使用JNDI配置Tomcat 8和Shiro

时间:2017-06-18 10:03:48

标签: java tomcat jndi tomcat8 shiro

我想配置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)

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

1 个答案:

答案 0 :(得分:0)

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