Giter Club home page Giter Club logo

keycloak-spring-boot-rest-angular-demo's Introduction

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://github.com/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

keycloak-spring-boot-rest-angular-demo's People

Contributors

iuliazidaru avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.