Skip to content

Commit f1a8fb6

Browse files
committed
Added sample for Servlet DIGEST authentication
1 parent b33396c commit f1a8fb6

10 files changed

Lines changed: 585 additions & 28 deletions

File tree

pom.xml

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
<skipJAXWS>${skipTests}</skipJAXWS>
2727
<skipEAR>${skipTests}</skipEAR>
2828

29+
<skipServletClientCertificate>${skipTests</skipServletClientCertificate>
30+
2931
<!--
3032
Application Server versions
3133
(these are downloaded and installed in these versions by Maven for the CI profiles)
@@ -357,10 +359,6 @@
357359
<profile>
358360
<id>payara-ci-managed</id>
359361

360-
<activation>
361-
<activeByDefault>true</activeByDefault>
362-
</activation>
363-
364362
<dependencies>
365363
<!-- Java EE based client dependencies to contact a server via WebSocket or REST -->
366364
<dependency>
@@ -426,10 +424,15 @@
426424
<id>payara-micro-managed</id>
427425

428426
<properties>
427+
<!-- JMS and JAXWS not supported by Micro -->
429428
<skipJMS>true</skipJMS>
430429
<skipJAXWS>true</skipJAXWS>
430+
431431
<!-- Being a WebProfile++, Micro does not support ear archives -->
432432
<skipEAR>true</skipEAR>
433+
434+
<!-- Client-cert needs complicated cert setup, which hasn't been done -->
435+
<skipServletClientCertificate>true</skipServletClientCertificate>
433436
</properties>
434437

435438
<dependencies>
@@ -490,6 +493,12 @@
490493

491494
<profile>
492495
<id>payara-embedded</id>
496+
497+
<properties>
498+
<!-- Client-cert needs complicated cert setup, which hasn't been done -->
499+
<skipServletClientCertificate>true</skipServletClientCertificate>
500+
</properties>
501+
493502
<dependencies>
494503
<!-- Java EE based client dependencies to contact a server via WebSocket or REST -->
495504
<dependency>
@@ -566,6 +575,12 @@
566575

567576
<profile>
568577
<id>glassfish-embedded</id>
578+
579+
<properties>
580+
<!-- Client-cert needs complicated cert setup, which hasn't been done -->
581+
<skipServletClientCertificate>true</skipServletClientCertificate>
582+
</properties>
583+
569584
<dependencies>
570585
<dependency>
571586
<groupId>org.glassfish.main.extras</groupId>
@@ -694,9 +709,12 @@
694709

695710
<profile>
696711
<id>wildfly-embedded</id>
712+
697713
<properties>
698714
<serverProfile>standalone-full.xml</serverProfile>
699715
<serverRoot>${project.build.directory}/wildfly-${wildfly.version}</serverRoot>
716+
<!-- Client-cert needs complicated cert setup, which hasn't been done -->
717+
<skipServletClientCertificate>true</skipServletClientCertificate>
700718
</properties>
701719
<dependencies>
702720
<!-- Needed for websocket tests that use ContainerProvider.getWebSocketContainer()
@@ -810,10 +828,14 @@
810828

811829
<profile>
812830
<id>wildfly-ci-managed</id>
831+
813832
<properties>
814833
<serverProfile>standalone-full.xml</serverProfile>
815834
<serverRoot>${project.build.directory}/wildfly-${wildfly.version}</serverRoot>
835+
<!-- Client-cert needs complicated cert setup, which hasn't been done -->
836+
<skipServletClientCertificate>true</skipServletClientCertificate>
816837
</properties>
838+
817839
<dependencies>
818840
<!-- Needed for websocket tests that use ContainerProvider.getWebSocketContainer()
819841
to connect from a the client to the websocket server.
@@ -921,6 +943,11 @@
921943
<profile>
922944
<id>wildfly-remote</id>
923945

946+
<properties>
947+
<!-- Client-cert needs complicated cert setup, which hasn't been done -->
948+
<skipServletClientCertificate>true</skipServletClientCertificate>
949+
</properties>
950+
924951
<dependencies>
925952
<!-- Needed for websocket tests that use ContainerProvider.getWebSocketContainer()
926953
to connect from a the client to the websocket server.
@@ -986,6 +1013,11 @@
9861013

9871014
<profile>
9881015
<id>wildfly-swarm</id>
1016+
1017+
<properties>
1018+
<!-- Client-cert needs complicated cert setup, which hasn't been done -->
1019+
<skipServletClientCertificate>true</skipServletClientCertificate>
1020+
</properties>
9891021

9901022
<dependencies>
9911023
<!-- Needed for websocket tests that use ContainerProvider.getWebSocketContainer()
@@ -1066,6 +1098,13 @@
10661098

10671099
<profile>
10681100
<id>tomee-embedded</id>
1101+
1102+
<properties>
1103+
<!-- Client-cert needs complicated cert setup, which hasn't been done -->
1104+
<skipServletClientCertificate>true</skipServletClientCertificate>
1105+
</properties>
1106+
1107+
10691108
<dependencies>
10701109
<dependency>
10711110
<groupId>org.apache.tomee</groupId>
@@ -1125,6 +1164,11 @@
11251164
<profile>
11261165
<id>tomee-ci-managed</id>
11271166

1167+
<properties>
1168+
<!-- Client-cert needs complicated cert setup, which hasn't been done -->
1169+
<skipServletClientCertificate>true</skipServletClientCertificate>
1170+
</properties>
1171+
11281172
<repositories>
11291173
<repository>
11301174
<id>apache.snapshots</id>
@@ -1234,6 +1278,11 @@
12341278
<profile>
12351279
<id>liberty-managed</id> <!-- Can also be used as remote -->
12361280

1281+
<properties>
1282+
<!-- Client-cert needs complicated cert setup, which hasn't been done -->
1283+
<skipServletClientCertificate>true</skipServletClientCertificate>
1284+
</properties>
1285+
12371286
<dependencies>
12381287
<dependency>
12391288
<groupId>org.jboss.arquillian.container</groupId>
@@ -1290,6 +1339,11 @@
12901339

12911340
<profile>
12921341
<id>liberty-ci-managed</id>
1342+
1343+
<properties>
1344+
<!-- Client-cert needs complicated cert setup, which hasn't been done -->
1345+
<skipServletClientCertificate>true</skipServletClientCertificate>
1346+
</properties>
12931347

12941348
<dependencies>
12951349
<dependency>
@@ -1387,6 +1441,9 @@
13871441
<!-- Default target after having installed developer zip distribution for
13881442
WebLogic -->
13891443
<weblogicRemoteArquillian_target>myserver</weblogicRemoteArquillian_target>
1444+
1445+
<!-- Client-cert needs complicated cert setup, which hasn't been done -->
1446+
<skipServletClientCertificate>true</skipServletClientCertificate>
13901447
</properties>
13911448

13921449
<dependencies>
@@ -1429,6 +1486,8 @@
14291486
<skipCDI>true</skipCDI>
14301487
<skipJSF>true</skipJSF>
14311488
<skipJACC>true</skipJACC>
1489+
<!-- Client-cert needs complicated cert setup, which hasn't been done -->
1490+
<skipServletClientCertificate>true</skipServletClientCertificate>
14321491
</properties>
14331492

14341493
<dependencies>
@@ -1463,6 +1522,8 @@
14631522
<skipCDI>true</skipCDI>
14641523
<skipJSF>true</skipJSF>
14651524
<skipJACC>true</skipJACC>
1525+
<!-- Client-cert needs complicated cert setup, which hasn't been done -->
1526+
<skipServletClientCertificate>true</skipServletClientCertificate>
14661527
</properties>
14671528

14681529
<repositories>

servlet/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@
3030

3131
<!-- Security samples assuming the container identity store -->
3232
<module>security-basicauth</module>
33+
<module>security-digest</module>
3334
<module>security-form-based</module>
35+
<module>security-clientcert</module>
36+
37+
3438
<module>security-programmatic</module>
3539
<module>security-deny-uncovered</module>
3640
<module>security-annotated</module>

servlet/security-clientcert/pom.xml

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,16 @@
2828
</dependency>
2929
</dependencies>
3030

31-
<profiles>
32-
<profile>
33-
<id>payara-micro-managed</id>
34-
<build>
35-
<testResources>
36-
<testResource>
37-
<directory>src/test/resources</directory>
38-
<filtering>true</filtering>
39-
</testResource>
40-
</testResources>
41-
<plugins>
42-
<plugin>
43-
<artifactId>maven-surefire-plugin</artifactId>
44-
<configuration>
45-
<systemProperties>
46-
<payara.extraMicroOptions>--postdeploycommandfile ${project.build.directory}/test-classes/addUsersPayara.txt</payara.extraMicroOptions>
47-
</systemProperties>
48-
</configuration>
49-
</plugin>
50-
</plugins>
51-
</build>
52-
</profile>
53-
</profiles>
31+
<build>
32+
<plugins>
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-surefire-plugin</artifactId>
36+
<configuration>
37+
<skipTests>${skipServletClientCertificate}</skipTests>
38+
</configuration>
39+
</plugin>
40+
</plugins>
41+
</build>
42+
5443
</project>

servlet/security-digest/pom.xml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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>
3+
4+
<parent>
5+
<groupId>org.javaee7</groupId>
6+
<artifactId>servlet</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>servlet-security-digest</artifactId>
11+
<packaging>war</packaging>
12+
13+
<name>Java EE 7 Sample: servlet - security-digest</name>
14+
15+
<build>
16+
<plugins>
17+
<plugin>
18+
<groupId>org.apache.maven.plugins</groupId>
19+
<artifactId>maven-surefire-plugin</artifactId>
20+
<configuration>
21+
<skipTests>${skipServletClientCertificate}</skipTests>
22+
<systemProperties>
23+
<org.apache.commons.logging.Log>org.apache.commons.logging.impl.SimpleLog</org.apache.commons.logging.Log>
24+
<org.apache.commons.logging.simplelog.showdatetime>true</org.apache.commons.logging.simplelog.showdatetime>
25+
<org.apache.commons.logging.simplelog.log.org.apache.http>DEBUG</org.apache.commons.logging.simplelog.log.org.apache.http>
26+
<org.apache.commons.logging.simplelog.log.org.apache.http.wire>ERROR</org.apache.commons.logging.simplelog.log.org.apache.http.wire>
27+
</systemProperties>
28+
</configuration>
29+
</plugin>
30+
</plugins>
31+
</build>
32+
33+
<dependencies>
34+
<dependency>
35+
<groupId>commons-codec</groupId>
36+
<artifactId>commons-codec</artifactId>
37+
<version>1.11</version>
38+
</dependency>
39+
</dependencies>
40+
41+
<profiles>
42+
<profile>
43+
<id>payara-micro-managed</id>
44+
<build>
45+
<testResources>
46+
<testResource>
47+
<directory>src/test/resources</directory>
48+
<filtering>true</filtering>
49+
</testResource>
50+
</testResources>
51+
<plugins>
52+
<plugin>
53+
<artifactId>maven-surefire-plugin</artifactId>
54+
<configuration>
55+
<systemProperties>
56+
<payara.extraMicroOptions>--postdeploycommandfile ${project.build.directory}/test-classes/addUsersPayara.txt</payara.extraMicroOptions>
57+
</systemProperties>
58+
</configuration>
59+
</plugin>
60+
</plugins>
61+
</build>
62+
</profile>
63+
</profiles>
64+
</project>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/** Copyright Payara Services Limited **/
2+
package org.javaee7.servlet.security.digest;
3+
4+
import static org.apache.commons.codec.digest.DigestUtils.md5Hex;
5+
6+
import java.sql.Connection;
7+
import java.sql.PreparedStatement;
8+
import java.sql.SQLException;
9+
10+
import javax.annotation.Resource;
11+
import javax.servlet.ServletContextEvent;
12+
import javax.servlet.ServletContextListener;
13+
import javax.servlet.annotation.WebListener;
14+
import javax.sql.DataSource;
15+
16+
17+
/**
18+
*
19+
* @author Arjan Tijms
20+
*
21+
*/
22+
@WebListener
23+
public class DatabaseSetup implements ServletContextListener {
24+
25+
@Resource
26+
private DataSource dataSource;
27+
28+
@Override
29+
public void contextInitialized(ServletContextEvent sce) {
30+
System.out.println("Creating DB tables");
31+
32+
// Note "eesamplesdigestrealm" is the name of the realm as defined in web.xml:
33+
//
34+
//<login-config>
35+
// <auth-method>DIGEST</auth-method>
36+
// <realm-name>eesamplesdigestrealm</realm-name>
37+
//</login-config>
38+
39+
String ha1 = md5Hex("u1" + ":" + "eesamplesdigestrealm" + ":" + "p1");
40+
41+
System.out.println("ha1=" + ha1);
42+
43+
execute(dataSource, "DROP TABLE IF EXISTS usertable");
44+
execute(dataSource, "DROP TABLE IF EXISTS grouptable");
45+
46+
System.out.println("Adding user u1 with group g1 to database");
47+
48+
execute(dataSource, "CREATE TABLE IF NOT EXISTS usertable(username VARCHAR(32) PRIMARY KEY, password VARCHAR(127))");
49+
execute(dataSource, "CREATE TABLE IF NOT EXISTS grouptable(username VARCHAR(64), groupname VARCHAR(64))");
50+
51+
execute(dataSource, "INSERT INTO usertable VALUES('u1', '" + ha1 + "')");
52+
53+
execute(dataSource, "INSERT INTO grouptable VALUES('u1', 'g1')");
54+
}
55+
56+
@Override
57+
public void contextDestroyed(ServletContextEvent sce) {
58+
try {
59+
execute(dataSource, "DROP TABLE IF EXISTS usertable");
60+
execute(dataSource, "DROP TABLE IF EXISTS grouptable");
61+
} catch (Exception e) {
62+
}
63+
64+
}
65+
66+
private void execute(DataSource dataSource, String query) {
67+
try (
68+
Connection connection = dataSource.getConnection();
69+
PreparedStatement statement = connection.prepareStatement(query);
70+
) {
71+
statement.executeUpdate();
72+
} catch (SQLException e) {
73+
throw new IllegalStateException(e);
74+
}
75+
}
76+
77+
}

0 commit comments

Comments
 (0)