我有一个用于 tomcat 7 的 Java webapp,它使用 H2 数据库来存储数据以及进行用户身份验证。当我在独立的 Tomcat 上部署 webapp 时,一切正常。
现在我想用 maven 运行集成测试。我使用 2.1 版的 tomcat7-maven-plugin 当我运行集成测试 tomcat(嵌入式)时,它仅使用 tomcat-users.xml。我已将 server.xml 放入 src/main/tomcatconf 中,它将被正确复制到 target/tomcat/conf 中。
我在 server.xml 中的领域定义
<Realm className="org.apache.catalina.realm.JDBCRealm"
driverName="org.h2.Driver"
connectionURL="jdbc:h2:DB/db;USER=user;PASSWORD=password;AUTO_SERVER=TRUE;"
userTable="userAuth" userNameCol="username" userCredCol="userpass"
userRoleTable="userAuth" roleNameCol="groupname" digest="SHA-512"/>
我的 pom.xml
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<argLine>${failsafe.argLine}</argLine>
<fork>true</fork>
<path>/</path>
<contextFile>${project.build.directory}/ROOT/META-INF/context.xml</contextFile>
<systemProperties>
<JAVA_OPTS>-Xms128m -Xmx256m -XX:PermSize=512m -XX:MaxPermSize=1024m -XX:+CMSClassUnloadingEnabled</JAVA_OPTS>
</systemProperties>
</configuration>
<executions>
<execution>
<id>tomcat-run</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run-war-only</goal>
</goals>
<configuration>
<path>/</path>
<serverXml>src/main/tomcatconf/server.xml</serverXml>
<port>8080</port>
</configuration>
</execution>
<execution>
<id>tomcat-shutdown</id>
<phase>post-integration-test</phase>
<goals>
<goal>shutdown</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.173</version>
</dependency>
</dependencies>
</plugin>
我在 Internet(以及 Apache 站点)上尝试了不同的方法,但没有任何效果。当我添加一个包含我的测试用户的 tomcat-users.xml 时,测试工作。
更新
这里是日志
Running war on http://localhost:8080/
Creating Tomcat server configuration at D:\project\target\tomcat
Coping additional tomcat config files
copy server.xml
copy _tomcat-users.xml
setting SystemProperties:
JAVA_OPTS=-Xms128m -Xmx256m -XX:PermSize=512m -XX:MaxPermSize=1024m -XX:+CMSClassUnloadingEnabled
create webapp with contextPath:
Feb 10, 2014 2:44:40 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Feb 10, 2014 2:44:40 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
Feb 10, 2014 2:44:40 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.37
Feb 10, 2014 2:44:42 PM org.jboss.weld.bootstrap.WeldBootstrap <clinit>
INFO: WELD-000900 2.0.3 (Final)
Feb 10, 2014 2:44:42 PM org.jboss.weld.bootstrap.WeldBootstrap startContainer
INFO: WELD-000101 Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
Feb 10, 2014 2:44:43 PM org.jboss.weld.environment.tomcat7.Tomcat7Container initialize
INFO: Tomcat 7 detected, CDI injection will be available in Servlets and Filters. Injection into Listeners is not supported
Feb 10, 2014 2:44:43 PM org.jboss.weld.interceptor.util.InterceptionTypeRegistry <clinit>
WARNING: Class 'javax.ejb.PostActivate' not found, interception based on it is not enabled
Feb 10, 2014 2:44:43 PM org.jboss.weld.interceptor.util.InterceptionTypeRegistry <clinit>
WARNING: Class 'javax.ejb.PrePassivate' not found, interception based on it is not enabled
Feb 10, 2014 2:44:43 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
--- jacoco-maven-plugin:0.6.3.201306030806:prepare-agent (pre-integration-test) @ ILS_webapp
--- exec-maven-plugin:1.2.1:exec (gen-mvnopts-bat) @ ILS_webapp ---
--- maven-resources-plugin:2.6:copy-resources (copy-resources) @ ILS_webapp ---
Using 'UTF-8' encoding to copy filtered resources.
Copying 6 resources
我重命名了 tomcat-users.xml th _tomcat-users.xml 以避免该文件被用作领域。webapps 部署良好。只有身份验证不起作用。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<listener>
<listener-class>
org.jboss.weld.environment.servlet.Listener
</listener-class>
</listener>
<resource-env-ref>
<resource-env-ref-name>BeanManager</resource-env-ref-name>
<resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
</resource-env-ref>
<security-constraint>
<web-resource-collection>
<url-pattern>/error/*</url-pattern>
<!-- <url-pattern>/css/*</url-pattern>-->
<url-pattern>/js/*</url-pattern>
<url-pattern>/img/*</url-pattern>
<url-pattern>/api/*</url-pattern>
<url-pattern>/resources/*</url-pattern>
<url-pattern>/favicon.ico</url-pattern>
</web-resource-collection>
</security-constraint>
<security-constraint>
<display-name>secure</display-name>
<web-resource-collection>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>ADMIN</role-name>
<role-name>USER</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<realm-name>myRealm</realm-name>
<form-login-config>
<form-login-page>/index.html</form-login-page>
<form-error-page>/index_failed.html</form-error-page>
</form-login-config>
</login-config>
<security-role>
<role-name>ADMIN</role-name>
</security-role>
<security-role>
<role-name>USER</role-name>
</security-role>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>