Skip to content

Commit

Permalink
Added test for Servlet client-cert authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
arjantijms committed Jun 21, 2018
1 parent cbd30dc commit c20bf73
Show file tree
Hide file tree
Showing 8 changed files with 575 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.13</version>
<version>2.31</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
2 changes: 2 additions & 0 deletions servlet/security-clientcert/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/clientKeyStore.jks
/clientTrustStore.jks
54 changes: 54 additions & 0 deletions servlet/security-clientcert/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.javaee7</groupId>
<artifactId>servlet</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>servlet-security-clientcert</artifactId>
<packaging>war</packaging>

<name>Java EE 7 Sample: servlet - security-clientcert</name>

<dependencies>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.59</version>
</dependency>

<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.59</version>
</dependency>
</dependencies>

<profiles>
<profile>
<id>payara-micro-managed</id>
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemProperties>
<payara.extraMicroOptions>--postdeploycommandfile ${project.build.directory}/test-classes/addUsersPayara.txt</payara.extraMicroOptions>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/** Copyright Payara Services Limited **/
package org.javaee7.servlet.security.clientcert;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @author Arjan Tijms
*/
@WebServlet(urlPatterns = { "/SecureServlet" })
public class SecureServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().print("principal " + request.getUserPrincipal() + " in role g1:" + request.isUserInRole("g1"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/** Copyright Payara Services Limited **/
-->
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>

<security-role-mapping>
<role-name>g1</role-name>
<group-name>g1</group-name>
<principal-name>C=UK, ST=lak, L=zak, O=kaz, OU=bar, CN=lfoo</principal-name>
</security-role-mapping>

</glassfish-web-app>
29 changes: 29 additions & 0 deletions servlet/security-clientcert/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/** Copyright Payara Services Limited **/
-->
<web-app 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"
version="3.1">

<security-constraint>
<web-resource-collection>
<web-resource-name>SecureServlet</web-resource-name>
<url-pattern>/SecureServlet</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>g1</role-name>
</auth-constraint>
</security-constraint>

<login-config>
<auth-method>CLIENT-CERT</auth-method>
</login-config>

<security-role>
<role-name>g1</role-name>
</security-role>
</web-app>
Loading

0 comments on commit c20bf73

Please sign in to comment.