Skip to content

Instantly share code, notes, and snippets.

@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 <[email protected]>
# 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.*;
@sandipchitale
sandipchitale / body.java
Last active August 19, 2023 21:23
WebClient body #webclient
body(BodyInserters.fromResource(new InputStreamResource(inputStream)))
@sandipchitale
sandipchitale / websecuritycustomizerbean.java
Last active August 16, 2023 07:16
Ignore resource #spring-security
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring()
.requestMatchers(
// To avoid wrapping in MvcMatcher make sure to use AntPathRequestMatcher
AntPathRequestMatcher.antMatcher("/index.html"),
AntPathRequestMatcher.antMatcher("/**/*.css")
);
}
@sandipchitale
sandipchitale / as.java
Last active August 15, 2023 06:20
OAuth2 AS use shared secret key encoded JWT #springboot-oauth2-as-jwt
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity httpSecurity,
OAuth2TokenGenerator<?> tokenGenerator,
RegisteredClientRepository registeredClientRepository) throws Exception {
OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(httpSecurity);
OAuth2AuthorizationServerConfigurer oAuth2AuthorizationServerConfigurer =
httpSecurity.getConfigurer(OAuth2AuthorizationServerConfigurer.class);
oAuth2AuthorizationServerConfigurer.registeredClientRepository(registeredClientRepository);
@sandipchitale
sandipchitale / DPEPP.java
Last active August 13, 2023 02:40
Dynamicall add a property source to inject additional property based on another property #springboot
@Order(Ordered.LOWEST_PRECEDENCE)
public static class OAuth2ClientPropertiesSchemeAdjusterEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
String asScheme = "http";
if ("true".equals(environment.getProperty("server.ssl.enabled"))) {
asScheme = "https";
}
@sandipchitale
sandipchitale / CPBPP.java
Last active August 13, 2023 02:37
Sample BeanProcessor to modify a configuration property bean #springboot
@Component
public class OAuth2ClientPropertiesSchemeAdjusterBeanPostProcessor implements BeanPostProcessor, EnvironmentAware {
private Environment environment;
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof OAuth2ClientProperties oAuth2ClientProperties) {
String tokenUri = oAuth2ClientProperties.getProvider().get("as").getTokenUri();
if ("true".equals(environment.getProperty("server.ssl.enabled"))) {