<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Saurabh Kumar on Medium]]></title>
        <description><![CDATA[Stories by Saurabh Kumar on Medium]]></description>
        <link>https://medium.com/@saurabhk15?source=rss-a199039e4fea------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*bfVGRczP9BAaFqNl.</url>
            <title>Stories by Saurabh Kumar on Medium</title>
            <link>https://medium.com/@saurabhk15?source=rss-a199039e4fea------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 07:51:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@saurabhk15/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[StringBuilder Demystified]]></title>
            <link>https://saurabhk15.medium.com/stringbuilder-demystified-582550bcbf66?source=rss-a199039e4fea------2</link>
            <guid isPermaLink="false">https://medium.com/p/582550bcbf66</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[leadership-coaching]]></category>
            <category><![CDATA[interview]]></category>
            <category><![CDATA[string]]></category>
            <dc:creator><![CDATA[Saurabh Kumar]]></dc:creator>
            <pubDate>Wed, 16 Sep 2020 10:55:05 GMT</pubDate>
            <atom:updated>2020-09-16T13:35:05.467Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-zq12UtTn-VyYSr0.png" /></figure><p>Let’s understand what makes every lead developer say that you should know how to create your own StringBuilder if you plan to modify your strings.<br> And most of all — what is the difference between string concatenation vs using this pattern.</p><p>Simply put what we want to do is — an efficient management of space and performance.</p><p>This is the simplest of all the implementations with core characteristics of a StringBuilder</p><h3>MyStringBuilder</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b20bf1db4452ee994a72c18bfb202ab/href">https://medium.com/media/4b20bf1db4452ee994a72c18bfb202ab/href</a></iframe><p>If you feel it’s cool — let me know which Java language framework object I should demystify.</p><p><em>Originally published at </em><a href="https://dev.to/saurabhpro/stringbuilder-demystified-1p64"><em>https://dev.to</em></a><em> on September 16, 2020.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=582550bcbf66" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create your ENUM (yes from Java’)]]></title>
            <link>https://saurabhk15.medium.com/create-your-enum-yes-from-java-ce86b37a8bcc?source=rss-a199039e4fea------2</link>
            <guid isPermaLink="false">https://medium.com/p/ce86b37a8bcc</guid>
            <category><![CDATA[datastrucutre]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[interview]]></category>
            <category><![CDATA[enum]]></category>
            <dc:creator><![CDATA[Saurabh Kumar]]></dc:creator>
            <pubDate>Wed, 02 Sep 2020 19:55:45 GMT</pubDate>
            <atom:updated>2020-09-03T02:15:10.151Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QiK8s2-MWPgwdbtn.png" /></figure><p>If you are always in awe of the simple yet powerful Enums in java, follow along ;)</p><p>In Java, enum data type internally inherits from java.lang.Enum class.</p><p><strong>Use cases to consider:</strong></p><ul><li>should have methods name(), ordinal() as final which no one can override</li><li>always maintain singleton status for each enum value</li><li>you shouldn’t be able to access enum constructor (make it private)</li><li>should have ordinal (consider sequential numbering idea)</li><li>the enum should have a values() which returns all enum values.</li></ul><p>I wrote a simple yet fairly complete implementation, so I welcome any feedback.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/71e82e2f7df76b401d2018f1a8cac45e/href">https://medium.com/media/71e82e2f7df76b401d2018f1a8cac45e/href</a></iframe><p><em>Originally published at </em><a href="https://dev.to/saurabhpro/my-enum-yes-from-java-a89"><em>https://dev.to</em></a><em> on September 2, 2020.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ce86b37a8bcc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Java Concurrency Is Easy Day-1]]></title>
            <link>https://saurabhk15.medium.com/java-concurrency-is-easy-day-1-4b41b2fe4122?source=rss-a199039e4fea------2</link>
            <guid isPermaLink="false">https://medium.com/p/4b41b2fe4122</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[multithreading]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Saurabh Kumar]]></dc:creator>
            <pubDate>Tue, 28 Jul 2020 17:02:51 GMT</pubDate>
            <atom:updated>2021-07-06T15:47:55.503Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*TMra6fABTfsKqY55.png" /></figure><p>The first thing you need to understand is Concurrency is not Threads!</p><p><strong>Thread</strong> is a task — <strong>Concurrency</strong> is about how you can do it so that tasks run safely in your application. Concurrency can be challenging when some enthusiast dev tries to over-engineer a problem that is not really well managed.</p><p>Also, make note that Parallelism is not the same as Concurrency! <a href="https://stackoverflow.com/a/24684037/4853910">Read more here</a>.</p><h3>How do you make concurrent tasks?</h3><p>The most common way is making a Thread <em>(read about threads </em><em>start(), </em><em>run() etc. methods)</em> - and over time Java has come up with a very simple library class called Executors. <br> They are the sole handlers of how/when to run your threads (actions). You can submit a task to them, and they will run it as soon as possible. We don&#39;t really need to care about anything other than the task itself!</p><p>So a simple way to making an Executor and then submitting a task</p><p>and that’s it — the executor will simply run the task as it sees fit.<br> You can actually use this approach and keep adding more tasks, and each task will ideally be run on a separate thread.</p><h3>So, what exactly is Concurrency!</h3><h3>A basic problem</h3><p>You have a dictionary, you want to search a word “stoic” — the first thought that came to head after reading this is that <em>“oh — yes! I will simply skip more than half of the dictionary as “S” comes after the mid — and hence I will be closer to my goal</em></p><p>but think your sister also wanted to find the word “manic” — she tries to access the dictionary, but wait — you were using it, so she asks you <em>“could you please give me the dictionary</em> and you reply <em>“I’m not done yet…”</em></p><p>In the best case, she waits until you are done or she steals the dictionary from your hand and runs away 😉 If she was happy to wait for you — turn the pages, and eventually you reach the location of the word.</p><h3>Now in java terms</h3><p>What you can imagine is that there were two Thread - who had a defined work and it is possible that the work might be conflicting by nature or you and your sister can do some mutually exclusive work without ever fighting.</p><blockquote><em>To summarize, we have</em></blockquote><ul><li><strong>threads</strong> <em>(with some action — in java we call them </em><em>Runnable)</em></li><li><strong>shared resources</strong> <em>(your CPU memory just like the dictionary)</em></li><li>and a <strong>manager</strong> says your mom dictates who will get the dictionary first and who will wait or if you should stop what you were doing and give the resource to your sister! <em>(in java we call it “interrupt”)</em> — obviously every runnable can be interrupted so we always guard the threads with a notification system with isInterruptedflag, this is very important</li></ul><p>Now the question is shouldn’t it be better that we could specify that You are using the dictionary exclusively and your sister respects this!</p><p>So, in Java the concept of Lock came into the picture - with just two basic methods - lock() and unlock()</p><p><em>Please forget about the </em><em>synchronized keyword for now which every genius programmer will tell you in concurrency. This is critical that you first understand </em><em>Lock and then the fancy keyword.</em></p><p>So any class ( <em>the resource which can be shared eg. </em><strong><em>Account</em></strong>) can keep the property of this Lock type inside and as soon as you start accessing the resource - you set it to accountLock.lock() - now the other person can&#39;t lock() it until you explicitly say accountLock.unlock() stating that the task is done.</p><p>Essentially this is how you handle concurrency in simpler terms. Some more interview worthy examples are:</p><p>We will see how we can fix it properly — spoilers <em>“everything is handled by locks “</em> 😋</p><p>🔚</p><p>So — if you are clear until this point, do let me know and wait for Day-2 😄</p><p><em>Originally published at </em><a href="https://dev.to/saurabhpro/java-concurrency-is-easy-day-1-46od"><em>https://dev.to</em></a><em> on July 6, 2021.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4b41b2fe4122" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Git Tags .FTW]]></title>
            <link>https://saurabhk15.medium.com/git-tags-ftw-21cffef9a719?source=rss-a199039e4fea------2</link>
            <guid isPermaLink="false">https://medium.com/p/21cffef9a719</guid>
            <category><![CDATA[git-tags]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[version-control]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Saurabh Kumar]]></dc:creator>
            <pubDate>Sat, 25 Jul 2020 17:29:28 GMT</pubDate>
            <atom:updated>2020-07-25T17:29:28.561Z</atom:updated>
            <content:encoded><![CDATA[<h3>Git Tags .FTW</h3><p>How they make life simpler</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TcpV_4XjXWpBqZ7Z" /><figcaption><a href="https://help.talend.com/reader/o2DMkp1_WVLFHSyNkjxn4w/oTkWboLA1NExIwmKqQdJIQ">https://help.talend.com/reader/o2DMkp1_WVLFHSyNkjxn4w/oTkWboLA1NExIwmKqQdJIQ</a></figcaption></figure><p>Tagging is an additional mechanism used to create a snapshot of a Git repo. Typically, it is used to create semantic version number identifier tags (release-20.0.7 ;) that correspond to software release cycles.</p><p><strong>Basically = A fixed point in git commit history</strong></p><blockquote><em>Eg: I want to migrate my existing financial statements microservice project from h2 -&gt; mongodb, how can I have the current fully functional h2 version for future</em></blockquote><blockquote><em>Solution: make a tag from the current version of my project</em></blockquote><h4># Type of Tags</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/414/0*lu6HZpKuA4CxYdT1.png" /><figcaption>preferred : annotated</figcaption></figure><ol><li><strong>Lightweight</strong> version and mostly good for private projects like mine (they don’t have any message or info like who did what)</li></ol><pre><strong>➜ </strong>git tag v0.2</pre><p>2. <strong>Annotated</strong> version has message like commit and can have more info, simply put — metadata</p><pre><strong>➜ </strong>git tag -a v0.2 -m “working h2 version of app”</pre><ul><li>If you don’t use -m, a text editor will open up where you can enter your required info</li><li>By default git tag takes the current head as fixed point in time, but you can have your own commit as the point like this</li></ul><pre><strong>➜ </strong>git tag -a v1.2 15027957951b64cf874c3557a0f3547bd83b3ff6</pre><ul><li>Also, it&#39;s possible to overwrite the tag just like world burning push -f command</li></ul><pre><strong>➜ </strong>git tag -a -f v1.2 15027957951b64cf874c3557a0f3547bd83b3ff6</pre><h4># How to push tags</h4><pre><strong>➜ </strong>git push origin v0.1</pre><h4># How to fetch tags</h4><pre><strong>➜ </strong>git pull</pre><p>Or if the remote tags are not available try</p><pre><strong>➜ </strong>git fetch --all --tags</pre><h4># How to checkout tags to our own branch</h4><pre><strong>➜ </strong>git checkout -b my-version tags/v0.1 <br>// point to this tag in my new branch &#39;my-version&#39;</pre><h4># List all tags in your repo</h4><pre><strong>➜ </strong>git tag</pre><ul><li>Also you can filter on it with</li></ul><pre><strong>➜  </strong>git tag -l `*basic` <br>//all tags having basic in them</pre><h4># Finally, delete a tag</h4><pre><strong>➜ </strong>git tag -d v0.1 (locally)<br><strong>or <br>➜ </strong>git push --delete origin v0.1 (from remote)</pre><p>A Complete Example:</p><pre><strong>➜ git:(master)</strong> git tag -a v0.1<br><strong>➜ git:(master)</strong> git push origin v0.1<br>Counting objects: 1, done.<br>Writing objects: 100% (1/1), 270 bytes | 270.00 KiB/s, done.<br>Total 1 (delta 0), reused 0 (delta 0)<br>To <a href="https://github.com/xxxxxx/xxxxxx.git">https://github.com/xxxxxx/xxxxxx.git</a><br>* [new tag] v0.1 -&gt; v0.1<br><strong>➜ git:(master)</strong></pre><h3><strong>Bonus</strong></h3><ol><li>You retrieve multiple tags from your remote repository. <strong>Then, retrieve the latest tag available by using the “git describe” command.</strong></li></ol><pre><strong>➜ </strong>tag=$(git describe — tags `git rev-list — tags — max-count=1`)<br><strong>➜ </strong>echo $tag<br>v0.2</pre><p>2. Inspect the state of your branch</p><pre><strong>➜ </strong>git log — oneline — graph<br>* b5c0345 (<strong>HEAD -&gt; my-version</strong>, <strong>tag: v1.0-rc</strong>, <strong>tag: v0.2</strong>, <strong>tag: v0.1</strong>, <strong>origin/master</strong>, <strong>origin/HEAD</strong>, <strong>master</strong>) queryDSL example<br>* 043a12c TextIndexed updated for Spring 2.3+<br>* f9e007a projection and aggregation<br>* 48d59eb mongo query dsl — refer README<br>* a59e6ca controllers with mongo template and mongorepository<br>* 9e6a36d init<br>* bfa47c0 Initial commit<br><strong>(END)</strong></pre><p>If you found this concise and helpful, do like <a href="https://emojipedia.org/smiling-face-with-smiling-eyes/">😊</a></p><p>Reference:</p><ul><li><a href="https://www.atlassian.com/git/tutorials/inspecting-a-repository/git-tag">https://www.atlassian.com/git/tutorials/inspecting-a-repository/git-tag</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=21cffef9a719" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Masterchef Australia and The Lockdown]]></title>
            <link>https://saurabhk15.medium.com/masterchef-australia-and-the-lockdown-cd5433c9d475?source=rss-a199039e4fea------2</link>
            <guid isPermaLink="false">https://medium.com/p/cd5433c9d475</guid>
            <category><![CDATA[personal-development]]></category>
            <category><![CDATA[masterchef-australia]]></category>
            <category><![CDATA[coronavirus]]></category>
            <category><![CDATA[cooking]]></category>
            <category><![CDATA[masterchefindia]]></category>
            <dc:creator><![CDATA[Saurabh Kumar]]></dc:creator>
            <pubDate>Tue, 21 Jul 2020 18:45:51 GMT</pubDate>
            <atom:updated>2020-07-21T18:45:51.905Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><em>A blog on how this show kept me going</em></blockquote><p>Before 2020, I knew about this show called Masterchef Australia… I knew this was the greatest show on cooking in the WORLD. But, In my time of watching The Flash, The Arrow and 40 other high octane drama… This show was somewhat left lurching in the shadows.</p><p>Then I started cooking myself, and… more aptly enjoying it!</p><p>The struggle for me since a long time was about</p><ul><li>“who cooks for themselves”,</li><li>I have no one to share my cooking with,</li><li>will i even cook food that’s worth presenting to anyone !</li></ul><p>Obviously these things and the obnoxiously tight headspace meant that I cooked, but always in pain. I never bothered to plate my dishes in a way that I would like to click a picture and memorize it.</p><p>Then last year I was tired of the boring old dramas trying those age old practice of just dragging things… I’m calling out you “The Flash” :P</p><p>Also, all my better dramas had ended in 2019 — No #BBT, no #ModernFamily and what not. I turned my direction to the indian version of the MAsterchef with Ranveer Brar and something changed in me. Even though the dishes the contestants made were class apart from i believe i’m capable of, the idea of understanding food kept me hooked. I learnt how to cook “<em>khada parasad</em>”, how to eat ice-cream and how to add — and i mean it in a very pleasant way — acidity in the food ;)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*txvF2wn9GYavOWiC.jpg" /></figure><p>Then i cooked mainly because I trusted myself with my health and just when i was striking a balance with cooking 3 times a week — the Pandemic happened. We all were locked in our houses. The stores closed, food delivery was impacted. And to make matters worse my locality was in the #redzone. This meant high risk. And so, I naturally had to adapt!</p><p>I started with ordering veggies and eggs on weekly basis from bigbasket, amazon fresh. And other groceries thanks to Grofers. One of the first things I started with was “chatani” — something I learnt from Masterchef India 2020. How adding olive oil in them can make them sing. And so i bought a nutribullet copy :P</p><p>But it didn’t matter — the taste was out of this world. I have never had a chatani tastier than what I made. But i was still not cooking everyday.</p><p>As luck would have it, one day when i was going through the list of probable shows I would watch — I stumbled upon episode 35 of Masterchef Australia 2020. At first glance i thought the show must be nearing its end as its already 35 episodes in, why not burn my time as just watch from episode 1. And so I did.</p><p>Everyday for a week straight i was glued to the goodness of cooking for 6–8 hours. I stopped working out, I stopped cooking (what irony) and just about everything. But the satisfaction of seeing such amazing creations sparked a new light in me. I wanted to experiment with vinegar in my Indian cuisines like stuffed paratha. I made “beetroot roti”, “palak roti”, even tried (and failed) in making poached eggs. But the journey continued.</p><p>It was the month of june now… I was all caught up the past episodes of this season, and then I realized — we were only just begun. Out of the 24 contestants, the show still had more than 15 left. I was excited for what to come.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/362/0*OJGqtJDtenJAStn-.jpg" /></figure><p>Every Week I would wait for the episodes to air in India on Hotstar and it had replaced all my other shows in terms of priority. I cheered for Jess, Tessa, Laura and Reynold. Never like Poh much :P</p><p>I enjoyed the masterclass from the chefs where they taught how to present an ice-cream “<strong><em>quenelle”. </em></strong>And then towards the finale — I started loving everyone, just because they were creating literal masterpieces which I understood.</p><p>The best thing I saw was in the semi-finals when Laura couldn’t strip the apple and Emilia came and literally did it for her. In a world where competition is everything, in a three member semi-final…that’s just unseen for me. That same semi-final saw the end of Reynolds (my bet for this season) failing in the dessert dish.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*azzPcp5UOHq_NP5V" /></figure><p>And then the finals. I cannot express the sadness of watching them not being able to hug whenever someone was evicted. This time they did. Laura and Emilia were friends from Season 6 (I will watch all now I think:P)</p><p>Another thing I really loved about this show was, there was no bias on color, gender, orientation or being fat or chubby. Whoever you were — only one thing mattered.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cd5433c9d475" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spring Boot 2+ with Spring Security and JWT]]></title>
            <link>https://saurabhk15.medium.com/spring-boot-2-with-spring-security-and-jwt-ad95c30d75f9?source=rss-a199039e4fea------2</link>
            <guid isPermaLink="false">https://medium.com/p/ad95c30d75f9</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[spring-security]]></category>
            <dc:creator><![CDATA[Saurabh Kumar]]></dc:creator>
            <pubDate>Wed, 08 Jul 2020 12:27:03 GMT</pubDate>
            <atom:updated>2020-07-08T12:29:48.891Z</atom:updated>
            <content:encoded><![CDATA[<h3>Spring Boot 2.3+ with Spring Security and JWT</h3><blockquote>TL;DR —</blockquote><p>I was always skeptical about adding securities in my microservices which I don’t plan to deploy or release — but finally collected an easy set of principals (<em>pun intended</em>) which might help you to get your own authentication set up easily.</p><blockquote>Ready — Lets Go.</blockquote><p>Let&#39;s understand first— from the days of J2EE, servlets are used to capture the <em>HttpServletRequest</em>. In Spring Boot we have wrappers and pre-written managers to do the same so — we don&#39;t need to worry about a lot.</p><h3>1. Add dependencies and User class</h3><p>I’m using Maven so start by adding in you pom.xml</p><pre>&lt;!--spring boot supprot for securties and filters--&gt;<br>&lt;dependency&gt;<br>    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;<br>    &lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;<br>&lt;/dependency&gt;<br><br>&lt;!--google library to convert string to json--&gt;<br>&lt;dependency&gt;<br>    &lt;groupId&gt;com.google.code.gson&lt;/groupId&gt;<br>    &lt;artifactId&gt;gson&lt;/artifactId&gt;<br>    &lt;version&gt;2.8.5&lt;/version&gt;<br>&lt;/dependency&gt;</pre><pre>&lt;!--json web token--&gt;<br>&lt;dependency&gt;<br>    &lt;groupId&gt;io.jsonwebtoken&lt;/groupId&gt;<br>    &lt;artifactId&gt;jjwt&lt;/artifactId&gt;<br>    &lt;version&gt;0.9.1&lt;/version&gt;<br>&lt;/dependency&gt;<br></pre><p>Start by enabling the user credentials — what we want is :</p><ul><li>an input USERNAME+PASSWORD</li><li>and a way to generate encrypted password which we can save in database</li><li>also, some validation to authenticate if the input is ok</li></ul><p>Let&#39;s do that by adding the BCryptPasswordEncoder which will take care of the <em>password encryption and decryption</em> — why both — think!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4431f7e8b8e898517e58d2b23335e225/href">https://medium.com/media/4431f7e8b8e898517e58d2b23335e225/href</a></iframe><p>We need to store the information about the username and password somewhere — right?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b5f2c083d1599ec0d05027b63d544f17/href">https://medium.com/media/b5f2c083d1599ec0d05027b63d544f17/href</a></iframe><p>So let&#39;s add it to the <strong>Users</strong> table in our DB — note that Spring Security already provides an interface to “<em>Provides core user information” </em>as an interface- UserDetails</p><p>This base type is also necessary for getting the User back from DB using spring boots UserDetailService which is the “<em>Core interface that loads user-specific data”.</em></p><blockquote>Other recommended way is to extend</blockquote><blockquote>org.springframework.security.core.userdetails.User (which itslef implements UserDetails)</blockquote><blockquote>as the parent class to your ownUser — it makes creating user hell lot easier.</blockquote><h3>2. User save and retrieve services</h3><p>We need a service to store this information in DB</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ef7a44f1451aacbcf7a7dc1c889af02/href">https://medium.com/media/3ef7a44f1451aacbcf7a7dc1c889af02/href</a></iframe><p>Now we are good to take the user input and store it in our database correctly i.e. with encrypted password, did you notice the <em>confirmPassword(null)</em> — think!</p><p>Also, we are using the UserDetailsService by spring boot, and provide our own implementation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a289fe8bb24870c97491fdeca0de36c/href">https://medium.com/media/5a289fe8bb24870c97491fdeca0de36c/href</a></iframe><blockquote>NOTICE the return type is not User?</blockquote><p>There is another default implementation from SpringBoot <em>InMemoryUserDetailsManager </em>which should only be used <em>for testing and demonstration purposes, where a full-blown persistent system isn’t required.</em></p><h3>3. Validation of user credentials</h3><p>Ok, what now — here comes the interesting part…</p><p>We will use Filters to put our <em>UserDetailService</em> as an intercepter — Filters (servlets) are what stands between our UI and controllers. — So</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4eaf190e3ab99bca50fa28c36072577e/href">https://medium.com/media/4eaf190e3ab99bca50fa28c36072577e/href</a></iframe><blockquote>I hope this makes sense — if not, do comment!</blockquote><h4>JWT (JSON Web Tokens)</h4><p>You also notice here I’m using JWTProvider which is a custom implementation for JWT related stuff like creating JWT token and worry about</p><ul><li>When should the token (Bearer token) expire</li><li>What is the id, username, and password that is part of the token itself and can be retrieved by the token itself</li><li>Define your own secret key used for encrypting the password — <em>Authentication 101 public-private key</em></li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c6b599612e7cf210c429091a5b2f24b1/href">https://medium.com/media/c6b599612e7cf210c429091a5b2f24b1/href</a></iframe><blockquote>watch carefully this last method!</blockquote><p>If everything is ok we are good to send the success response to client</p><h3>4. User login/register Controller</h3><p>Let’s create the login object (an abstraction from our real User)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2770e163441e67295c180ebd7c1bf167/href">https://medium.com/media/2770e163441e67295c180ebd7c1bf167/href</a></iframe><p>let&#39;s see our user controller now-</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/394b553d5c39da61a65addf08f2cd763/href">https://medium.com/media/394b553d5c39da61a65addf08f2cd763/href</a></iframe><p>If you see the “/register” it has the complete User object as method argument and we will do validation on it with Spring Boot’s Validator interface like min password length and stuff.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/307b88328d79a1111e58515348f35824/href">https://medium.com/media/307b88328d79a1111e58515348f35824/href</a></iframe><p>Then the “/login” in controller only takes LoginRequest which we created above and the Authentication bean we created previously in SecurityConfig (coming soon)</p><p>This does the authentication and if it&#39;s fine saves it to the <em>application context</em>.</p><blockquote>We return the token because we must — its the only key of our door ;)</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72e37bec9387ccab1b11cf1659ece03e/href">https://medium.com/media/72e37bec9387ccab1b11cf1659ece03e/href</a></iframe><p>that’s it — our login and register functions are ready — but what if login is incorrect — we should send a 401 status back, to ease this we can simply create our own AuthenticationEntryPoint#commence()</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5621df40b70e48a556352ae3621a4b4b/href">https://medium.com/media/5621df40b70e48a556352ae3621a4b4b/href</a></iframe><p>So, now we are truly done, not quite ;)</p><p>Just one last thing to do — we actually have everything to support login a user but we still need to tell Spring Boot what is authorized and what now — that&#39;s done by the WebSecurityConfigurerAdapter</p><p>And this class basically takes the above 401 class as an injected component and does its magic in the <em>configure(HttpSecurity).</em></p><p>This is also the place where we tell our application which AuthenticationManager and password encoder to use <em>configure(AuthenticationManagerBuilder)</em></p><p>And this is where we tell the app that the <em>JWTAuthenticationFilter</em> we created above has to be executed before <em>UsernamePasswordAuthenticationFilter.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1aafe31d81e420d33937302008a1c4ab/href">https://medium.com/media/1aafe31d81e420d33937302008a1c4ab/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ad95c30d75f9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Install Maven on Mac OSX Catalina]]></title>
            <link>https://saurabhk15.medium.com/install-maven-on-mac-osx-catalina-c2e229a61763?source=rss-a199039e4fea------2</link>
            <guid isPermaLink="false">https://medium.com/p/c2e229a61763</guid>
            <category><![CDATA[maven]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Saurabh Kumar]]></dc:creator>
            <pubDate>Sat, 27 Jun 2020 18:15:25 GMT</pubDate>
            <atom:updated>2020-06-27T18:16:26.794Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Download and unzip from — <a href="https://maven.apache.org/download.cgi">https://maven.apache.org/download.cgi</a></blockquote><p>I placed them in my <strong>$HOME</strong> directory, then</p><pre><strong>➜ ~</strong> nano $HOME/.profile</pre><p>and add this to the file (<em>control+x to exit</em>)</p><pre>export PATH=$HOME/apache-maven-3.6.3/bin:$PATH</pre><p>after exit, reload the config</p><pre><strong>➜ ~</strong> source .profile</pre><p>now run</p><pre><strong>➜  ~</strong> mvn -version</pre><pre><strong>Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)</strong></pre><pre>Maven home: /Users/saurabhkumar/apache-maven-3.6.3</pre><pre>Java version: 14.0.1, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home</pre><pre>Default locale: en_IN, platform encoding: UTF-8</pre><pre>OS name: &quot;mac os x&quot;, version: &quot;10.15.4&quot;, arch: &quot;x86_64&quot;, family: &quot;mac&quot;</pre><p>that’s it. Its the shortest and cleanest summary you can find anywhere. If you like it — do clap ;)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c2e229a61763" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Writing a GOOD Unit Test]]></title>
            <link>https://saurabhk15.medium.com/writing-a-good-unit-test-32d68c08f6f8?source=rss-a199039e4fea------2</link>
            <guid isPermaLink="false">https://medium.com/p/32d68c08f6f8</guid>
            <category><![CDATA[testng]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[junit]]></category>
            <dc:creator><![CDATA[Saurabh Kumar]]></dc:creator>
            <pubDate>Fri, 16 Feb 2018 04:16:03 GMT</pubDate>
            <atom:updated>2018-02-16T04:20:01.203Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Point 1</strong> — the Language in which a Unit Test is written <strong>DOESN’T MATTER!</strong></p><blockquote><strong>This must be clarified to the beginners. Teach Principles First, Then ask them to write UT.</strong></blockquote><p>now that point 1 is clear -</p><p><strong>Point 2 </strong>— This post is written in Java with TestNg framework (you can still apply same principles in JUnit 5 with other languages like C# etc.)</p><p><strong>Point 3</strong> — The name of @Test method should be as descriptive as it can be. See Example below to see some good naming practices — THIS IS PROBABLY THE MOST IMPORTANT THING IN THIS POST!</p><blockquote>Thankfully jUnit5 has the “@DisplayText()” decorator. <a href="https://medium.com/@saurabhk15/testing-in-java-with-junit-5-good-parts-31ccad53415d">[#Read This Post]</a></blockquote><p><strong>Point 4 </strong>— A Blank Unit @Test method <strong>should pass</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f5173f9c203eb840616abec74da5f8b/href">https://medium.com/media/9f5173f9c203eb840616abec74da5f8b/href</a></iframe><p><strong>Point 5</strong>— Every @Test method gets its <em>own copy of instance variables</em>. REALLY! Try — so it’s a matter of choice to put the instantiation while declaring them</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47549db7693f1c3ac2212184e8da7678/href">https://medium.com/media/47549db7693f1c3ac2212184e8da7678/href</a></iframe><p>or in @BeforeMethod which creates a new object every-time a @test method is called.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2fa779235332ec3574a2853944371e27/href">https://medium.com/media/2fa779235332ec3574a2853944371e27/href</a></iframe><p><strong>Point 6</strong>— If there is a chance that a method MIGHT throw a simple or useless error — you could THINK that putting a try-catch inside your @Test makes sense — <strong>KINDLY DON’T!</strong></p><p>Define a @Test (expectedException = XY.class ) and take care.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c90a9aa4de6bb5f721dd8fba6f8a8565/href">https://medium.com/media/c90a9aa4de6bb5f721dd8fba6f8a8565/href</a></iframe><p><strong>Point 7</strong>— NEVER keep an `<em>expectedValue</em>` variable global — it doesn’t give any Performance benefit! The only reason to validate it’s use would be to say “<em>I want to Assert it in @AfterMethod</em>” — then see point 8</p><p><strong>Point 8</strong>— Keep assert Statements in @Test locally— it will always be more readable to a toddler.</p><p>So @AfterMethod shouldn’t be used to assert — hence validating Point 1 and Point 7.</p><blockquote>If you think many asserts statements are similar — create a private method — and put asserts there — by doing this you are not breaking point 1.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b168e5b278e99aa67103a006e8c47c02/href">https://medium.com/media/b168e5b278e99aa67103a006e8c47c02/href</a></iframe><p><strong>Point 9</strong>— Create Test Classes with naming convention <strong>`<em>XXXTest.java</em>` </strong>and not TestXXX.java — you will be saving someone a lot of effort — even you sometimes.</p><p><strong>Point 10</strong>— If manual data is too much — make a separate class — YES — that’s legal. Then the convention based on Point 9— <strong><em>`XXXTestData.java</em></strong>` should be used.</p><p><em>Now test you understanding on this snippet — which clearly would be fine (</em><strong><em>but only for that developer</em></strong><em>)</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*_Yih0fTZgDr1rIn7V8n9DQ.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=32d68c08f6f8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Format your Stuff with Java]]></title>
            <link>https://saurabhk15.medium.com/format-using-java-2f0fc8dfd187?source=rss-a199039e4fea------2</link>
            <guid isPermaLink="false">https://medium.com/p/2f0fc8dfd187</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[date]]></category>
            <category><![CDATA[java8]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[currency]]></category>
            <dc:creator><![CDATA[Saurabh Kumar]]></dc:creator>
            <pubDate>Sat, 28 Oct 2017 17:02:10 GMT</pubDate>
            <atom:updated>2017-10-28T17:05:48.532Z</atom:updated>
            <content:encoded><![CDATA[<p>I had a requirement to show the amount in my application as currency, more specifically I needed a more ROBUST approach than this</p><p>System.out.println(&quot;Rs. &quot;, myApp.getAmount());</p><p>So, whats wrong with this — I can say that when i need to change the currency I would need to replace this fixed “<strong>Rs.</strong>” in every place but then we could extract it as constant, right ?</p><p>Then what about if I need to also support other currencies ? Say “<strong><em>$, #…. etc</em></strong>”. Hence I went on a search and found this brilliant Singleton Implementations in Java → <strong>java.text</strong> package → the <strong>Format</strong> interface</p><p>I would be discussing three most common aspects of formatting here:</p><ol><li>Formatting numbers</li><li>Formatting String Messages</li><li>Formatting Dates</li></ol><h4>1. Formatting numbers with NumberFormat class</h4><p>This is very simple, basically you can have localized values and settings for how you want to see your number, mainly I know the value of <strong>PI</strong> in general sense is 3.14 — but in <strong><em>Austria</em></strong> they have opposite separators, so the same value would look like 3,14 (crazy). So read below for more</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a99a3614c84cf7dea3ff580593d954fe/href">https://medium.com/media/a99a3614c84cf7dea3ff580593d954fe/href</a></iframe><h4>2. Formatting Strings Messages with MessageFormat class</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c8c98ad6d7cb41bb6f7ac48694b624b7/href">https://medium.com/media/c8c98ad6d7cb41bb6f7ac48694b624b7/href</a></iframe><p>The special things about this class is that it supports</p><ol><li>type formatters: <strong>“number”</strong>,<strong>“date”</strong>,<strong>“time”</strong></li><li>number formatters: <strong>“currency”</strong>,<strong>“percent”</strong>,<strong>“integer”</strong></li><li>date-time formatters: <strong>“short”</strong>,<strong>“medium”</strong>,<strong>“long”</strong>,<strong>“full”</strong></li></ol><h4>3. The Date Time formatting</h4><p>With java 8 we have moved from using java.util.Date to using <strong>LocalDateTime</strong> classes or simply Date-Time API, so the <strong>SimpleDateFormat</strong> class is somewhat discouraged from using since it was primarily used to format Date().</p><p>But counting it too — we have three approaches to format dates</p><ol><li>SimpleDateFormat () // format + parse</li><li>DateFormat.getInstace() // format + parse</li><li>DateTimeFormatter() //format , Java8</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98d8dbf0b6253713b136600bd97005d3/href">https://medium.com/media/98d8dbf0b6253713b136600bd97005d3/href</a></iframe><p>additionaly DateTimeFormatter has the following formats</p><pre>BASIC_ISO_DATE<br><br>ISO_LOCAL_DATE<br>ISO_LOCAL_TIME<br>ISO_LOCAL_DATE_TIME<br><br>ISO_OFFSET_DATE<br>ISO_OFFSET_TIME<br>ISO_OFFSET_DATE_TIME<br><br>ISO_ZONED_DATE_TIME<br><br>ISO_INSTANT<br><br>ISO_DATE<br>ISO_TIME<br>ISO_DATE_TIME<br><br>ISO_ORDINAL_TIME<br>ISO_WEEK_DATE<br><br>RFC_1123_DATE_TIME</pre><p>Now play along and see for yourself , where this knowledge will help you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2f0fc8dfd187" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Testing in Java with JUnit 5 Good Parts]]></title>
            <link>https://saurabhk15.medium.com/testing-in-java-with-junit-5-good-parts-31ccad53415d?source=rss-a199039e4fea------2</link>
            <guid isPermaLink="false">https://medium.com/p/31ccad53415d</guid>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[junit]]></category>
            <category><![CDATA[java9]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Saurabh Kumar]]></dc:creator>
            <pubDate>Sat, 21 Oct 2017 14:58:13 GMT</pubDate>
            <atom:updated>2017-10-21T14:58:13.739Z</atom:updated>
            <content:encoded><![CDATA[<p>Frankly I was reluctant about the concept of TDD (Test Driven Development), but since then now i only see the benefits of designing and reaching the test coverage of &gt; 70% .</p><p>My journey began with JUnit 4.12 (and then Testing was merely a concept that included ASSERT THAT something should return something expected.</p><p>But that was the old times — now in JUnit 5 - <em>Assert.assertThat() </em>is gone, and is replaced by much simpler and readable <em>Assertions.assertEquals()</em></p><h4>So typical JUnit5 code looks like this</h4><p>Assertions.assertEquals(expected, <em>actual-result-from-your-method-call</em>);</p><p>and that’s it. If you are new to Testing this is enough for you, go be creative, comeback later. Trust me you might think this is just a statement but NO.</p><p>Think about this — now every method you write that returned a Fibonacci number of a result of factorial, etc. will be tested by this small one line. (<em>Obviously you already did that with System.out.println() but now you won’t miss any mistakes since it will scream all over the console with red lines if you did)</em>.</p><p>If you are comfortable, get this there is <strong>Optional ARGUMENT</strong> in every Assertions :)</p><p>Well not so shocking — make sure you use the latest version of JUnit which is JUnit 5 (Jupiter) since the old one didn’t use awesome things that got improved in Java over the years. For Ex.</p><p>Assertions.assertEquals(expected, actual_result, &quot;message if error occurs&quot;);</p><blockquote>Best Practice is — Show in your words what you were expecting. Because months later if you yourself are reading it — it should make sense ;)</blockquote><p>If you know how to write <strong>lambda</strong>’s you can mock a lazy initialization instead of simply passing a String Message by</p><p>Assertions.assertEquals(a, b, () -&gt; “message is lazily loaded”);</p><h4>This message / lambda message (message supplier) is present in every Assertions.assertXXX.</h4><p>Now that this is done the question is what else if there in JUnit :</p><h3>1. More Assertions</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ac269cebdd221eea8fc8bb3ecf6c8c0/href">https://medium.com/media/5ac269cebdd221eea8fc8bb3ecf6c8c0/href</a></iframe><p>If this is clear enough, lets make it more interesting</p><h3>2. Assumptions</h3><p>How about saying I expect that my result will be different on mac so I <strong><em>assume</em></strong> that we are currently on mac and then run my test ?</p><p>If you think about it if you are not on Mac is there any point to run that test ! No.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e880a796f529ad92da8197a92ef188fe/href">https://medium.com/media/e880a796f529ad92da8197a92ef188fe/href</a></iframe><h3>3. Annotations (Only the Cool Ones)</h3><p>JUnit5 believes in annotations so we have some cools ones</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/73a3f03726515b1720b5bcec1526d16f/href">https://medium.com/media/73a3f03726515b1720b5bcec1526d16f/href</a></iframe><p>The last @RepeatedTest will have an output like</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/356/1*H0mgeD72tSxFRwB5FhVlaQ.png" /></figure><p>So, that’s it for now — Do let me know if you learned something new :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=31ccad53415d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>