Skip to content

Commit 6184b4e

Browse files
committed
[WFLY-15167] added simple-webbapp-oidc quickstart
1 parent 20dc56b commit 6184b4e

File tree

8 files changed

+346
-1
lines changed

8 files changed

+346
-1
lines changed

oidc-webapp/README.adoc

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
include::../shared-doc/attributes.adoc[]
2+
3+
= oidc-webapp: Securing an application deployed to WildFly with OpenID Connect (OIDC)
4+
:toc: left
5+
:icons: font
6+
:idprefix:
7+
:idseparator: -
8+
:level: Beginner
9+
:technologies: OIDC
10+
:openshift: false
11+
12+
[abstract]
13+
The `oidc-webapp` quickstart demonstrates a simple application, bundled and deployed as a WAR, secured with OpenID Connect (OIDC).
14+
15+
:standalone-server-type: custom
16+
:serverArguments: -Djboss.socket.binding.port-offset=10
17+
:archiveType: war
18+
:mavenDeployArgs: wildfly:deploy -Dwildfly.port=10000
19+
20+
== What is it?
21+
22+
The `oidc-webapp` quickstart demonstrates how to secure an application deployed to {productNameFull} with OpenID Connect
23+
(OIDC) without needing to use the Keycloak client adapter.
24+
25+
The OIDC configuration in this example is part of the deployment itself. Alternatively,
26+
this configuration could be specified via the `elytron-oidc-client` subsystem instead.
27+
For more details, take a look at the https://docs.wildfly.org/28/Admin_Guide.html#Elytron_OIDC_Client[documentation].
28+
29+
// System Requirements
30+
include::../shared-doc/system-requirements.adoc[leveloffset=+1]
31+
// Use of {jbossHomeName}
32+
include::../shared-doc/use-of-jboss-home-name.adoc[leveloffset=+1]
33+
34+
== Set up your Keycloak OpenID provider
35+
36+
Follow the steps in this https://www.keycloak.org/getting-started/getting-started-docker[getting started guide] to
37+
start Keycloak, create a realm called `myrealm`, create a user called `myuser`, and register a client called `myclient`.
38+
39+
After registering our client, `myclient`, we also need to configure valid redirect URIs. Simply click
40+
on `Clients` and then on `myclient`. In the `Valid Redirect URIs` field, enter http://localhost:8090/oidc-webapp/*.
41+
42+
// build and run with standard server distribution
43+
[[build_and_run_the_quickstart_with_server_dist]]
44+
== Building and running the quickstart application with a {productName} server distribution
45+
46+
First, we're going to start our WildFly instance (notice that we're specifying a port offset here
47+
since our Keycloak instance is already exposed on port 8080).
48+
49+
// Start the {productName} Standalone Server
50+
include::../shared-doc/start-the-standalone-server.adoc[leveloffset=+2]
51+
// Build and Deploy the Quickstart
52+
include::../shared-doc/build-and-deploy-the-quickstart.adoc[leveloffset=+2]
53+
54+
## Access the app
55+
56+
We can access our application using `http://localhost:8090/oidc-webapp/`.
57+
58+
Click on "Access Secured Servlet".
59+
60+
Now, you'll be redirected to Keycloak to log in. Log in with `myuser` and the password that you
61+
set when configuring Keycloak.
62+
63+
Next, you'll be redirected back to our application and you should see the "Secured Servlet" page.
64+
65+
We were able to successfully log in to our application via the Keycloak OpenID provider!
66+
67+
// Build and run sections for other environments/builds
68+
ifndef::ProductRelease,EAPXPRelease[]
69+
include::../shared-doc/build-and-run-the-quickstart-with-provisioned-server.adoc[leveloffset=+1]
70+
endif::[]

