Awesome Open Source
Awesome Open Source

keycloak-spring-boot-rest-angular-demo

Demo for configuring Keycloak authentication for a spring-boot rest service and AngularJs web client

Prerequisite

  • Keycloack (1.1.0) server installed
  • Java (1.8)
  • Maven (3.2.2)
  • Source code

Setup

  1. Clone https://awesomeopensource.com/project/iuliazidaru/keycloak-spring-boot-rest-angular-demo

  2. Download keycloak-appliance-dist-all-1.1.0.final.zip (or later version).

Start keycloack and import the realm provided with the source code.

  1. Start the rest server:

mvn spring-boot:run 4. Start the angular application

mvn spring-boot:run 5. Go to localhost:7005 and login using user/pass.

  1. Press reload to create a hello-world request to server.

Configuration

As all three applications run on different domains, we have to configure CORS.

Enable CORS in rest project

In keycloak.json a new line has to be added:

"enable-cors": true Also, CORSFilter has to be added in order to provide required headers for XMLHttpRequests.

https://github.com/iuliazidaru/keycloak-spring-boot-rest-angular-demo/blob/master/rest/src/main/java/hello/SimpleCORSFilter.java

AngularJS project configuration

The application must be public. Also, configure the web origin and the matching redirect URL.

Spring Security Context

In some cases we may need to access the user in context. Keycloak configuration is configured at container level, so Spring Security filters cannot be used.

Add security context dependency in order to have SpringContextHolder class. Do not use spring-boot-security as then the web application will be secure by default with ‘basic’ authentication on all HTTP endpoints.

We can add a simple HttpFilter which sets the securityContext:

https://github.com/iuliazidaru/keycloak-spring-boot-rest-angular-demo/blob/master/rest/src/main/java/hello/AuthenticationFilter.java

Tests

For writing integration tests we need Kecloak's Direct Access API.

https://github.com/iuliazidaru/keycloak-spring-boot-rest-angular-demo/blob/master/rest/src/test/java/hello/HelloWorldConfigurationTests.java

Resources

Alternatives To Keycloak Spring Boot Rest Angular Demo
Select To Compare


Alternative Project Comparisons
Related Awesome Lists
Top Programming Languages
Top Projects

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Javascript (1,090,062
Security (31,926
Rest (29,241
Spring (28,670
Authentication (26,919
Spring Boot (16,046
Angularjs (11,901
Cors (2,974
Keycloak (1,304
Keycloak Configuration (3