2017-06-18 98 views
0

我想配置Apache Shiro以使用JNDI来认证用户。 我配置shiro.ini使用JNDI配置Tomcat 8和Shiro

[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) 

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

回答

0

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