oidc-webapp/pom.xml

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
JBoss, Home of Professional Open Source
4+
Copyright 2023, Red Hat, Inc. and/or its affiliates, and individual
5+
contributors by the @authors tag. See the copyright.txt in the
6+
distribution for a full listing of individual contributors.
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
<parent>
22+
<groupId>org.wildfly.quickstarts</groupId>
23+
<artifactId>wildfly-quickstart-parent</artifactId>
24+
<!--
25+
Maintain separation between the artifact id and the version to help prevent
26+
merge conflicts between commits changing the GA and those changing the V.
27+
-->
28+
<version>3</version>
29+
<relativePath/>
30+
</parent>
31+
<artifactId>oidc-webapp</artifactId>
32+
<version>29.0.0.Alpha1-SNAPSHOT</version>
33+
<packaging>war</packaging>
34+
<name>Quickstart: OIDC Webapp</name>
35+
<description>This quickstart demonstrates how to secure an application with OpenID Connect (OIDC) without needing to use the Keycloak client adapter</description>
36+
37+
<licenses>
38+
<license>
39+
<name>Apache License, Version 2.0</name>
40+
<url>http://www.apache.org/licenses/LICENSE-2.0.html</url>
41+
<distribution>repo</distribution>
42+
</license>
43+
</licenses>
44+
45+
<properties>
46+
<!-- The versions for BOMs, Dependencies and Plugins -->
47+
<version.server.bom>28.0.0.Final</version.server.bom>
48+
<version.server>28.0.0.Final</version.server>
49+
<version.wildfly.maven.plugin>4.1.0.Final</version.wildfly.maven.plugin>
50+
<version.cloud.fp>3.0.0.Final</version.cloud.fp>
51+
</properties>
52+
53+
<repositories>
54+
<repository>
55+
<id>jboss-public-maven-repository</id>
56+
<name>JBoss Public Maven Repository</name>
57+
<url>https://repository.jboss.org/nexus/content/groups/public/</url>
58+
<releases>
59+
<enabled>true</enabled>
60+
<updatePolicy>never</updatePolicy>
61+
</releases>
62+
<snapshots>
63+
<enabled>true</enabled>
64+
<updatePolicy>never</updatePolicy>
65+
</snapshots>
66+
<layout>default</layout>
67+
</repository>
68+
<repository>
69+
<id>redhat-ga-maven-repository</id>
70+
<name>Red Hat GA Maven Repository</name>
71+
<url>https://maven.repository.redhat.com/ga/</url>
72+
<releases>
73+
<enabled>true</enabled>
74+
<updatePolicy>never</updatePolicy>
75+
</releases>
76+
<snapshots>
77+
<enabled>true</enabled>
78+
<updatePolicy>never</updatePolicy>
79+
</snapshots>
80+
<layout>default</layout>
81+
</repository>
82+
</repositories>
83+
<pluginRepositories>
84+
<pluginRepository>
85+
<id>jboss-public-maven-repository</id>
86+
<name>JBoss Public Maven Repository</name>
87+
<url>https://repository.jboss.org/nexus/content/groups/public/</url>
88+
<releases>
89+
<enabled>true</enabled>
90+
</releases>
91+
<snapshots>
92+
<enabled>true</enabled>
93+
</snapshots>
94+
</pluginRepository>
95+
<pluginRepository>
96+
<id>redhat-ga-maven-repository</id>
97+
<name>Red Hat GA Maven Repository</name>
98+
<url>https://maven.repository.redhat.com/ga/</url>
99+
<releases>
100+
<enabled>true</enabled>
101+
</releases>
102+
<snapshots>
103+
<enabled>true</enabled>
104+
</snapshots>
105+
</pluginRepository>
106+
</pluginRepositories>
107+
108+
<dependencyManagement>
109+
<dependencies>
110+
<!-- importing the jakartaee-with-tools BOM adds specs and other useful artifacts as managed dependencies -->
111+
<dependency>
112+
<groupId>org.wildfly.bom</groupId>
113+
<artifactId>wildfly-ee-with-tools</artifactId>
114+
<version>${version.server.bom}</version>
115+
<type>pom</type>
116+
<scope>import</scope>
117+
</dependency>
118+
</dependencies>
119+
</dependencyManagement>
120+
121+
<dependencies>
122+
<!-- Import the Servlet API, we use provided scope as the API is included
123+
in JBoss EAP -->
124+
<dependency>
125+
<groupId>jakarta.servlet</groupId>
126+
<artifactId>jakarta.servlet-api</artifactId>
127+
<scope>provided</scope>
128+
</dependency>
129+
</dependencies>
130+
131+
<profiles>
132+
<profile>
133+
<id>provisioned-server</id>
134+
<dependencies>
135+
<dependency>
136+
<groupId>org.wildfly.arquillian</groupId>
137+
<artifactId>wildfly-arquillian-container-managed</artifactId>
138+
<scope>test</scope>
139+
</dependency>
140+
</dependencies>
141+
<build>
142+
<plugins>
143+
<plugin>
144+
<groupId>org.wildfly.plugins</groupId>
145+
<artifactId>wildfly-maven-plugin</artifactId>
146+
<version>${version.wildfly.maven.plugin}</version>
147+
<configuration>
148+
<feature-packs>
149+
<feature-pack>
150+
<location>org.wildfly:wildfly-galleon-pack:${version.server}</location>
151+
</feature-pack>
152+
</feature-packs>
153+
<layers>
154+
<layer>cloud-server</layer>
155+
<layer>elytron-oidc-client</layer>
156+
</layers>
157+
</configuration>
158+
<executions>
159+
<execution>
160+
<goals>
161+
<goal>package</goal>
162+
</goals>
163+
</execution>
164+
</executions>
165+
</plugin>
166+
</plugins>
167+
</build>
168+
</profile>
169+
</profiles>
170+
171+
</project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* JBoss, Home of Professional Open Source.
3+
* Copyright 2017 Red Hat, Inc., and individual contributors
4+
* as indicated by the @author tags.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.wildfly.quickstarts.oidc.simplewebapp;
19+
20+
import java.io.IOException;
21+
import java.io.PrintWriter;
22+
import java.security.Principal;
23+
24+
import jakarta.servlet.ServletException;
25+
import jakarta.servlet.annotation.WebServlet;
26+
import jakarta.servlet.http.HttpServlet;
27+
import jakarta.servlet.http.HttpServletRequest;
28+
import jakarta.servlet.http.HttpServletResponse;
29+
30+
/**
31+
* A simple secured HTTP servlet.
32+
*
33+
* @author <a href="mailto:[email protected]">Darran Lofthouse</a>
34+
*/
35+
@WebServlet("/secured")
36+
public class SecuredServlet extends HttpServlet {
37+
38+
@Override
39+
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
40+
try (PrintWriter writer = resp.getWriter()) {
41+
writer.println("<html>");
42+
writer.println(" <head><title>Secured Servlet</title></head>");
43+
writer.println(" <body>");
44+
writer.println(" <h1>Secured Servlet</h1>");
45+
writer.println(" <p>");
46+
writer.print(" Current Principal '");
47+
Principal user = req.getUserPrincipal();
48+
writer.print(user != null ? user.getName() : "NO AUTHENTICATED USER");
49+
writer.print("'");
50+
writer.println(" </p>");
51+
writer.println(" </body>");
52+
writer.println("</html>");
53+
}
54+
}
55+
56+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"client-id" : "myclient",
3+
"provider-url" : "${env.OIDC_PROVIDER_URL:http://localhost:8080}/realms/myrealm",
4+
"public-client" : "true",
5+
"principal-attribute" : "preferred_username",
6+
"ssl-required" : "EXTERNAL"
7+
}
8+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
6+
metadata-complete="false">
7+
8+
<security-constraint>
9+
<web-resource-collection>
10+
<web-resource-name>secured</web-resource-name>
11+
<url-pattern>/secured</url-pattern>
12+
</web-resource-collection>
13+
<auth-constraint>
14+
<role-name>*</role-name>
15+
</auth-constraint>
16+
</security-constraint>
17+
18+
<login-config>
19+
<auth-method>OIDC</auth-method>
20+
</login-config>
21+
22+
<security-role>
23+
<role-name>*</role-name>
24+
</security-role>
25+
</web-app>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<html>
2+
<body>
3+
<h2>Hello World!</h2>
4+
<a href="./secured">Access Secured Servlet</a>
5+
</body>
6+
</html>

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@
339339
<module>microprofile-reactive-messaging-kafka</module>
340340
<module>microprofile-rest-client</module>
341341
<module>numberguess</module>
342+
<module>oidc-webapp</module>
342343
<module>remote-helloworld-mdb</module>
343344
<module>security-domain-to-domain</module>
344345
<module>servlet-async</module>

shared-doc/build-and-deploy-the-quickstart.adoc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
// * To override the Maven command, define the `mavenCommand` variable,
2121
// for example:
2222
// :mavenCommand: clean install wildfly:deploy
23+
//
24+
// * To override the Maven deploy command arguments, define the `mavenDeployArgs` variable,
25+
// for example:
26+
// :mavenDeployArgs: wildfly:deploy -Dwildfly.port=10000
2327
//******************************************************************************
2428
2529
// The archive name defaults to the artifactId if not overridden
@@ -84,11 +88,15 @@ endif::reactive-messaging[]
8488
$ mvn {mavenCommand}
8589
----
8690
91+
ifndef::mavenDeployArgs[]
92+
:mavenDeployArgs: wildfly:deploy
93+
endif::mavenDeployArgs[]
94+
8795
. Type the following command to deploy the quickstart.
8896
+
8997
[source,subs="attributes+",options="nowrap"]
9098
----
91-
$ mvn wildfly:deploy
99+
$ mvn {mavenDeployArgs}
92100
----
93101
94102
ifdef::rest-client-qs[]

0 commit comments

Comments
 (0)