我目前正在构build一个Java-servlet应用程序(在GlassFish上使用Jersey来具体说明)。 在应用程序的某些部分,我需要使用基本身份validation来validation用户,而在其他一些部分,我需要使用客户端证书。 使用哪一个将基于请求的path。 例如/ cert / secretMethod1或/ basic / secretMethod2。
我怎么做? 下面是我目前的web.xml,目前只做基本authentication。 我想我需要使用两个不同的,但我宁愿只使用一个身份validation领域。 是否有web.xml的标签/属性,使我可以指定不同的应用程序path不同的authentication方法?
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name> TestServer </display-name> <servlet> <description>Jersey servlet</description> <display-name>Jersey servlet</display-name> <servlet-name>JerseyServlet</servlet-name> <servlet-class> com.sun.jersey.spi.container.servlet.ServletContainer </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>JerseyServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <security-constraint> <display-name>TestServer</display-name> <web-resource-collection> <web-resource-name>TestServer</web-resource-name> <description></description> <url-pattern>/</url-pattern> <http-method>GET</http-method> <http-method>POST</http-method> <http-method>HEAD</http-method> <http-method>PUT</http-method> <http-method>OPTIONS</http-method> <http-method>TRACE</http-method> <http-method>DELETE</http-method> </web-resource-collection> <auth-constraint> <description>Have to be a USER</description> <role-name>User</role-name> </auth-constraint> </security-constraint> <login-config> <auth-method>BASIC</auth-method> <realm-name>jpaCustomRealm</realm-name> </login-config> <security-role> <description/> <role-name>User</role-name> </security-role> </web-app>
不幸的是,客户端证书身份validation在与应用程序不同的层上工作。 所以,最好的办法是将客户端redirect到另一个需要客户端证书authentication来连接的SSL侦听端口。
例如
https://example.com/basic ,使用标准的SSL连接,无需客户端authenticationvalidation。 正常工作。 https://example.com/cert ,而无需客户端证书validation。 然后被redirect到https://example.com:8443/cert ,这是一个需要客户端证书validation的SSL连接。 应用程序正常进行。 我通过创build两个独立的Web应用程序(即IntelliJ中的两个模块或Eclipse中的两个项目)解决了这个问题。 这样我可以分别configuration身份validation机制,并为不同的path有不同的机制。