This Java backend application uses the java-security module to validate JWT tokens issued by the Identity
service.
It inspects incoming requests and handles authentication and authorization by using the IasTokenAuthenticator
.
Disclaimer: as of now the Identity tokens can only be validated in case the token from the consuming application is issued for the same Identity tenant.
mvn clean package
Deployment on Cloud Foundry
Use the cf CLI to create an IAS service instance.
cf create-service identity application ias-java-security
The vars contain hosts and paths that need to be adapted.
Deploy the application using the cf CLI.
cf push --vars-file ../vars.yml
Deployment on Kubernetes
Execute the following docker commands to build and push the docker image to a repository.
Replace <repository>/<image>
with your repository and image name.
docker build -t <repository>/<image> .
docker push <repository>/<image>
In deployment.yml replace the placeholder <YOUR IMAGE TAG>
with the image tag created in the previous step.
Deploy the application using kubectl.
kubectl apply -f k8s/deployment.yml
The sample application provides three HTTP endpoints:
/health
- accessible without authentication/hello-java-security-ias
- authenticated access only
Before sending requests to the latter endpoint we need to obtain a valid access token for a user.
To this we need to retrieve the ias-java-security
service instance credentials from Cloud Foundry or Kubernetes.
Retrieve IAS credentials from Cloud Foundry
Either use the cockpit to navigate to your application (via subaccount and space) and click on 'Environment Variables' or use the cf CLI command
cf env java-security-usage-ias
to retrieve the application environment.
The environment variable VCAP_SERVICES
contains a credentials
section for the xsuaa-java-security
service instance.
Retrieve IAS credentials from Kubernetes
Use the following Kubernetes CLI command to retrieve the ias-java-security
service instance credentials by reading the ias-service-binding
secret.
kubectl get secret "ias-service-binding" -o go-template='{{range $k,$v := .data}}{{"### "}}{{$k}}{{"\n"}}{{$v|base64decode}}{{"\n\n"}}{{end}}'
Use the IAS credentials to retrieve an access token for the sample application by following the HowToFetchToken guide.
Now you can use the access token to access the application via curl.
curl command to access Cloud Foundry deployment
curl -X GET \
https://java-security-usage-ias-<<ID>>.<<LANDSCAPE_APPS_DOMAIN>>/hello-java-security-ias \
-H 'Authorization: Bearer <<id token>>'
curl command to access Kubernetes deployment
curl -X GET \
https://java-security-ias-api.<<K8S DOMAIN>>/java-security-usage-ias/hello-java-security-ias \
-H 'Authorization: Bearer <<access token>>'
You should see something like this:
You ('<your user>') can access the application with the following scopes: '<your scopes>'.
If you no longer need the sample application, you can free up resources using the cf CLI or the Kubernetes CLI.
Cleanup commands for Cloud Foundry
cf unbind-service java-security-usage-ias ias-java-security
cf delete -f java-security-usage-ias
cf delete-service -f ias-java-security
Cleanup command for Kubernetes
kubectl delete -f k8s/deployment.yml