-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from aguibert/basic-auth
Add @BasicAuthConfig for testing applications with Basic auth
- Loading branch information
Showing
12 changed files
with
358 additions
and
24 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
core/src/main/java/org/microshed/testing/jaxrs/BasicAuthConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (c) 2020 IBM Corporation and others | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.microshed.testing.jaxrs; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Used to annotate a REST Client to configure Basic Authorization | ||
* that will be applied to all of its HTTP invocations. | ||
* In order for this annotation to have any effect, the field must also | ||
* be annotated with {@link RESTClient}. | ||
*/ | ||
@Target({ ElementType.FIELD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface BasicAuthConfig { | ||
|
||
String user(); | ||
|
||
String password(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# OpenLiberty | ||
FROM openliberty/open-liberty:full-java8-openj9-ubi | ||
COPY src/main/liberty/config /config/ | ||
ADD build/libs/myservice.war /config/apps | ||
|
||
# Wildfly | ||
#FROM jboss/wildfly | ||
#ADD build/libs/myservice.war /opt/jboss/wildfly/standalone/deployments/ | ||
|
||
# Payara | ||
#FROM payara/micro:5.193 | ||
#CMD ["--deploymentDir", "/opt/payara/deployments", "--noCluster"] | ||
#ADD build/libs/myservice.war /opt/payara/deployments | ||
|
||
# TomEE | ||
#FROM tomee:8-jre-8.0.0-M2-microprofile | ||
#COPY build/libs/myservice.war /usr/local/tomee/webapps/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
plugins { | ||
id 'war' | ||
} | ||
|
||
dependencies { | ||
providedCompile 'javax:javaee-api:8.0.1' | ||
providedCompile 'org.eclipse.microprofile:microprofile:2.1' | ||
testCompile project(':microshed-testing-testcontainers') | ||
testCompile group: 'org.assertj', name: 'assertj-core', version: '3.15.0' | ||
testCompile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.29' | ||
testImplementation 'org.junit.jupiter:junit-jupiter:5.6.0' | ||
} | ||
|
||
war.archiveName 'myservice.war' | ||
test.dependsOn 'war' | ||
|
||
// Always re-run tests on every build for the sake of this sample | ||
// In a real project, this setting would not be desirable | ||
test.outputs.upToDateWhen { false } |
68 changes: 68 additions & 0 deletions
68
sample-apps/jaxrs-basicauth/src/main/java/org/example/app/SecuredService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (c) 2019 IBM Corporation and others | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.example.app; | ||
|
||
import javax.annotation.security.PermitAll; | ||
import javax.annotation.security.RolesAllowed; | ||
import javax.enterprise.context.RequestScoped; | ||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.HttpHeaders; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.SecurityContext; | ||
|
||
@Path("/data") | ||
@RequestScoped | ||
@RolesAllowed("admin") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
public class SecuredService { | ||
|
||
@Context | ||
SecurityContext securityContext; | ||
|
||
@Context | ||
HttpHeaders headers; | ||
|
||
@GET | ||
@Path("/ping") | ||
@PermitAll | ||
public String ping() { | ||
return "ping"; | ||
} | ||
|
||
@GET | ||
@Path("/headers") | ||
@PermitAll | ||
public String getHeaders() { | ||
String result = "*** HEADERS: " + headers.getRequestHeaders().toString(); | ||
result += "\n" + "*** PRINCIPAL NAME=" + ( securityContext == null ? "null" : securityContext.getUserPrincipal().getName()); | ||
return result; | ||
} | ||
|
||
@GET | ||
public String getSecuredInfo() { | ||
return "this is some secured info"; | ||
} | ||
|
||
|
||
} |
25 changes: 25 additions & 0 deletions
25
sample-apps/jaxrs-basicauth/src/main/java/org/example/app/SecuredServiceApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright (c) 2019 IBM Corporation and others | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.example.app; | ||
|
||
import javax.ws.rs.ApplicationPath; | ||
import javax.ws.rs.core.Application; | ||
|
||
@ApplicationPath("/app") | ||
public class SecuredServiceApp extends Application { } |
27 changes: 27 additions & 0 deletions
27
sample-apps/jaxrs-basicauth/src/main/liberty/config/server.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<server> | ||
|
||
<featureManager> | ||
<feature>jaxrs-2.1</feature> | ||
<feature>jsonb-1.0</feature> | ||
<feature>mpHealth-1.0</feature> | ||
<feature>mpConfig-1.3</feature> | ||
<feature>mpRestClient-1.1</feature> | ||
<feature>cdi-2.0</feature> | ||
<feature>appSecurity-3.0</feature> | ||
</featureManager> | ||
|
||
<basicRegistry id="basic"> | ||
<user name="alice" password="alicepwd"/> | ||
<user name="bob" password="bobpwd"/> | ||
</basicRegistry> | ||
|
||
<webApplication location="myservice.war"> | ||
<application-bnd> | ||
<!-- this can also be defined in web.xml instead --> | ||
<security-role name="admin"> | ||
<user name="bob"/> | ||
</security-role> | ||
</application-bnd> | ||
</webApplication> | ||
|
||
</server> |
Oops, something went wrong.