<?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 Ahmad Hafid on Medium]]></title>
        <description><![CDATA[Stories by Ahmad Hafid on Medium]]></description>
        <link>https://medium.com/@ocinz?source=rss-c2f372429193------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Cipt6IM9zAY_3hW1iafmGQ.png</url>
            <title>Stories by Ahmad Hafid on Medium</title>
            <link>https://medium.com/@ocinz?source=rss-c2f372429193------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 18:09:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ocinz/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[Why You Need to Understand Vectors]]></title>
            <link>https://ocinz.medium.com/why-you-need-to-understand-vectors-8eefe5750df4?source=rss-c2f372429193------2</link>
            <guid isPermaLink="false">https://medium.com/p/8eefe5750df4</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[vector]]></category>
            <category><![CDATA[math]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Ahmad Hafid]]></dc:creator>
            <pubDate>Sat, 15 Aug 2026 13:14:12 GMT</pubDate>
            <atom:updated>2026-08-15T13:14:12.500Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi guys, I’m currently relearning everything from scratch to become a machine learning engineer, and I’m trying to add some meaning to every aspect so I can remember and use every technique wisely.</p><p>So, why a vector?</p><p>Well, the answer is simple: our computer doesn’t know how to process feelings or what to do with real-world attributes. So, engineers try to convert every real-world attribute into numbers so that they can perform mathematical operations to solve problems. Math is what our computers are good at.</p><p>For example, a real-world attribute that can be vectorized — you might have heard about the Iris dataset. The species (setosa, versicolor, virginica) are usually converted into numbers as the target.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*BL61ViL-z7lpSrLG7uetRQ.png" /></figure><p>So, at this point, numbers are not the same anymore. They might not hold hierarchical value anymore; they might just represent some class in this case.</p><p>That’s it for now, see you later :))</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8eefe5750df4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to generate and extract claim JWT in Spring Boot]]></title>
            <link>https://ocinz.medium.com/how-to-generate-and-extract-claim-jwt-in-spring-boot-77573bff0752?source=rss-c2f372429193------2</link>
            <guid isPermaLink="false">https://medium.com/p/77573bff0752</guid>
            <dc:creator><![CDATA[Ahmad Hafid]]></dc:creator>
            <pubDate>Tue, 27 Feb 2024 11:41:31 GMT</pubDate>
            <atom:updated>2024-02-27T11:43:07.785Z</atom:updated>
            <content:encoded><![CDATA[<p>As the title suggests, this article will explain how to generate and extract JSON Web token in Spring Boot so you can experiment with other implementations. We will try to generate and extract the token via API. This will be our agenda:</p><ol><li><strong>What is Json Web Token?</strong></li><li><strong>What is the JSON Web Token structure?</strong></li><li><strong>Preparing a spring boot project</strong></li><li><strong>Implementing JWT Utility</strong></li><li><strong>API for generating and extracting the token</strong></li><li><strong>W hat is a JSON Web Token?</strong></li></ol><p>JSON Web Token, or simply JWT, is a famous token that works with application security. It is a token that defines a compact and self-contained way of securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm).</p><p>This is the most common scenario for using JWT. Once the user is authenticated, the server will give us JWT so that each subsequent request will include the JWT, allowing the user to access routes, services, and resources that are permitted with that token. JWT is known best for its small overhead and its ability to be easily used across different domains.</p><p>Actually, as developers, we don’t really know about the algorithm in detail. What we know is that it is one of many ways to secure your application.</p><p><strong>2. What is the JSON Web Token structure?</strong></p><p>JSON Web Tokens consist of three parts separated by dots, which are:</p><p><strong>Header.Payload.Signature</strong></p><p><strong>Header<br></strong>The header consists of two parts: the type of the token, which is JWT, and the algorithm being used, such as <strong>HMAC SHA256</strong> or <strong>RSA</strong>. For example:</p><p>{ “alg”: “HS256”, “typ”: “JWT” }</p><p><strong>Payload<br></strong>The second part of the token contains the claims. Claims are simply data. There are three types of claims: registered, public, and private claims.</p><p>Registered claims: These are a set of predefined claims which are not mandatory but recommended. Some of them are: iss (issuer), exp (expiration time), sub (subject), aud (audience), and others.</p><p>Public claims: These can be defined at will by those using JWTs. But to avoid collisions they should be defined in the IANA JSON Web Token Registry or be defined as a URI that contains a collision resistant namespace.</p><p>Private claims: These are the custom claims created to share information between parties that agree on using them and are neither registered or public claims. An example payload could be:</p><p>{ “id”: “1234567890”, “name”: “John Doe”, “admin”: true }</p><p>We don’t put secret information in the payload or header elements of a JWT because it is readable by anyone unless it is encrypted.</p><p><strong>Signature</strong></p><p>To create the signature part you have to take the encoded header, the encoded payload, a secret, the algorithm specified in the header, and sign that.</p><p>For example if you want to use the HMAC SHA256 algorithm, the signature will be created in the following way:</p><p><strong>HMACSHA256(<br>base64UrlEncode(header) + “.” + base64UrlEncode( payload ), secret )</strong></p><p>The signature is used to verify the message wasn’t changed along the way, and, in the case of tokens signed with a private key, it can also verify that the sender of the JWT is who it says it is.</p><p><strong>Conclusion (I have to say that you should jump here from the start :D)</strong></p><p>The output is three Base64-URL strings separated by dots that can be easily passed in HTML and HTTP environments, while being more compact when compared to XML-based standards such as SAML.</p><p><strong>3. Preparing a spring boot project</strong></p><p>For starting the spring boot application, you can go to spring.initializr.io and select:</p><ul><li>Project: maven</li><li>Language: java</li><li>Select the latest version (not snapshot and M1).</li><li>Packaging: jar</li><li>Java 17 or 21 is up to you.</li><li>Add dependency: spring web and lombok</li></ul><p>then it should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0BYybEE1hXFhFxu9Qx9gJg.png" /></figure><p>Once everything is done, click generate to download. extract the zip file and open it in your IDE (mine is Intellij Idea Community Edition). We need to add JWT dependency to our project, go to the Maven repository, and go for JJWT-api, JJWT-impl, and JJWT-jackson.</p><p>or you can simply add the following codes to your pom.xml file:</p><pre>&lt;!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt-api --&gt;<br>&lt;dependency&gt;<br>   &lt;groupId&gt;io.jsonwebtoken&lt;/groupId&gt;<br>   &lt;artifactId&gt;jjwt-api&lt;/artifactId&gt;<br>   &lt;version&gt;0.12.5&lt;/version&gt;<br>&lt;/dependency&gt;<br>&lt;!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt-impl --&gt;<br>&lt;dependency&gt;<br>   &lt;groupId&gt;io.jsonwebtoken&lt;/groupId&gt;<br>   &lt;artifactId&gt;jjwt-impl&lt;/artifactId&gt;<br>   &lt;version&gt;0.12.5&lt;/version&gt;<br>   &lt;scope&gt;runtime&lt;/scope&gt;<br>&lt;/dependency&gt;<br>&lt;!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt-jackson --&gt;<br>&lt;dependency&gt;<br>   &lt;groupId&gt;io.jsonwebtoken&lt;/groupId&gt;<br>   &lt;artifactId&gt;jjwt-jackson&lt;/artifactId&gt;<br>   &lt;version&gt;0.12.5&lt;/version&gt;<br>   &lt;scope&gt;runtime&lt;/scope&gt;<br>&lt;/dependency&gt;</pre><p>Once done, we are ready to implement the JWT utility.</p><p><strong>4. Implementing JWT Utility<br></strong>Make a class named <strong>JwtUtil, </strong>the class would look like this:</p><pre>package com.example.demo.service;<br><br><br>import io.jsonwebtoken.Claims;<br>import io.jsonwebtoken.ExpiredJwtException;<br>import io.jsonwebtoken.Jwts;<br>import io.jsonwebtoken.security.Keys;<br>import lombok.RequiredArgsConstructor;<br>import org.springframework.security.core.userdetails.UserDetails;<br>import org.springframework.stereotype.Service;<br><br><br>import javax.crypto.SecretKey;<br>import java.util.Date;<br>import java.util.function.Function;<br><br><br>@Service<br>@RequiredArgsConstructor<br>public class JwtUtil {<br>   private static final String SECRET = &quot;31ab4082a1fe8c1423395e658176fb45cdf7f697017fb739b6cca33c40d6045e&quot;; <br>   private static final long EXPIRATION_TIME = 1000 * 60;<br>   private static final SecretKey SIGNING_KEY = Keys.hmacShaKeyFor(SECRET.getBytes()); <br>   public String generateToken(String username) {<br>       return Jwts.builder()<br>               .subject(username)<br>               .issuedAt(new Date(System.currentTimeMillis()))<br>               .expiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME))<br>               .signWith(SIGNING_KEY)<br>               .compact();<br>   }<br>   public Claims extractClaim(String token) throws ExpiredJwtException{<br>       try {<br>           return Jwts.parser()<br>                   .verifyWith(SIGNING_KEY)<br>                   .build()<br>                   .parseSignedClaims(token)<br>                   .getPayload();<br>       } catch (ExpiredJwtException e){<br>           return e.getClaims();<br>       }<br>   }<br>   public &lt;T&gt; T getClaimFromToken(String token, Function&lt;Claims, T&gt; claimsResolver) {<br>       final Claims claims = extractClaim(token);<br>       return claimsResolver.apply(claims);<br>   }<br>   public String extractUsername(String token) {<br>       return getClaimFromToken(token, Claims::getSubject);<br>   }<br>   public boolean isTokenExpired(String token){<br>       return getClaimFromToken(token, Claims::getExpiration).before(new Date(System.currentTimeMillis()));<br>   }<br>   public boolean tokenExpired(String token) {<br>       return getClaimFromToken(token, Claims::getExpiration).before(new Date());<br>   }<br>}</pre><ul><li>Of course, you want to store the <strong>SECRET </strong>in the application.properties file. Make sure to change it with a 256-bit string of random keys.</li><li>You can adjust the token expiration time by changing <strong>EXPIRATION_TIME,</strong> which is set for 1 minute.</li><li>You need to wrap the <strong>extractClaim</strong> method in the try-catch block. This is because if the token is expired, by default, JJWT will automatically return an <strong>ExpiredJwtException</strong> and cause a white label error, which means we cannot extract the claim.</li></ul><p><strong>5.</strong> <strong>API for generating and extracting the token<br></strong>We are expected to make APIs to generate the token, extract the subject, check whether the token is still valid or not, and refresh the token. Make a class named JwtController:</p><pre>package com.example.demo.controller;<br><br><br>import com.example.demo.service.JwtUtil;<br>import lombok.RequiredArgsConstructor;<br>import org.springframework.web.bind.annotation.*;<br><br><br>@RestController<br>@RequestMapping(&quot;/jwt&quot;)<br>@RequiredArgsConstructor<br>public class JwtController {<br>   private final JwtUtil jwtUtil;<br><br><br>   @GetMapping(&quot;/generate/{username}&quot;)<br>   public String generate(<br>           @PathVariable(&quot;username&quot;) String username<br>   ){<br>       return jwtUtil.generateToken(username);<br>   }<br>   @GetMapping(&quot;/refresh/{token}&quot;)<br>   public String refresh(<br>           @PathVariable(&quot;token&quot;) String token<br>   ){<br>       String username = jwtUtil.extractUsername(token);<br>       return jwtUtil.generateToken(username);<br>   }<br>   @GetMapping(&quot;/extract/{token}&quot;)<br>   public String extract(<br>           @PathVariable(&quot;token&quot;) String token<br>   ){<br>       return jwtUtil.tokenExpired(token) ? &quot;Token is expired&quot; : jwtUtil.extractUsername(token);<br>   }<br>   @GetMapping(&quot;/is-valid/{token}&quot;)<br>   public String isvalid(<br>           @PathVariable(&quot;token&quot;) String token<br>   ){<br>       return jwtUtil.tokenExpired(token) ? &quot;Token is expired&quot; : &quot;Token is valid&quot;;<br>   }<br>}</pre><p>We made 4 APIs:</p><ul><li>/generate/{username}, this API will generate the token with username as the subject.</li><li>/refresh/{token}, this API will re-generate a token if our token has expired.</li><li>/extract/{token}, this API will return the string of our username.</li><li>/is-valid/{token}, this API will return “Token is expired” if the token has been expired and “Token is valid” if the token has not expired yet.</li></ul><p>For authentication with Spring Security, there are several ways to implement it, but mostly with some kind of filter, either setting it manually from the application order or through the HttpSecurity addFilterBefore method.</p><p>Hopefully this article will help other junior developers, as Spring Boot is hard to learn. See you in the next article :D</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=77573bff0752" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simple Express API Tutorial for Total Beginner]]></title>
            <link>https://ocinz.medium.com/simple-express-api-tutorial-for-total-beginner-2bc4ad4fe70b?source=rss-c2f372429193------2</link>
            <guid isPermaLink="false">https://medium.com/p/2bc4ad4fe70b</guid>
            <category><![CDATA[express]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[web-developement]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Ahmad Hafid]]></dc:creator>
            <pubDate>Sun, 19 Nov 2023 14:00:06 GMT</pubDate>
            <atom:updated>2023-11-19T14:05:23.417Z</atom:updated>
            <content:encoded><![CDATA[<p>As a beginner in web development it can get hard reading documentation. Watching videos on youtube can take long time. This is for you who seek fast and simple in API development learning using Express JS.</p><p>This is a quick and easy tutorial on creating Rest API with express JS, a specific practical tutorial.</p><h4>Requirements are needed</h4><ol><li>Node JS installed (get the latest LTS version)</li><li>Basic understanding of java script such as creating variables and functions.</li><li>Code editor (I recommend Visual Studio Code)</li><li>Postman/Rest Client (to test the API)</li></ol><h4>Initiate Project and Serving</h4><p>Right away, create a folder and open it in Visual Studio Code. Click</p><blockquote>ctrl + shift + `</blockquote><p>(backtick, should be under the esc key) simultaneously to open the terminal. Type the following code in terminal to initiate the project:</p><blockquote>npm init -y</blockquote><h4>Install Dependencies</h4><p>Install Express and other required dependencies</p><blockquote>npm install express</blockquote><p>Install nodemon to run the application:</p><blockquote>npm install nodemon — save-dev</blockquote><h4>Switch from Common JS to ES6</h4><p>when done, you should get 2 files, package.json and package-lock.json. Open the package.json file and add:</p><blockquote>“type”: “module”</blockquote><p>this is to enable import in JavaScript files(ES6).</p><h4>Create Index.js main file</h4><p>At the same level as package.json, create an index.js file. We will start writing our JavaScript code in the main file connected to package.json, index.js. In the index.js file import express like this:</p><blockquote>import express from “express”;</blockquote><p>Create a variable to call express as a function:</p><blockquote>const app = express();</blockquote><p>Add middleware to parse incoming requests:</p><blockquote>app.use(express().json())</blockquote><p>Create an application to listen on a specific port and provide a callback function to know our application is running:</p><blockquote>const PORT = 3000<br>app.listen(3000, ()=&gt;{<br> console.log(`Server running on port ${PORT}`)<br>})</blockquote><p>The preparation is complete and you should have the following index file:</p><blockquote>import express from “express”;<br>const app = express();<br>app.use(express().json());<br>const PORT = 3000<br>app.listen(PORT, ()=&gt;{<br> console.log(`Server running on port ${PORT}`)<br>})</blockquote><h4>Run App with Nodemon</h4><p>You can run the application by typing in the terminal:</p><blockquote>nodemon index.js</blockquote><p>After running, you should get the message “Server running on port 3000” in the terminal.</p><h4>Define REST API</h4><p>It’s time to write your API program. to create an API in Express JS just type:</p><blockquote>app.get(“/”, (req, res)=&gt;{<br> res.json(“Hello world”);<br>});</blockquote><p>Express supports all http methods (GET, POST, PUT, PATCH, DELETE, etc). the code above uses the Http method Get. to use other Http methods just call like this:</p><blockquote>app.post() //for POST method<br>app.patch() //for patch methods etc.</blockquote><p>These methods can accept parameters in the form of route paths and infinite callbacks. consider the following code:</p><blockquote>app.get(“/”, (req, res)=&gt;{<br>res.json(“Hello world”);<br>});</blockquote><h4>Express Callback</h4><p>The first parameter is the route path and the next is the callback. the callback can only accept up to 4 parameters here are the rules:</p><ul><li>2 parameters: request and response</li></ul><blockquote>(req, res)=&gt;{}</blockquote><ul><li>3 parameters: request, response and next</li></ul><blockquote>(req, res, next)=&gt;{}</blockquote><ul><li>4 parameters: error, request, response and next</li></ul><blockquote>(error, req, res, next)=&gt;{}</blockquote><h4>Express Request Callback Parameter</h4><p>Request parameter has 3 properties: body, params and query example route with request params:</p><blockquote>app.get(“/user/:id”, (req, res)=&gt;{<br>const id = req.params.id;<br>res.json(`User id is ${id}`)<br>})</blockquote><p>Example with request body by sending JSON with field name:</p><blockquote>app.post(“/user”, (req, res)=&gt; {<br>const body = req.body;<br>res.json(body.name);<br>})</blockquote><p>or</p><blockquote>app.post(“/user”, (req, res)=&gt; {<br>const {name} = req.body;<br>res.json(name);<br>})</blockquote><p>example with request query name:</p><blockquote>app.post(“/user”, (req, res)=&gt; {<br>const query = req.query;<br>res.json(query.name);<br>})</blockquote><p>Next, you can run the application by typing in the terminal:</p><blockquote>nodemon index.js</blockquote><p>and then test the API in Postman or Rest Client.</p><h4>Conclusion</h4><p>Hope you find it easy to understand, next i’ll try to explain callback params in detail.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2bc4ad4fe70b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>