Skip to content

Instantly share code, notes, and snippets.

View piyusht007's full-sized avatar
🏠
Working from home

Piyush Tiwari piyusht007

🏠
Working from home
View GitHub Profile
@piyusht007
piyusht007 / AuthenticationSuccessHandlerImpl.java
Last active July 6, 2021 05:36
Add JSESSIONID cookie with SameSite mode as Strict from Spring's AuthenticationSuccessHandler
package x.y.z;
import lombok.extern.log4j.Log4j2;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@piyusht007
piyusht007 / shell_cheatsheet.sh
Created May 17, 2021 06:27
Shell scripting cheatsheet
Date arithematic in Shell:
# Assign a specific date to variable
start=`date -d 2021-03-22`
# Add 1 day to the start
date -d "$start 1 days" # 2021-04-01
# Subtract 1 day from the start
date -d "$start -1 days" # 2021-03-29
@piyusht007
piyusht007 / vpn_fix.sh
Created May 15, 2021 14:28
WSL VPN Fix
# Add below function to the ~/.bashrc file to make it available globally
vpn_fix () {
# Get the DNS servers from windows and add then to the /etc/resolv.conf
powershell.exe -Command 'Get-DnsClientServerAddress -AddressFamily ipv4 | Select-Object -ExpandProperty ServerAddresses' | awk 'BEGIN { print "# Generated by vpn fix func on", strftime("%c"); print } { print "nameserver", $1 } END { print "search", "nuance.com" }' | tr -d '\r' | tee /etc/resolv.conf
echo "/etc/resolv.conf re-written!!"
}
@piyusht007
piyusht007 / MyApplicationTest.java
Last active November 11, 2020 06:40
Spring bean lifecycle - A Spring bean that tap into various lifecycle extension points
package org.piyush;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class MyApplicationTest {
@Test
public void contextLoads() {
@piyusht007
piyusht007 / date_utils.py
Last active September 21, 2020 10:10
Date and time in Python
import datetime
from datetime import timezone
# Get timezone unaware current date time
print(datetime.datetime.now()) # 2020-09-18 22:17:52.878325
# Get timezone unaware current date time in UTC
print(datetime.datetime.utcnow()) # 2020-09-18 16:48:17.822981
# Preferred way is to use 'now' and pass it a timezone
print(datetime.datetime.now(timezone.utc)) # 2020-09-18 16:48:17.822981
@piyusht007
piyusht007 / sns.py
Created July 14, 2020 17:27
Amazon SNS - Publish message to a topic using python boto3
import boto3
client = boto3.client('sns')
topics = client.list_topics()
print(topics)
topicArn = topics['Topics'][0]['TopicArn']
@piyusht007
piyusht007 / db_queries.txt
Last active November 15, 2020 11:31
Database Queries
# Nth highest salary - [Generic]
SELECT * from employee e1 where N-1 = (SELECT COUNT(DISTINCT(salary)) from employee e2 where e2.salary > e1.salary);
# Ex: To get 2nd highest, N = 2
SELECT * from employee e1 where 1 = (SELECT COUNT(DISTINCT(salary)) from employee e2 where e2.salary > e1.salary);
# Nth highest salary - [PostgreSQL] - Faster
SELECT DISTINCT(salary) from employee order by salary desc limit 1 offset N-1;
# Ex: To get 2nd highest, N = 2
SELECT DISTINCT(salary) from employee order by salary desc limit 1 offset 1;
@piyusht007
piyusht007 / CFExample.java
Last active August 9, 2020 08:16
CompletableFuture to divide the task and accumulate the results
package com.tomtom.postal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
@piyusht007
piyusht007 / test.html
Created June 11, 2020 10:32
Role checking Cheatsheet in Spring security and Thymeleaf
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<div>
<b>Username:</b>
<div sec:authentication="name">
The value of the "name" property of the authentication object should appear here.
</div>
</div>
<div>
<b>User Roles: </b>
@piyusht007
piyusht007 / cheatsheet.txt
Last active March 23, 2020 13:48
AWS Cheatsheet
Using AWS Route 53 DNS:
1. As active and passive failover setup: Make 2 record sets with the same domain name and set one as primary and the other as secondary under Routing Policy Type "Failover".
2. As round-robin DNS setup: Make 2 record sets with same domain name and set weight to 50 percent on both under Routing Policy Type "Weighted".