【发布时间】:2017-01-27 01:48:55
【问题描述】:
我想在嵌入式 tomcat 中添加两个 servlet。在这种情况下,其中一个 servlet 应该受到基本身份验证的“保护”。我只想通过代码添加安全约束。关于这个Link,应该不难。
我构建了一个测试场景:
项目:EmbeddedTomcatTest
-Source Packages
--tomcat.test
---ServletOne.java
---ServletTwo.java
---StartEmbeddedTomcat.java (contains main method)
-Test Packages
-Other Sources
--src/main/resources
---Tomcat-users.xml
-Project Files
--pom.xml
我的 pom 的依赖如下:
<dependencies>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>8.5.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
到目前为止,我添加了一个带有两个 servlet 的嵌入式 tomcat,这两个 servlet 可以通过不同的 url 模式访问,效果很好。
public class StartEmbeddedTomcat {
private static final String AUTH_ROLE = "test";
public static void main(String[] args) throws LifecycleException {
Tomcat tomcat = new Tomcat();
tomcat.setPort(8080);
// adding a context
Context ctx = tomcat.addContext("/", new File(".").getAbsolutePath());
// Login Config
LoginConfig config = new LoginConfig();
config.setAuthMethod("BASIC");
// adding constraint with role "test"
SecurityConstraint constraint = new SecurityConstraint();
constraint.addAuthRole(AUTH_ROLE);
// add constraint to a collection with pattern /second
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/second");
constraint.addCollection(collection);
ctx.setLoginConfig(config);
// does the context need a auth role too?
ctx.addSecurityRole(AUTH_ROLE);
ctx.addConstraint(constraint);
// add servlet with pattenr /first and /second
Tomcat.addServlet(ctx, "one", new ServletOne());
ctx.addServletMapping("/first", "one");
Tomcat.addServlet(ctx, "two", new ServletTwo());
ctx.addServletMapping("/second", "two");
// add tomcat users to realm
String path = "tomcat-users.xml";
URL uri = ServletOne.class.getClassLoader().getResource(path);
MemoryRealm realm = new MemoryRealm();
realm.setPathname(uri.toString());
tomcat.getEngine().setRealm(realm);
tomcat.start();
tomcat.getServer().await();
}
}
tomcat-users.xml 不是很特别。
<tomcat-users>
<role rolename="manager-gui"/>
<role rolename="test"/>
<user name="admin" password="" roles="manager-gui"/>
<user name="test" password="test" roles="test"/>
</tomcat-users>
我认为 servlet 的代码并不重要,因为它们只是响应“Im servlet one”或“Im servlet two”。
在我看来,整个应用程序或至少对/second 的每个请求都应该受到保护。我做错了什么?我还有什么事情要做吗?
----编辑---
今天我发现了这个question 的堆栈溢出。我在我的测试场景中添加了noauthCTX.setAltDDName("Path\\to\\web.xml");。 web.xml 看起来像:
<web-app>
<display-name>Test Service</display-name>
<servlet>
<servlet-name>ServletOne</servlet-name>
<servlet-class>tomcat.test.test.ServletOne</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletOne</servlet-name>
<url-pattern>/servletOne/*</url-pattern>
</servlet-mapping>
</web-app>
我认为我的 servlet 也可以通过 localhost:8080/servletOne/* 访问。这不是..实际上我很困惑。我希望有人可以帮助我..
【问题讨论】:
标签: java tomcat servlets basic-authentication