How to Secure a React App with Keycloak

In this video, we will secure a React application with Keycloak security. But before we begin don’t forget to subscribe to my channel and hi...


In this video, we will secure a React application with Keycloak security.

But before we begin don’t forget to subscribe to my channel and hit the bell icon to get notified on new video uploads.

Check out the React project in the reference section, or you can create your own React project from scratch using create-react-app. For this demo, we will use the project from Github.

Before running the React application we first need to configure the Keycloak server for security.

Configure the Keycloak Server

  1. Run Keycloak on terminal docker run -p 8080:8080 -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin jboss/keycloak. Note that I’m running my instance on another machine thus I’ll be using a network IP.
  2. Import the realm configuration as well as the users from the Spring Keycloak with REST API project, config folder. See reference below for the link.
  3. Make sure that the primereact-client is set to accessType=confidential.
  4. Check that the keycloak.json in the react project matches the primereact-client, specially the credential secret.
  5. Import the users in the same folder. It should import 3 users: kerri, admin, and norole.

The React Project

This project is already complex, so I want you to focus on the files inside the framework and api-services folder. I’m using a free React template/layout from Primefaces.

As with most of the React project index.js is the entry point of the application. Here are the most notable files that handle runs the app and handle the security.
  1. framework
    1. redux
      1. ApplicationStore - Where the store is configured. Here we define both middlewares and enhancers.
      2. Dispatch - common global action definition
      3. Reducers - the main reducer file
      4. modules
        1. Authorization - where keycloak initialization and actions are defined like refresh token, logout, etc.
        2. Customers - customer action creators and reducers
  2. pages
    1. layout
      1. SecureLayout - HOC layout template for secure pages
    2. routing
      1. SecureRoutes - HOC that secures a page
      2. SecurityContext - this is our React security context
    3. secure
      1. Customers - secured pages

Running the React Project

  1. Inside the React project run npm start.
  2. Navigate to http://localhost:3000, it should open the home component. This is a simple component with a router link to a secured page.
  3. To secure the page we introduce 2 HOC:
    1. withSecurity - handles the security, redirects to login page if not authenticated
    2. withSecureLayout - handles the layout of the secured pages

References

COMMENTS

mas template
Name

amazon,1,angular,8,bigdata,2,business,1,course-spring,27,courses,6,database,4,docker,3,java,50,kafka,1,keycloak,4,microservices,5,mysql,1,neworking,1,nosql,2,php,1,pinned,2,react,3,server management,7,shared drive,1,spring,7,synology,1,troubleshooting,2,web,1,wordpress,1,
ltr
item
toztech: How to Secure a React App with Keycloak
How to Secure a React App with Keycloak
https://i.ytimg.com/vi/5Ki0z3d0f7Y/0.jpg
https://i.ytimg.com/vi/5Ki0z3d0f7Y/0.jpg
toztech
https://toztech.blogspot.com/2019/08/how-to-secure-react-app-with-keycloak.html
https://toztech.blogspot.com/
https://toztech.blogspot.com/
https://toztech.blogspot.com/2019/08/how-to-secure-react-app-with-keycloak.html
true
2554149350007112447
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content