<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: kishorek2511</title>
    <description>The latest articles on DEV Community by kishorek2511 (@kishorek2511).</description>
    <link>https://dev.to/kishorek2511</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1080164%2F7b3aa428-75bb-4af7-8724-27fe0ac4e141.png</url>
      <title>DEV Community: kishorek2511</title>
      <link>https://dev.to/kishorek2511</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kishorek2511"/>
    <language>en</language>
    <item>
      <title>How to secure Spring Boot with JWT Authentication and Authorization</title>
      <dc:creator>kishorek2511</dc:creator>
      <pubDate>Thu, 11 May 2023 06:07:45 +0000</pubDate>
      <link>https://dev.to/kishorek2511/how-to-secure-spring-boot-with-jwt-authentication-and-authorization-3c00</link>
      <guid>https://dev.to/kishorek2511/how-to-secure-spring-boot-with-jwt-authentication-and-authorization-3c00</guid>
      <description>&lt;p&gt;Hello learners, here we are going to learn about spring security implementation with spring boot3.0 and JWT.&lt;/p&gt;

&lt;p&gt;JWT (JSON Web Tokens) is a standard for representing claims securely between two parties. It is a compact, URL-safe means of representing claims to be transferred between two parties. A JWT consists of three parts: a header, a payload, and a signature.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8Kh0m95e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kd35nvvv41e1xn1f8566.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8Kh0m95e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kd35nvvv41e1xn1f8566.png" alt="Image description" width="800" height="399"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Header consist of algorithm and token type, Payload consist of subject,name,issuedat, Verify Signature is the combination of encoded header,payload and secret key. JWT token is a combination of these three.&lt;/p&gt;

&lt;p&gt;Spring Security can be integrated with JWT to secure web applications by generating, parsing, and validating JWTs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9F9OHz2i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/npdy4avhsk59alvbnt6a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9F9OHz2i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/npdy4avhsk59alvbnt6a.png" alt="Image description" width="474" height="314"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The following is a overview of how JWT authentication and authorization works in Spring Boot:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;User authentication&lt;/strong&gt;: When a user logs in to the system, the server verifies the user's credentials, such as their username and password. If the credentials are correct, the server generates a JWT for the user and returns it to the client.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JWT creation&lt;/strong&gt;: The server creates a JWT by encoding the user's identity information and other necessary data, such as expiration time, into a JSON object. The JSON object is then signed using a secret key or public/private key pair.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Token storage&lt;/strong&gt;: The client stores the JWT locally, usually in a cookie or local storage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Authorization&lt;/strong&gt;: For each subsequent request, the client sends the JWT in the request header. The server verifies the JWT's signature and decodes its contents to extract the user's identity information and other details. Based on this information, the server can then authorize the user to access certain resources or perform certain actions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Token validation&lt;/strong&gt;: The server can also validate the JWT to ensure that it has not been tampered with and has not expired. If the token is invalid, the server can reject the request or request the user to log in again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Steps to Implement JWT
&lt;/h2&gt;

&lt;p&gt;Step 1: Add the JWT dependencies&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cZ2P53ew--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/stjmnmsg4waa12jujj9l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cZ2P53ew--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/stjmnmsg4waa12jujj9l.png" alt="Image description" width="465" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step 2: Create a endpoint to authenticate and generate JWT&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eDhf3Pes--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r53izbkopq2x9tczt02l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eDhf3Pes--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r53izbkopq2x9tczt02l.png" alt="Image description" width="800" height="127"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RZxRbLro--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezcst3414xp6lgwlq0qz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RZxRbLro--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezcst3414xp6lgwlq0qz.png" alt="Image description" width="753" height="358"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In CreateToken method, we are creating token by setting claims,subject,issued and expiration date, signature.&lt;/p&gt;

&lt;p&gt;Step 3: Create a filter to validate JWT&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BXt4KxTq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mcx7ls3tqnnfjo5ztuc5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BXt4KxTq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mcx7ls3tqnnfjo5ztuc5.png" alt="Image description" width="800" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;create a class for filter and extends with OncePerRequestFilter, override doFilterInternal method and invoke authorization token from header. Validate the token by extracting username and expiration date from the token. This filer is used to validate the JWT token for each API call.&lt;/p&gt;

