Skip to content

Latest commit

 

History

History
156 lines (93 loc) · 7.8 KB

File metadata and controls

156 lines (93 loc) · 7.8 KB

Spring boot angular authorization code sample.

Addditional Prerequisite:

Add ssl certificate to the trusted store.

Before proceeding lets perform an additional prerequisite. Visit https://localhost:9999/

pingfedlanding

Click on the "Not secure" as shown below.

certificatecheck

Proceed as shown next.

(A quick side note: Its possible to add proper ssl certificates into pingfederate and is very straightforward. Not covering that here. )

certificatecheck1

Click on the menu item corresponding to Certificate is not valid as shown above.
This will launch the certificate viewer.
Showing below a partial screen of same.

certificatecheck2

Lets visit the details tab.

certificatecheck3

Press the "Export" button.
This will launch File save as dialog for the crt file.

certificatecheck4

Save the file at a suitable location. In command prompt or terminal visit same location. Execute the below command:

keytool -import -trustcacerts -file localhost.crt -alias localpingfed -keystore %JAVA_HOME%/lib/security/cacerts -storepass changeit

On linux: sudo keytool -import -trustcacerts -file localhost.crt -alias localpingfed -keystore $JAVA_HOME/lib/security/cacerts -storepass changeit

Also ensure name of the file "localhost.crt" matches with actual including the file name extension.

keytool

When prompted enter yes

keytool1

Demo:

springboot.oidc.with.angular is a maven project. Its also a submodule of pingfed-automation\oidc-check.
It should have been already built. However we need to build it properly with its angular parts. In command prompt or terminal navigate to the usual pingfed-automation folder.
Run the below command.

.\gradlew :oidc-check:springboot.oidc.with.angular:bootRun

build_springboot_angular

This should give a result as shown below.
build_and run_springboot_angular1
Showing above how it launches.

Its a simple spring boot based project. Using angular for front end.
We are going to use this project to verify if we are able to obtain some access tokens from pingfederate. Vist http://localhost:8081

springboot-angular3

Click Login

springboot-angular4

Note: Enter user.0 and password for the username and password.
Just the text - user.0 and password for username and password respectively.

springboot-angular5

Uncheck foo now. Press Allow.

uncheck foo

Login should be successful.

springboot-angular6

The second link is a Protected route and appears only when logged in.
But both links have same functionality for this demo.
Click the first one.

springboot-angular6a

Now check the "Show Innaccessible" checkbox.

springboot-angular6b

The Access Foo Buton is now available even though it should not be.
Click all the buttons.

springboot-angular6c

Notice how all the buttons work. But "Access Foo" button reported 403 and Problem.

If you logout and are at this page you will see even less buttons.

springboot-angular6d

Again pressing the checkbox would show you all the buttons including the inaccessibble ones.

springboot-angular6e

Pressing the inaccessible ones will report 401 as shown below.
springboot-angular6f

Conclusion: We have seen how the login and scopes selection affects how the users can do what on the application.

You can also launch with application with a VM argument of -DisOn4200=true.
If you do that during development you can keep the server running at 8081 but front end at 4200. You could launch the application then at http://localhost:4200 when doing angular development.

Additional Notes:

A spring boot angular OIDC application can be done in different ways.
The approach taken here is the same approach used by back end web applications. As can be seen here the SPA angular app is not a pure SPA. Its compromising on its SPA'ness during login. Have deliberately chosen an approach where the front end, back end application are all in the same web app.
The main advanatage of this approach is that the access token never leaves the back end.
It never is made accessible to the angular javascript front end.
This is often a most recommended highly secure and compact approach- I am attempting here the The BFF Pattern (Backend for Frontend).
However this is not the only way.
There are other approaches also possible. If needed one can also use Authorization Code with PKCE when working with Pure SPA or native applications where browser redirect is being avoided or not possible. Depending on the requirements choose the appropriate approach.

Improvements in this sample code

Logout needs to use a pingfed logout endpoint. Note: Logout itself works.

Cleanup if needed:

Remove ssl certificate from the trusted store.

Run below command to remove certificate from the trusted store. keytool -delete -alias localpingfed -keystore %JAVA_HOME%/lib/security/cacerts

This should remove the ssl certificate from the trust store.