<?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 Veeramani Ns on Medium]]></title>
        <description><![CDATA[Stories by Veeramani Ns on Medium]]></description>
        <link>https://medium.com/@veeramani.ns?source=rss-b9ec10c8b2b4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Qi1fHvdfnkszrHl9E646Hg.jpeg</url>
            <title>Stories by Veeramani Ns on Medium</title>
            <link>https://medium.com/@veeramani.ns?source=rss-b9ec10c8b2b4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 23:56:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@veeramani.ns/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[JDBC Token Store Vs REDIS Token Store]]></title>
            <link>https://techmusings.optisolbusiness.com/jdbc-token-store-vs-redis-token-store-c921fc6dbb9a?source=rss-b9ec10c8b2b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/c921fc6dbb9a</guid>
            <dc:creator><![CDATA[Veeramani Ns]]></dc:creator>
            <pubDate>Tue, 29 Mar 2022 08:14:13 GMT</pubDate>
            <atom:updated>2022-12-21T09:31:43.759Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Introduction:</strong></p><p><em>Security is essential for the back-end system. From the security, it will evaluate the strength of an application.</em> <strong>Oauth2 </strong><em>is a widely used authorization framework and is supported by Spring. The Spring OAuth 2.0 Authorization mechanism manages and verifies the tokens. These tokens are then used to access the protected resources. So here we focused on the spring security token store compatibility and flexibility. Whenever the user enters our app it would be authenticated and authorized. It is like a wall of communication inside the app.</em></p><p><strong>Spring Security essentials:</strong></p><ul><li><strong>Authentication</strong> <em>refers to the process of verifying the identity of a user, based on provided credentials. A common example is entering a username and a password when you log in to a website. You can think of it as an answer to the question Who are you?</em></li><li><strong>Authorization</strong> <em>refers to the process of determining if a user has proper permission to perform a particular action or read particular data, assuming that the user is successfully authenticated. You can think of it as an answer to the question Can a user do/read this?</em></li><li><strong>The principle</strong> <em>refers to the currently authenticated user. -Subsequent API calls will have an identity belonging to who is doing in code.</em></li><li><strong>Granted authority</strong> <em>refers to the permission of the authenticated user.</em></li><li><strong>Role</strong> <em>refers to a group of permissions of the authenticated user.</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3tSHSU5A0mf5-VWDLvKqVg.png" /></figure><p><em>Let us come to the discussion points which are the pros and cons of the token stores</em></p><p><em>JDBC Token stores take us to configure in spring boot security.</em></p><p><em>Here we are using the Oauth2 concept to store the token based on the above functional diagram.</em></p><p><strong>Setup the JDBC Token Store (code example):</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fnmdPaxkMjXCkWuS5O5lCQ.png" /></figure><p><strong>@EnableAuthorizationServer</strong> —<em> It will enable the authorization server to reach the endpoint which is the associated port and enable web security.</em></p><p><em>In that authorization server, we have to mention the data source-which is having a connection to the DB data source.</em></p><p><em>For using the JDBC token store-It has a default query to insert a pre-defined table.</em></p><p><strong>Example:</strong></p><p><strong>private static final String DEFAULT_ACCESS_TOKEN_INSERT_STATEMENT = “insert into oauth_access_token (token_id, token, authentication_id, user_name, client_id, authentication, refresh_token) values (?, ?, ?, ?, ?, ?, ?)”;</strong></p><p><em>House Keeping level is also there, Delete the token based on the default query in the token store.</em></p><p><strong>private static final String DEFAULT_ACCESS_TOKEN_DELETE_STATEMENT = “delete from oauth_access_token where token_id = ?”;</strong></p><p><em>To add Additional Information we have enhanced the method by using TokenEnhancer</em></p><p><strong>Example:</strong></p><p><strong>additionalInfo.put(“User_id”, us.getUserId());</strong></p><p><strong>additionalInfo.put(“User_type”, us.getUserType());</strong></p><p><em>By above the additional info to identify the user&#39;s types and roles.</em></p><p><em>After that, the token store will retrieve the token from Database if they have generated the token in JDBC token store.</em></p><p><em>But these concepts are handled by the Oauth2 Access Denied Handler also.</em></p><p><em>CustomerDetails Interface is playing a vital role in accessing by username.</em></p><p><em>As a developer, we can reuse this in the manipulation of tokens using a database.</em></p><p><em>But while we doing any DB activities like production index creations and other DB-related activities means that time will lock the databases.</em></p><p><em>And the housekeeping of the token based on the email will be more complicated in JDBC token store.</em></p><p><em>Token validity also takes a major impact on that token. If we are not used to expiration time means. A spammer can take the token and get the details of the app.</em></p><p><em>Even though if we put the logout concept to delete the token means, it will be tougher to achieve in database data wise.</em></p><p><strong><em>Advantages of JDBC Token Store:</em></strong></p><ol><li><em>Easily access the token by default by querying</em></li><li><em>By encrypting the hash, we can store and retrieve the password.</em></li><li><em>Store the keys in DB and retrieve them throughout the app.</em></li><li><em>Making the Refresh Token will be easier.</em></li><li><em>Never expire will be set easily.</em></li><li><em>A default table will be added if we enabled this as a token store.</em></li></ol><p><strong><em>REDIS Token Store:</em></strong></p><p><em>Everything is the same if we are going to use the Oauth2 security token means.</em></p><p><strong>Only the changes we need to apply for the code Setup(code example):</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j8ohX4WvXN-2tpS866PHNQ.png" /></figure><p><strong>Dependency</strong>:</p><p><em>&lt;dependency&gt;</em></p><p><em>&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</em></p><p><em>&lt;artifactId&gt;spring-boot-starter-data-redis&lt;/artifactId&gt;</em></p><p><em>&lt;/dependency&gt;</em></p><p><strong>Properties:</strong></p><p><em>spring.redis.host=localhostspring.redis.password=spring.redis.port=6379</em></p><p><strong>Connection Factory:</strong></p><p><strong>@Bean public TokenStore redisTokenStore() { return new RedisTokenStore(redisConnectionFactory); }</strong></p><p><strong>RedisConnectionFactory redisConnectionFactory</strong>- <em>This will make the major connection of the REDIS client.</em></p><p><em>Using the enhancer to save the Oauth2 token by adding the salt and making the encryption as well add the expiration time.</em></p><p><em>As per the basics of the REDIS, it will be stored as a hash by using a key and value.</em></p><p><em>So, we can store the key as the username -email of the user.</em></p><p><strong><em>EXPIRES_IN -</em></strong><em>based on this value setting the expiration time in milliseconds.</em></p><p><em>So, the housekeeping of the token will be easier.</em></p><p><em>If we use the Enhance method to save the token values in REDIS means, housekeeping will automatically enable while we generate the token.</em></p><p><em>It will support the encryption logic as well-because we will store the values against the key-user email.</em></p><p><strong><em>Map&lt;?, ?&gt; customvalues = (Map&lt;?, ?&gt;) authentication.getDetails();</em></strong></p><p><strong><em>additionalInfo.put(“key_super_key”, customvalues.get(“key_super_key”))</em></strong></p><p><strong><em>Retrieval of key:</em></strong></p><p><strong><em>Map&lt;?, ?&gt; sessioninfo = (Map&lt;?, ?&gt;) redis.redisTemplate().opsForHash().get(</em></strong></p><p><strong><em>authentication.getPrincipal().toString() + user.getRole(),</em></strong></p><p><strong><em>authentication.getPrincipal().toString() + user.getRole());</em></strong></p><p><em>Whenever the app endpoint URL comes in the server, we access the token by using username-email while login in.</em></p><p><em>Once authentication is set means it will add the principal, the principal will take over the authentication to subsequent API calls to determine the token which belongs to whom.</em></p><p><strong><em>Advantages of REDIS Token Store:</em></strong></p><p><strong>1. </strong><em>Easily access the token</em></p><p><strong><em>2</em></strong><em>. Encrypting takes place and needs to decrypt when it is retrieved.</em></p><p><strong><em>3</em></strong><em>. Housekeeping will be more fairylike sessions we are creating for milliseconds.</em></p><p><strong><em>4. </em></strong><em>Never expire also we take place.</em></p><p><strong><em>5</em></strong><em>. Addition of salt to prevent the spammer attack.</em></p><p><strong><em>6</em></strong><em>. Retrieval of the data is faster because it is cached.</em></p><p><strong><em>7</em></strong><em>. Redis is open source but we can make a proxy server means the REDIS server access will be more secure.</em></p><p><strong>Conclusion:</strong></p><p>Both of the token stores have pros and cons. Here I gave the ways of implementing two token stores. So, we will make a design that should be robust with either JDBC or REDIS Token store. Hope this will be useful for Spring Security users!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c921fc6dbb9a" width="1" height="1" alt=""><hr><p><a href="https://techmusings.optisolbusiness.com/jdbc-token-store-vs-redis-token-store-c921fc6dbb9a">JDBC Token Store Vs REDIS Token Store</a> was originally published in <a href="https://techmusings.optisolbusiness.com/">Tech Musings</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>