&lt;p&gt;Step 4: Add the configuration to authorize API calls&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_WoISffU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4gu3fk0rwqaz61fw9qx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_WoISffU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4gu3fk0rwqaz61fw9qx.png" alt="Image description" width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create a class and add a method to authorize, in above code given access to "/products/new","/products/authenticate" endpoints to all users, for "/products/**" endpoint only authenticated users with valid JWT can access.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Testing the implementation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Authenticating and Generating the token&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--k09HK3SQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zfchpdxavnvmjbkcamq3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k09HK3SQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zfchpdxavnvmjbkcamq3.png" alt="Image description" width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Copy the generated token and add it to other API call, I'm accessing the below API with Admin credentials&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--L7QQiv-c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/48sc0o626r2dryzo3dkr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--L7QQiv-c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/48sc0o626r2dryzo3dkr.png" alt="Image description" width="800" height="501"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we try to access the API with Admin credentials where only user can access will get 403 Forbidden error&lt;/p&gt;

</description>
      <category>development</category>
      <category>webdev</category>
      <category>programming</category>
      <category>java</category>
    </item>
    <item>
      <title>Spring Security role based Authentication &amp; Authorization Implementation with Spring Boot 3.0</title>
      <dc:creator>kishorek2511</dc:creator>
      <pubDate>Wed, 10 May 2023 10:30:15 +0000</pubDate>
      <link>https://dev.to/kishorek2511/spring-security-role-based-authentication-authorization-implementation-with-spring-boot-30-23c0</link>
      <guid>https://dev.to/kishorek2511/spring-security-role-based-authentication-authorization-implementation-with-spring-boot-30-23c0</guid>
      <description>&lt;p&gt;Hello learners, here we are going to know about spring security implementation with spring boot. Spring security provides authentication, authorization, and protection against common attacks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vPkBWdTe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/co3i01kpu0gplplqjjve.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vPkBWdTe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/co3i01kpu0gplplqjjve.png" alt="Image description" width="474" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Authentication&lt;/strong&gt; - Authentication is how we verify the identity of the user trying to access a particular resource, once authentication is performed we know the identity and can perform authorization.&lt;br&gt;
&lt;strong&gt;Authorization&lt;/strong&gt; - Authorization means giving permission to access particular resource/url.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps to Implement Spring Security&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Step 1: Add Spring Security dependency in POM.XML&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d-65E0wz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sqcxf5145bhtuu5cylm6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d-65E0wz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sqcxf5145bhtuu5cylm6.png" alt="Image description" width="670" height="129"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step 2: Create a configuration class , add authentication and authorization methods.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4tIE4vLU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16qtn3monsz131frdh6x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4tIE4vLU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16qtn3monsz131frdh6x.png" alt="Image description" width="800" height="776"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;@EnableWebSecurity&lt;/strong&gt; provides default security configuration to our application.Default security activates both HTTP security filters and the security filter chain and applies basic authentication to our endpoints.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;@Configuration&lt;/strong&gt; tells Spring Boot to scan the class for bean definitions and register them with the application context.&lt;/p&gt;

&lt;p&gt;authenticateProvider() method is used to store all the user deatils like username, password, roles.Spring Security contains DaoAuthenticationProvider class which contains userDetailsService and passwordEncoder.passwordEncoder() is used to encrypt the password and encrypted password is stored in DB.&lt;/p&gt;

&lt;p&gt;SecutityFilterChain() method is to authorize the resources, here&lt;br&gt;
.requestMatchers("/products/welcome","/products/new").permitAll() is to give access to all the users, any user can access those two urls.&lt;br&gt;
requestMatchers("/products/**").authenticated() is to give access to authenticated users.&lt;/p&gt;

&lt;p&gt;Step 3: Implement role based authorization&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--q7gxE4Gl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fbe9u6mwftwu4ert7klp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--q7gxE4Gl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fbe9u6mwftwu4ert7klp.png" alt="Image description" width="534" height="315"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;@PreAuthorize&lt;/strong&gt; annotation is used to specify a expression that will be evaluated before the method is executed. If the expression evaluates to true, the method is executed otherwise, an AccessDeniedException is thrown.&lt;/p&gt;

&lt;p&gt;The getAllProducts() method can only be executed by users with the ROLE_USER role, while the getProductById() method can be executed by users with the ROLE_ADMINrole.&lt;br&gt;
Testing the implementation&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eO2l275i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7hkxfytuetal5svrcbi4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eO2l275i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7hkxfytuetal5svrcbi4.png" alt="Image description" width="531" height="271"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Added the sample code to test the implementation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bzYQs3SZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/godm7eeycgv91hblt2a4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bzYQs3SZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/godm7eeycgv91hblt2a4.png" alt="Image description" width="800" height="274"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After giving user credentials user can able to access the user endpoint&lt;br&gt;
When user try to access Admin endpoint with user credential, error page will display&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wqiyDB1u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eq04wsq91q2ozo3ncovf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wqiyDB1u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eq04wsq91q2ozo3ncovf.png" alt="Image description" width="800" height="423"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>api</category>
      <category>java</category>
    </item>
  </channel>
</rss>
