All Projects → hantsy → keycloak-springsecurity5-sample

hantsy / keycloak-springsecurity5-sample

Licence: GPL-3.0 license
Spring Security 5 OAuth2 Client/OIDC integration with Keycloak sample

Programming Languages

java
68154 projects - #9 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to keycloak-springsecurity5-sample

Keycloak
Open Source Identity and Access Management For Modern Applications and Services
Stars: ✭ 10,826 (+19583.64%)
Mutual labels:  keycloak, oidc
okta-jhipster-microservices-oauth-example
A microservices architecture built with JHipster, OAuth 2.0, and Okta
Stars: ✭ 29 (-47.27%)
Mutual labels:  spring-security, oidc
spring-boot-web-application-sample
Real World Spring Boot Web Application Example with tons of ready to use features
Stars: ✭ 143 (+160%)
Mutual labels:  keycloak, spring-security
oidckube
Wrapper for minikube that provisions and integrates it with Keycloak
Stars: ✭ 40 (-27.27%)
Mutual labels:  keycloak, oidc
brauzie
Awesome CLI for fetching JWT tokens for OAuth2.0 clients
Stars: ✭ 14 (-74.55%)
Mutual labels:  keycloak, oidc
secure-oauth2-oidc-workshop
Hands-On Workshop for OAuth 2.0 and OpenID Connect 1.0
Stars: ✭ 58 (+5.45%)
Mutual labels:  keycloak, spring-security
okta-spring-security-5-example
Authentication with Spring Security 5 and Okta OIDC
Stars: ✭ 16 (-70.91%)
Mutual labels:  spring-security, oidc
keycloak-sso
custom account management template, keycloak authentication and authorization
Stars: ✭ 41 (-25.45%)
Mutual labels:  keycloak, spring-security
spring-boot-keycloak-angular
Securing a Angular frontend and a Spring Boot backend with Keycloak and Spring Security
Stars: ✭ 40 (-27.27%)
Mutual labels:  keycloak, spring-security
angular-11-spring-boot-jwt-authentication
Angular 11 Spring Boot JWT Authentication example with Authorization | User Registration & Login
Stars: ✭ 62 (+12.73%)
Mutual labels:  spring-security
springboot-rest-api-angularjs-https
REST API https with Spring Boot and Angular JS. Use MySQL, Hibernate and Spring Security.
Stars: ✭ 38 (-30.91%)
Mutual labels:  spring-security
wired-vpn
WireGuard behind OIDC
Stars: ✭ 21 (-61.82%)
Mutual labels:  oidc
learn
一个学习使用的综合项目。实现方案为spring cloud alibaba
Stars: ✭ 38 (-30.91%)
Mutual labels:  spring-security
sotsera.blazor.oidc
OpenID Connect client for Blazor client-side projects
Stars: ✭ 21 (-61.82%)
Mutual labels:  oidc
spring-tiles-sample-app
Spring MVC - Apache Tile - AdminLTE Bootstrap template - Sample Application
Stars: ✭ 33 (-40%)
Mutual labels:  spring-security
auth-backends
Custom authentication backends and views for edX services
Stars: ✭ 20 (-63.64%)
Mutual labels:  oidc
mall
SpringBoot + Layui 电子商城系统
Stars: ✭ 38 (-30.91%)
Mutual labels:  spring-security
authentik
The authentication glue you need.
Stars: ✭ 2,941 (+5247.27%)
Mutual labels:  oidc
spring-boot-jwt-auth
🔑 Sample Spring boot application secured using JWT auth in custom header(X-Auth-Token).
Stars: ✭ 57 (+3.64%)
Mutual labels:  spring-security
Valley-eCommerce-prototype
An eCommerce website prototype with a layered architecture and MVC using Spring Boot v1.2, Spring Security, Hibernate, and Apache Lucene for full-text searching. for front-end: Bootstrap, Typeahead.js and Graph.js using Thymeleaf as RE.
Stars: ✭ 28 (-49.09%)
Mutual labels:  spring-security

keycloak-springsecurity5-sample

Spring Security 5 brought new OAuth2/OIDC client instead of the legacy client support in the old Spring Security OAuth sub project. The new OAuth2 umbrella modules in the core project will replace the old Spring Security OAuth, Spring Social etc. In the further 5.1, OAuth2 authorization server and resource server are planned to implement, check the OAuth2 related issues on Github .

Spring Security 5 OAuth2 client has built-in supports for facebook, github, okta, Google etc, unlike Spring Social, in this new client, Spring Security 5 provides a generic solution for client registration, thus you can configure any OAuth2/OIDC providers without codes.

A new oauth2login sample is added in Spring Security source codes to demonstrate the newest OAuth2 client.

In this post, we will fork this sample, and try to start up a local keycloak server and configure it as a custom OAuth2/OIDC provider in our project.

Setup local keycloak server

To simplify the work, I prepared a docker-compose.yml file to start keycloak server in a single command.

version: '3.3' 

services:    
     
  keycloak:
    image: jboss/keycloak
    ports:
      - "8000:8080"
    environment:
      - KEYCLOAK_LOGLEVEL=DEBUG
      - PROXY_ADDRESS_FORWARDING=true
      - KEYCLOAK_USER=keycloak 
      - KEYCLOAK_PASSWORD=keycloak
    depends_on:
      - mysql
      
  mysql:
    image: mysql
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=keycloak
      - MYSQL_USER=keycloak
      - MYSQL_PASSWORD=password
    volumes:
      - ./data/mysql:/var/lib/mysql

Start up keycloak by docker-compose command.

docker-compose up

Register client app in keycloak

When keycloak is started, open your browser and navigate to http://localhost:8000 or http://<docker-machine ip>:8000 if you are using a docker machine.

  1. Create a new schema: demo.
  2. Switch to the new demo schema in the dropdown menu.
  3. Create a client app: demoapp.
  4. Create a new user for test purpose.

Configure keycloak in our application

Generate a new project via Spring Initializr or fork the official oauth2login sample as start point.

Add a new keycloak node under the spring/security/oauth2/client node in the application.yml file.

spring:
  security:
    oauth2:
      client:
        registration:
          keycloak:
            client-id: demoapp
            client-secret: demoapp
            clientName: Keycloak
            authorization-grant-type: authorization_code
            redirectUriTemplate: '{baseUrl}/login/oauth2/code/{registrationId}'
            scope:
              - openid
              - profile
              - email
        provider:
          keycloak:
            authorization-uri: http://localhost:8000/auth/realms/demo/protocol/openid-connect/auth
            token-uri: http://localhost:8000/auth/realms/demo/protocol/openid-connect/token
            user-info-uri: http://localhost:8000/auth/realms/demo/protocol/openid-connect/userinfo
            jwk-set-uri: http://localhost:8000/auth/realms/demo/protocol/openid-connect/certs
            user-name-attribute: preferred_username

For custom OAuth2 provider, you have to configure the details of the OAuth2 provider, and provides the details of client registration for OAuth client support.

Bootstrap the application by mvn spring-boot:run or run it in IDE directly, then navigate to http://localhost:8080 in your browser.

You will find a new Keycloak link added in our application login page.

  1. Click the Keycloak link, it will guide you to redirect to keycloak login page.

    keycloak

  2. Use the user/password we have created in the last step to login.

  3. if it is successful, it will return back to our application home page.

    logged

  4. Click the Display User Info link, it will show all user attributes from /userinfo endpiont exposed by keycloak.

    userinfo

Check out the source codes from my github account.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].