对 GlassFish 使用基本和客户端证书身份验证

5 certificate glassfish java authentication servlet

我目前正在构建一个 Java-servlet 应用程序(具体来说,在 GlassFish 上使用 Jersey)。在应用程序的某些部分,我需要使用基本身份验证对用户进行身份验证,而在其他一些部分,我需要使用客户端证书。使用哪一个将取决于请求的路径。例如 /cert/secretMethod1 或 /basic/secretMethod2 。

我怎么做?下面是我当前的 web.xml,它目前只进行基本的身份验证。我想我需要使用两种不同的,但我更愿意只使用一种身份验证领域。是否有 web.xml 的标签/属性使我能够为应用程序的不同路径指定不同的身份验证方法?

<?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>
Run Code Online (Sandbox Code Playgroud)

小智 2

我通过创建两个单独的 Web 应用程序(即 IntelliJ 中的两个模块或 Eclipse 中的两个项目)解决了这个问题。这样我就可以单独配置身份验证机制,并针对不同的路径使用不同的机制。