Skip to content

Instantly share code, notes, and snippets.

@sandipchitale
sandipchitale / Insecure.java
Last active November 27, 2023 02:54
SslBundles #https #ssl #sslbundles
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import io.netty.handler.ssl.util.KeyManagerFactoryWrapper;
import javax.net.ssl.KeyManager;
SslManagerBundle insecureSslManagerBundle = SslManagerBundle.of(
new KeyManagerFactoryWrapper(new KeyManager() {}),
InsecureTrustManagerFactory.INSTANCE);
insecureSslBundle = SslBundle.of(null,
null,
@sandipchitale
sandipchitale / PostProcessor.java
Last active November 19, 2023 11:28
Set portmapper for oauth2Login() #oauth2Login #portMapper
ObjectPostProcessor<AuthenticationEntryPoint> authenticationEntryPointFilterPostProcessor = new ObjectPostProcessor<>() {
@Override
public <O extends AuthenticationEntryPoint> O postProcess(O authenticationEntryPoint) {
if (authenticationEntryPoint instanceof DelegatingAuthenticationEntryPoint delegatingAuthenticationEntryPoint) {
Field entryPointsField = ReflectionUtils.findField(DelegatingAuthenticationEntryPoint.class, "entryPoints");
assert entryPointsField != null;
entryPointsField.setAccessible(true);
LinkedHashMap<RequestMatcher, AuthenticationEntryPoint> entryPoints =
(LinkedHashMap<RequestMatcher, AuthenticationEntryPoint>) ReflectionUtils.getField(entryPointsField,
delegatingAuthenticationEntryPoint);
@sandipchitale
sandipchitale / application.properties
Created November 13, 2023 20:27
Hidden Spring HandlerMapping logger #logging
logging.level._org.springframework.web.servlet.HandlerMapping=DEBUG
@sandipchitale
sandipchitale / ErrorHandling.java
Created November 10, 2023 05:15
ErrorHandling #spring-web-mvc
@ExceptionHandler(Exception.class)
public ResponseEntity<String> handleError(HttpServletRequest req, Exception ex) {
if (ex.getCause() instanceof SocketTimeoutException) {
return ResponseEntity.status(HttpStatus.GATEWAY_TIMEOUT).body(HttpStatus.GATEWAY_TIMEOUT.getReasonPhrase());
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
}
@sandipchitale
sandipchitale / wraprequest.java
Last active October 29, 2023 22:31
Where request dispatcher creates forward request #tomcat #requestdispatcher
org.apache.catalina.core.ApplicationDispatcher#wrapRequest
@sandipchitale
sandipchitale / HttpHeaderModificationConfig.java
Last active October 18, 2023 01:49
:authority: header handling in HTTP2 #http2
package sandipchitale.twowaytlsclient.twowaytlsclient;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
@sandipchitale
sandipchitale / get_completions.bash
Created September 9, 2023 22:47
Get completions #bash
#
# Author: Brian Beffa <brbsix@gmail.com>
# Original source: https://brbsix.github.io/2015/11/29/accessing-tab-completion-programmatically-in-bash/
# License: LGPLv3 (http://www.gnu.org/licenses/lgpl-3.0.txt)
#
get_completions(){
local completion COMP_CWORD COMP_LINE COMP_POINT COMP_WORDS COMPREPLY=()
# load bash-completion if necessary
@sandipchitale
sandipchitale / PreConfigDataEnvironmentPostProcessor.java
Last active August 29, 2023 03:03
Set profile based on other profile #springboot #profile
static class PreConfigDataEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
PropertiesPropertySourceLoader propertiesPropertySourceLoader = new PropertiesPropertySourceLoader();
try {
List<PropertySource<?>> propertySources = propertiesPropertySourceLoader.load("Config resource 'class path resource [application.properties]' via location 'optional:classpath:/'",
new ClassPathResource("/application.properties"));
propertySources.stream().forEach((PropertySource<?> propertySource) -> {
Object springProfilesActive = propertySource.getProperty("spring.profiles.active");
if (springProfilesActive instanceof String springProfilesActiveString) {
@sandipchitale
sandipchitale / HSPFRC.java
Last active August 27, 2023 21:02
Live Templates #intellij
record $MODEL$(long id, String name) {}
@Bean
@Qualifier("$DCMODEL$-rest-client")
RestClient $DCMODEL$RestClient(RestClient.Builder builder) {
return builder.baseUrl("https://$DCMODEL$.api.com/$MODEL$s").build();
}
interface $MODEL$Service {
@GetExchange
@sandipchitale
sandipchitale / WebclientPassthruApplication.java
Last active August 21, 2023 06:42
Webclient passthru #webflux #webclient #springboot
package sandipchitale.webclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.reactive.ClientHttpRequest;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;