+ * You may not modify, use, reproduce, or distribute this software except in
+ * compliance with the terms of the License at:
+ * https://github.com/javaee/tutorial-examples/LICENSE.txt
+ */
+
+package javaeetutorial.custom_identity_store;
+
+import javax.security.enterprise.authentication.mechanism.http.BasicAuthenticationMechanismDefinition;
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Named;
+import javax.security.enterprise.identitystore.DatabaseIdentityStoreDefinition;
+
+@BasicAuthenticationMechanismDefinition(
+ realmName = "file"
+)
+
+@ApplicationScoped
+@Named
+public class ApplicationConfig {
+
+}
diff --git a/security/security-api/custom-identity-store/src/main/java/javaeetutorial/custom_identity_store/Servlet.java b/security/security-api/custom-identity-store/src/main/java/javaeetutorial/custom_identity_store/Servlet.java
new file mode 100644
index 0000000..2df6eea
--- /dev/null
+++ b/security/security-api/custom-identity-store/src/main/java/javaeetutorial/custom_identity_store/Servlet.java
@@ -0,0 +1,50 @@
+/**
+ * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
+ *
+ * You may not modify, use, reproduce, or distribute this software except in
+ * compliance with the terms of the License at:
+ * https://github.com/javaee/tutorial-examples/LICENSE.txt
+ */
+
+package javaeetutorial.custom_identity_store;
+
+import java.io.IOException;
+
+import javax.annotation.security.DeclareRoles;
+import javax.servlet.ServletException;
+import javax.servlet.annotation.HttpConstraint;
+import javax.servlet.annotation.ServletSecurity;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * Test Servlet that prints out the name of the authenticated caller and whether
+ * this caller is in any of the roles {foo, bar, kaz}
+ *
+ */
+
+@WebServlet("/servlet")
+@DeclareRoles({ "foo", "bar", "kaz" })
+@ServletSecurity(@HttpConstraint(rolesAllowed = "foo"))
+public class Servlet extends HttpServlet {
+
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+
+ String webName = null;
+ if (request.getUserPrincipal() != null) {
+ webName = request.getUserPrincipal().getName();
+ }
+
+ response.getWriter().write("web username: " + webName + "\n");
+
+ response.getWriter().write("web user has role \"foo\": " + request.isUserInRole("foo") + "\n");
+ response.getWriter().write("web user has role \"bar\": " + request.isUserInRole("bar") + "\n");
+ response.getWriter().write("web user has role \"kaz\": " + request.isUserInRole("kaz") + "\n");
+ }
+
+}
diff --git a/security/security-api/custom-identity-store/src/main/java/javaeetutorial/custom_identity_store/TestIdentityStore.java b/security/security-api/custom-identity-store/src/main/java/javaeetutorial/custom_identity_store/TestIdentityStore.java
new file mode 100644
index 0000000..9dfea1b
--- /dev/null
+++ b/security/security-api/custom-identity-store/src/main/java/javaeetutorial/custom_identity_store/TestIdentityStore.java
@@ -0,0 +1,41 @@
+/**
+ * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
+ *
+ * You may not modify, use, reproduce, or distribute this software except in
+ * compliance with the terms of the License at:
+ * https://github.com/javaee/tutorial-examples/LICENSE.txt
+ */
+
+package javaeetutorial.custom_identity_store;
+
+import static java.util.Arrays.asList;
+import static javax.security.enterprise.identitystore.CredentialValidationResult.INVALID_RESULT;
+
+import java.util.HashSet;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.security.enterprise.credential.UsernamePasswordCredential;
+import javax.security.enterprise.identitystore.CredentialValidationResult;
+import javax.security.enterprise.identitystore.IdentityStore;
+import javax.security.enterprise.credential.UsernamePasswordCredential;
+
+@ApplicationScoped
+public class TestIdentityStore implements IdentityStore {
+
+ public CredentialValidationResult validate(UsernamePasswordCredential usernamePasswordCredential) {
+
+ // This is for illustrative purposes only, and a real IdentityStore should include secure storage
+ // and credential validation capabilities.
+ // This example is a trivial one and is not meant to be used in production setup at all. Use of
+ // hard-coded/in-memory stores or user databases trivially provided as unencrypted files etc is not
+ // encouraged and has been used here in this manner just to demonstrate how a custom identity
+ // store can be defined.
+
+ if (usernamePasswordCredential.compareTo("Joe", "secret1")) {
+ return new CredentialValidationResult("Joe", new HashSet<>(asList("foo", "bar")));
+ }
+
+ return INVALID_RESULT;
+ }
+
+}
diff --git a/security/security-api/pom.xml b/security/security-api/pom.xml
new file mode 100644
index 0000000..1b14564
--- /dev/null
+++ b/security/security-api/pom.xml
@@ -0,0 +1,23 @@
+
+