<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>Spring Boot - Inero Software - Software Consulting</title>
	<atom:link href="https://inero-software.com/tag/spring-boot/feed/" rel="self" type="application/rss+xml" />
	<link>https://inero-software.com/tag/spring-boot/</link>
	<description>We unleash innovations using cutting-edge technologies, modern design and AI</description>
	<lastBuildDate>Wed, 06 Nov 2024 07:29:20 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://inero-software.com/wp-content/uploads/2018/11/inero-logo-favicon.png</url>
	<title>Spring Boot - Inero Software - Software Consulting</title>
	<link>https://inero-software.com/tag/spring-boot/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">153509928</site>	<item>
		<title>Keycloak Integration Guide: Securing Java Spring Endpoints with Keycloak</title>
		<link>https://inero-software.com/securing-java-spring-endpoints-with-keycloak/</link>
		
		<dc:creator><![CDATA[Marceli Formela]]></dc:creator>
		<pubDate>Mon, 27 May 2024 13:19:53 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Company]]></category>
		<category><![CDATA[Keycloak]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[Java Spring]]></category>
		<category><![CDATA[keycloak]]></category>
		<category><![CDATA[RBAC]]></category>
		<category><![CDATA[Role-Based Access Control]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<guid isPermaLink="false">https://inero-software.com/?p=5759</guid>

					<description><![CDATA[<p>&#160; &#160;</p>
<p>Artykuł <a href="https://inero-software.com/securing-java-spring-endpoints-with-keycloak/">Keycloak Integration Guide: Securing Java Spring Endpoints with Keycloak</a> pochodzi z serwisu <a href="https://inero-software.com">Inero Software - Software Consulting</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>&nbsp;</p>
<div class="row"><div class="col-sm-1"></div><div class="col-sm-10">
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><span style="font-weight: 400;">In the world of web application development, securing endpoints is a critical task to ensure that only authorized users can access certain resources. One of the most robust ways to handle authentication and authorization in Java Spring applications is by integrating with Keycloak, an open-source identity and access management solution.</span></p>
<p><span style="font-weight: 400;">Spring Security is a powerful and customizable authentication and access-control framework for the Java Spring ecosystem. However, managing and implementing security policies and authentication mechanisms from scratch can be cumbersome and error-prone. By integrating Keycloak, developers can offload many of these responsibilities to a robust, external system. Keycloak handles user management, roles, and permissions, and provides an easy-to-use interface for administrators.</span></p>
<p><span style="font-weight: 400;">In this article, we will demonstrate how to seamlessly integrate Keycloak with Spring Boot and secure endpoints based on Role-Based Access Control (RBAC). We&#8217;ll guide you through the necessary steps to configure Keycloak, set up roles, and apply security constraints to your application. By the end of this tutorial, you will have authentication and authorization system implemented in your Spring Boot project.</span></p>
<p>&nbsp;</p>
<h3><b>Setting Up Keycloak</b></h3>
<p><span style="font-weight: 400;">Before integrating Keycloak with a Spring Boot application, you need to set up a Keycloak server. Here&#8217;s a quick guide to getting started:</span></p>
<ol>
<li><b></b><span style="font-weight: 400;">   </span> <b>Download and Install Keycloak</b></li>
</ol>
<p><span style="font-weight: 400;">Download the latest version of <a href="https://www.keycloak.org/">Keycloak from the official website</a>. Extract the downloaded archive and start the server by navigating to the bin directory and executing:</span></p>
<pre><i><span style="font-weight: 400;">bin/kc.sh start-dev</span></i>
<i><span style="font-weight: 400;">2024-05-23 06:59:48,625 INFO  [io.quarkus] (main) Keycloak 24.0.4 on JVM (powered by Quarkus 3.8.4) started in 5.096s. Listening on: http://0.0.0.0:8080</span></i></pre>
<p>&nbsp;</p>
<ol start="2">
<li><b></b><span style="font-weight: 400;">   </span> <b>Access the Admin Console</b></li>
</ol>
<p><span style="font-weight: 400;">Open a browser and navigate to</span><a href="http://localhost:8080"> <span style="font-weight: 400;">http://localhost:8080</span></a><span style="font-weight: 400;">. After creating admin account and logging in you should be redirected to /admin/master/console/.</span></p>
<p><img fetchpriority="high" decoding="async" data-attachment-id="5769" data-permalink="https://inero-software.com/securing-java-spring-endpoints-with-keycloak/keyclock1-2/" data-orig-file="https://inero-software.com/wp-content/uploads/2024/05/KEYCLOCK1-2.png" data-orig-size="1920,1080" data-comments-opened="0" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="KEYCLOCK1 (2)" data-image-description="" data-image-caption="" data-medium-file="https://inero-software.com/wp-content/uploads/2024/05/KEYCLOCK1-2-300x169.png" data-large-file="https://inero-software.com/wp-content/uploads/2024/05/KEYCLOCK1-2-1030x579.png" tabindex="0" role="button" class="wp-image-5769 aligncenter" src="https://inero-software.com/wp-content/uploads/2024/05/KEYCLOCK1-2-300x169.png" alt="" width="500" height="282" srcset="https://inero-software.com/wp-content/uploads/2024/05/KEYCLOCK1-2-300x169.png 300w, https://inero-software.com/wp-content/uploads/2024/05/KEYCLOCK1-2-1030x579.png 1030w, https://inero-software.com/wp-content/uploads/2024/05/KEYCLOCK1-2-768x432.png 768w, https://inero-software.com/wp-content/uploads/2024/05/KEYCLOCK1-2-1536x864.png 1536w, https://inero-software.com/wp-content/uploads/2024/05/KEYCLOCK1-2-533x300.png 533w, https://inero-software.com/wp-content/uploads/2024/05/KEYCLOCK1-2.png 1920w" sizes="(max-width: 500px) 100vw, 500px" /></p>
<ol start="3">
<li><b></b><span style="font-weight: 400;">   </span> <b>Create a Realm</b></li>
</ol>
<p><span style="font-weight: 400;">A successful login will take us to the console and open up the default Master realm for us. In this scenario we will create a custom realm. Create a new realm by clicking on the &#8220;Add Realm&#8221; button. After clicking the </span><i><span style="font-weight: 400;">Create</span></i><span style="font-weight: 400;"> button, a new realm will be created and we’ll be redirected to it. All the operations in the next sections will be performed in this new </span><i><span style="font-weight: 400;">custom</span></i><span style="font-weight: 400;"> realm.</span></p>
<p><span style="font-weight: 400;">A realm represents a logical grouping of users, credentials, roles, and configurations. It basicly acts as an isolated environment within the Keycloak server where you can manage users and define security policies independently from other realms. This separation allows for enabling different applications or organizations to have their own distinct security domains within a single Keycloak instance. Realms provide the flexibility to customize authentication and authorization settings to meet the specific needs of different projects or clients.</span></p>
<p><img decoding="async" data-attachment-id="5774" data-permalink="https://inero-software.com/securing-java-spring-endpoints-with-keycloak/keycloak-blog-1/" data-orig-file="https://inero-software.com/wp-content/uploads/2024/05/Keycloak-blog-1.png" data-orig-size="3600,2025" data-comments-opened="0" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Keycloak blog 1" data-image-description="" data-image-caption="" data-medium-file="https://inero-software.com/wp-content/uploads/2024/05/Keycloak-blog-1-300x169.png" data-large-file="https://inero-software.com/wp-content/uploads/2024/05/Keycloak-blog-1-1030x579.png" tabindex="0" role="button" class="wp-image-5774 aligncenter" src="https://inero-software.com/wp-content/uploads/2024/05/Keycloak-blog-1-300x169.png" alt="" width="730" height="411" srcset="https://inero-software.com/wp-content/uploads/2024/05/Keycloak-blog-1-300x169.png 300w, https://inero-software.com/wp-content/uploads/2024/05/Keycloak-blog-1-1030x579.png 1030w, https://inero-software.com/wp-content/uploads/2024/05/Keycloak-blog-1-768x432.png 768w, https://inero-software.com/wp-content/uploads/2024/05/Keycloak-blog-1-1536x864.png 1536w, https://inero-software.com/wp-content/uploads/2024/05/Keycloak-blog-1-2048x1152.png 2048w, https://inero-software.com/wp-content/uploads/2024/05/Keycloak-blog-1-533x300.png 533w" sizes="(max-width: 730px) 100vw, 730px" /></p>
<ol start="4">
<li><b></b><span style="font-weight: 400;">   </span> <b>Create a Client</b></li>
</ol>
<p><span style="font-weight: 400;">Now we’ll navigate to the Clients page. As we can see in the image below, Keycloak comes with Clients that are already built-in. We still need to add a new client to our application, so we’ll click </span><i><span style="font-weight: 400;">Create</span></i><span style="font-weight: 400;">.</span></p>
<p><span style="font-weight: 400;">A client in Keycloak represents an application or service that requests authentication and authorization from the Keycloak server. Clients are configured within a realm and can have specific roles, credentials, and access policies. They can be web applications, mobile apps, or other types of services that require secure access control. By defining clients, Keycloak can manage how different applications interact with the authentication server, ensuring that each client follows the appropriate security protocols and has the necessary permissions to access protected resources.</span></p>
<p><img decoding="async" data-attachment-id="5775" data-permalink="https://inero-software.com/securing-java-spring-endpoints-with-keycloak/keycloak-blog-1-1/" data-orig-file="https://inero-software.com/wp-content/uploads/2024/05/Keycloak-blog-1-1.png" data-orig-size="3600,2025" data-comments-opened="0" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Keycloak blog 1 (1)" data-image-description="" data-image-caption="" data-medium-file="https://inero-software.com/wp-content/uploads/2024/05/Keycloak-blog-1-1-300x169.png" data-large-file="https://inero-software.com/wp-content/uploads/2024/05/Keycloak-blog-1-1-1030x579.png" tabindex="0" role="button" class="wp-image-5775 aligncenter" src="https://inero-software.com/wp-content/uploads/2024/05/Keycloak-blog-1-1-300x169.png" alt="" width="731" height="412" srcset="https://inero-software.com/wp-content/uploads/2024/05/Keycloak-blog-1-1-300x169.png 300w, https://inero-software.com/wp-content/uploads/2024/05/Keycloak-blog-1-1-1030x579.png 1030w, https://inero-software.com/wp-content/uploads/2024/05/Keycloak-blog-1-1-768x432.png 768w, https://inero-software.com/wp-content/uploads/2024/05/Keycloak-blog-1-1-1536x864.png 1536w, https://inero-software.com/wp-content/uploads/2024/05/Keycloak-blog-1-1-2048x1152.png 2048w, https://inero-software.com/wp-content/uploads/2024/05/Keycloak-blog-1-1-533x300.png 533w" sizes="(max-width: 731px) 100vw, 731px" /></p>
<ol start="5">
<li><b></b><span style="font-weight: 400;">   </span> <b>Creating Roles and Users</b></li>
</ol>
<p><span style="font-weight: 400;">Define roles and users for your application. Navigate to the Roles and Users sections to add and configure them accordingly.</span></p>
<p>&nbsp;</p>
<h3><b>Integrating Keycloak with Spring Boot</b></h3>
<p>&nbsp;</p>
<ol>
<li><b></b><strong>       Add dependencies</strong></li>
</ol>
<p><span style="font-weight: 400;">We use the Spring Security OAuth2.0 Client to connect to the Keycloak server.</span></p>
<p><span style="font-weight: 400;">Let’s start by declaring spring-boot-starter-oauth2-client dependency in a Spring Boot application in the pom.xml:</span></p>
<pre><span style="font-weight: 400;">&lt;dependency&gt;</span>
<span style="font-weight: 400;">    &lt;groupId&gt;</span><span style="font-weight: 400;">org.springframework.boot</span><span style="font-weight: 400;">&lt;/groupId&gt;</span>
<span style="font-weight: 400;">        &lt;artifactId&gt;</span><span style="font-weight: 400;">spring-boot-starter-oauth2-client</span><span style="font-weight: 400;">&lt;/artifactId&gt;</span>
<span style="font-weight: 400;">&lt;/dependency&gt;</span></pre>
<p><span style="font-weight: 400;">Also, as we need to use Spring Security with Spring Boot, we must add this dependency:</span></p>
<pre><span style="font-weight: 400;">&lt;dependency&gt;</span>
<span style="font-weight: 400;">    &lt;groupId&gt;</span><span style="font-weight: 400;">org.springframework.boot</span><span style="font-weight: 400;">&lt;/groupId&gt;</span>
<span style="font-weight: 400;">    &lt;artifactId&gt;</span><span style="font-weight: 400;">spring-boot-starter-security</span><span style="font-weight: 400;">&lt;/artifactId&gt;</span>
<span style="font-weight: 400;">&lt;/dependency&gt;</span></pre>
<p><span style="font-weight: 400;">In order to delegate the identification control to a Keycloak server, we’ll use the spring-boot-starter-oauth2-resource-server library. This library facilitates the validation of a JWT token with the Keycloak server. Therefore, let&#8217;s include it in our project&#8217;s pom.xml file.</span></p>
<pre><span style="font-weight: 400;">&lt;dependency&gt;</span>
<span style="font-weight: 400;">    &lt;groupId&gt;</span><span style="font-weight: 400;">org.springframework.boot</span><span style="font-weight: 400;">&lt;/groupId&gt;</span>
<span style="font-weight: 400;">    &lt;artifactId&gt;</span><span style="font-weight: 400;">spring-boot-starter-oauth2-resource-server</span><span style="font-weight: 400;">&lt;/artifactId&gt;</span>
<span style="font-weight: 400;">&lt;/dependency&gt;</span></pre>
<p>&nbsp;</p>
<ol start="2">
<li><strong>      Configure Keycloak in application.properties</strong></li>
</ol>
<p><span style="font-weight: 400;">This grant type is suitable for server-to-server communication, where the client application itself is considered the resource owner. It involves exchanging the client’s credentials (client ID and client secret) for an access token.</span></p>
<pre style="padding-left: 40px;"><b>spring.security.oauth2.client.registration.keycloak.authorization-grant-type=client_credentials</b></pre>
<p>&nbsp;</p>
<p><span style="font-weight: 400;">These properties define the client ID and secret that the application will use to authenticate with Keycloak. In this example, the secret is set to placeholder value.</span></p>
<pre style="padding-left: 40px;"><b>spring.security.oauth2.client.registration.keycloak.client-id=back-end-resource-server</b>

<b>spring.security.oauth2.client.registration.keycloak.client-secret=test-secret</b></pre>
<p>&nbsp;</p>
<p><span style="font-weight: 400;">Finally, let’s add the configuration needed for validating JWT token against our Keycloak server:</span></p>
<pre style="padding-left: 40px;"><b>spring.security.oauth2.client.provider.keycloak.token-uri=${test.keycloak-base-url}/realms/test/protocol/openid-connect/token</b>

<b>spring.security.oauth2.resourceserver.jwt.issuer-uri=${app.keycloak-base-url}/realms/test-realm</b></pre>
<p><span style="font-weight: 400;">By combining these configurations, your Spring Boot application will be able to authenticate itself to Keycloak using the client credentials flow and validate JWTs issued by Keycloak, ensuring secure access to its endpoints.</span></p>
<p><span style="font-weight: 400;">Now, the Spring Boot application can interact with Keycloak.</span></p>
<p><a href="https://inero-software.com/keycloak-services/"><img loading="lazy" decoding="async" data-attachment-id="5752" data-permalink="https://inero-software.com/best-keycloak-practices/_baner-en-300x60-1/" data-orig-file="https://inero-software.com/wp-content/uploads/2024/05/baner-en-300x60-1-1.png" data-orig-size="825,165" data-comments-opened="0" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="_baner en 300&amp;#215;60 (1)" data-image-description="" data-image-caption="" data-medium-file="https://inero-software.com/wp-content/uploads/2024/05/baner-en-300x60-1-1-300x60.png" data-large-file="https://inero-software.com/wp-content/uploads/2024/05/baner-en-300x60-1-1.png" tabindex="0" role="button" class="wp-image-5752 aligncenter" src="https://inero-software.com/wp-content/uploads/2024/05/baner-en-300x60-1-1-300x60.png" alt="" width="755" height="151" srcset="https://inero-software.com/wp-content/uploads/2024/05/baner-en-300x60-1-1-300x60.png 300w, https://inero-software.com/wp-content/uploads/2024/05/baner-en-300x60-1-1-768x154.png 768w, https://inero-software.com/wp-content/uploads/2024/05/baner-en-300x60-1-1.png 825w" sizes="(max-width: 755px) 100vw, 755px" /></a></p>
<ol start="3">
<li><b></b><strong>       Spring Security configuration</strong></li>
</ol>
<p><span style="font-weight: 400;">Now, let&#8217;s examine the resource server configuration. We need to extract Keycloak-specific information from the JWT. All requests with an Authorization header must include a user&#8217;s token with the necessary scope.</span></p>
<p><span style="font-weight: 400;">In this example CORS is enabled and session management is set to stateless, which is typial for APIs to ensure each request is independently authenticated. We’ve also disabled CSRF protection since our API is stateless.</span></p>
<p><span style="font-weight: 400;">The application is set up as an OAuth 2 resource server that uses JWT for authentication and jwtAuthenticationConverter is specified to handle the conversion of JWT claims into authorities.</span></p>
<pre><span style="font-weight: 400;">@Bean</span>
<span style="font-weight: 400;">public </span><span style="font-weight: 400;">SecurityFilterChain </span><span style="font-weight: 400;">setupOAuth</span><span style="font-weight: 400;">(HttpSecurity http) </span><span style="font-weight: 400;">throws </span><span style="font-weight: 400;">Exception {</span>
<span style="font-weight: 400;">          http.cors().and()</span>
<span style="font-weight: 400;">        </span> <span style="font-weight: 400;">.sessionManagement().sessionCreationPolicy(</span><i><span style="font-weight: 400;">STATELESS</span></i><span style="font-weight: 400;">).and()</span>
<span style="font-weight: 400;">        </span> <span style="font-weight: 400;">.csrf().disable()
</span>  <span style="font-weight: 400;">.oauth2ResourceServer().jwt().jwtAuthenticationConverter(getJwtAuthenticationConverter()).and().and()</span>
<span style="font-weight: 400;">        </span> <span style="font-weight: 400;">.authorizeHttpRequests(authz -&gt; authz</span>
<span style="font-weight: 400;">                </span> <span style="font-weight: 400;">.dispatcherTypeMatchers(DispatcherType.</span><i><span style="font-weight: 400;">ERROR</span></i><span style="font-weight: 400;">).permitAll()</span>
<span style="font-weight: 400;">                </span> <span style="font-weight: 400;">.anyRequest().hasAnyAuthority(</span><span style="font-weight: 400;">"SCOPE_back-end-resource-server"</span><span style="font-weight: 400;">)</span>
<span style="font-weight: 400;">        </span> <span style="font-weight: 400;">)</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">    return </span><span style="font-weight: 400;">http.build()</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">}</span>
<span style="font-weight: 400;">private </span><span style="font-weight: 400;">CompositeJwtAuthenticationConverter </span><span style="font-weight: 400;">getJwtAuthenticationConverter</span><span style="font-weight: 400;">() {</span>
<span style="font-weight: 400;">    </span><span style="font-weight: 400;">return new </span><span style="font-weight: 400;">CompositeJwtAuthenticationConverter()</span>
<span style="font-weight: 400;">        </span> <span style="font-weight: 400;">.extractAuthorities(</span><span style="font-weight: 400;">"ROLE_"</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">"realm_access"</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">"roles"</span><span style="font-weight: 400;">)</span>
<span style="font-weight: 400;">        </span> <span style="font-weight: 400;">.extractAuthorities(</span><span style="font-weight: 400;">"SCOPE_"</span><span style="font-weight: 400;">, </span><span style="font-weight: 400;">"scope"</span><span style="font-weight: 400;">)</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">}</span></pre>
<h3><b>Securing REST API</b></h3>
<p><span style="font-weight: 400;">This specific configuration guarantees that only verified users possessing the correct roles can reach secured content. Suppose there&#8217;s a role named ADMIN established, and we aim to restrict access to the list of entities exclusively to users assigned to this role.</span></p>
<pre><span style="font-weight: 400;">@RolesAllowed</span><span style="font-weight: 400;">({Roles.</span><i><span style="font-weight: 400;">ADMIN</span></i><span style="font-weight: 400;">})</span>
<span style="font-weight: 400;">@GetMapping</span><span style="font-weight: 400;">(</span><span style="font-weight: 400;">"/vehicles"</span><span style="font-weight: 400;">)</span>
<span style="font-weight: 400;">public </span><span style="font-weight: 400;">ResponseEntity&lt;?&gt; </span><span style="font-weight: 400;">getVehicles</span><span style="font-weight: 400;">() {</span>
<span style="font-weight: 400;">return </span><span style="font-weight: 400;">vehicleRepository.findAll()</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">}</span></pre>
<p><span style="font-weight: 400;">One way to obtain the JWT token in a user-friendly way is to integrate Keycloak in the front-end client, e.g. using appropriate Angular libraries, but this will be probably the subject of another post, so let&#8217;s take a quick look at initializing KeycloakService (from keycloak-angular library) and move on.</span></p>
<pre><span style="font-weight: 400;">function </span><span style="font-weight: 400;">initializeKeycloak</span><span style="font-weight: 400;">(keycloak: KeycloakService) {</span>
<span style="font-weight: 400;">return </span><span style="font-weight: 400;">() =&gt;</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">keycloak.</span><span style="font-weight: 400;">init</span><span style="font-weight: 400;">({</span>
<span style="font-weight: 400;">        </span> <span style="font-weight: 400;">config</span><span style="font-weight: 400;">: {</span>
<span style="font-weight: 400;">            </span> <span style="font-weight: 400;">url</span><span style="font-weight: 400;">: </span><b><i>environment</i></b><span style="font-weight: 400;">.</span><span style="font-weight: 400;">keycloakUrl</span><span style="font-weight: 400;">,</span>
<span style="font-weight: 400;">            </span> <span style="font-weight: 400;">realm</span><span style="font-weight: 400;">: </span><span style="font-weight: 400;">'test'</span><span style="font-weight: 400;">,</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">        </span><span style="font-weight: 400;">clientId</span><span style="font-weight: 400;">: </span><span style="font-weight: 400;">'front-end'</span><span style="font-weight: 400;">,</span>
<span style="font-weight: 400;">        </span> <span style="font-weight: 400;">}</span><span style="font-weight: 400;">,</span>
<span style="font-weight: 400;">        </span> <span style="font-weight: 400;">initOptions</span><span style="font-weight: 400;">: {</span>
<span style="font-weight: 400;">            </span> <span style="font-weight: 400;">checkLoginIframe</span><span style="font-weight: 400;">: </span><span style="font-weight: 400;">false,</span>
<span style="font-weight: 400;">            </span> <span style="font-weight: 400;">scope</span><span style="font-weight: 400;">: </span><span style="font-weight: 400;">'back-end-resource-server'</span>
<span style="font-weight: 400;">        </span> <span style="font-weight: 400;">}</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">})</span><span style="font-weight: 400;">;</span>
<span style="font-weight: 400;">}</span></pre>
<p><span style="font-weight: 400;">After successful authentication, we should receive a JWT token with similar structure.</span></p>
<pre><span style="font-weight: 400;">"realm_access"</span><span style="font-weight: 400;">: {</span>
<span style="font-weight: 400;">"roles"</span><span style="font-weight: 400;">: [</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">"offline_access"</span><span style="font-weight: 400;">,</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">"default-roles-test"</span><span style="font-weight: 400;">,</span>
<span style="font-weight: 400;">    </span> <span style="font-weight: 400;">"uma_authorization"</span><span style="font-weight: 400;">,</span>
<span style="font-weight: 400;"> </span> <span style="font-weight: 400;">   </span><span style="font-weight: 400;">"ADMIN"</span>
<span style="font-weight: 400;">]</span>
<span style="font-weight: 400;">}</span><span style="font-weight: 400;">,</span>
<span style="font-weight: 400;">"scope"</span><span style="font-weight: 400;">: </span><span style="font-weight: 400;">"openid profile email back-end-resource-server"</span></pre>
<p><span style="font-weight: 400;">While running a local server, we could use cURL to test our new endpoint:</span></p>
<pre><span style="font-weight: 400;">curl -H "Authorization: Bearer {valid_jwt_token}"</span><a href="http://localhost:4200/api/vehicles"> <span style="font-weight: 400;">http://localhost:4200/api/vehicles</span></a></pre>
<p><span style="font-weight: 400;">Even if we have the JWT token, the lack of the admin role will result in 403 response.</span></p>
<pre><i><span style="font-weight: 400;">StatusCode: 403</span></i>
<i><span style="font-weight: 400;">StatusDescription: Forbidden</span></i></pre>
<p><span style="font-weight: 400;">Let&#8217;s consider a scenario where the user logged in to the admin account:</span></p>
<pre><i><span style="font-weight: 400;">StatusCode: 200</span></i>
<i><span style="font-weight: 400;">StatusDescription: OK</span></i></pre>
<p><span style="font-weight: 400;">As we can see using Keycloak Role-Based Access Control (RBAC) in web applications offers several advantages. Firstly, it provides a robust and scalable solution for managing access to resources based on user roles and permissions. By defining roles within Keycloak and assigning them to users or groups, administrators can easily control who can access specific functionalities or data in the application. This granular access control enhances security by ensuring that only authorized users can perform certain actions or view sensitive information.</span></p>
<p><span style="font-weight: 400;">Moreover, Keycloak RBAC simplifies the management of access control policies across multiple applications or microservices. Centralizing user roles and permissions in Keycloak eliminates the need to implement and maintain separate authorization mechanisms in each application. This centralization streamlines administration tasks, reduces development overhead, and ensures consistency in access control policies across the entire ecosystem of applications.</span></p>
<p><span style="font-weight: 400;">In summary, leveraging Keycloak RBAC in web applications brings about enhanced security, modularity, centralization, and adaptability in access control management. These benefits contribute to building more secure, maintainable, and scalable web applications while simplifying administration tasks and ensuring consistent access control policies across the application ecosystem.</span></p>
<p>&nbsp;</p>
<p></p></div><div class="col-sm-1"></div></div>
<p>&nbsp;</p>
<p>Artykuł <a href="https://inero-software.com/securing-java-spring-endpoints-with-keycloak/">Keycloak Integration Guide: Securing Java Spring Endpoints with Keycloak</a> pochodzi z serwisu <a href="https://inero-software.com">Inero Software - Software Consulting</a>.</p>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5759</post-id>	</item>
	</channel>
</rss>
