Skip to content

Instantly share code, notes, and snippets.

View krnbr's full-sized avatar
🏠

Karanbir Singh krnbr

🏠
  • Home
  • India
View GitHub Profile
@krnbr
krnbr / pom.xml
Last active July 23, 2020 06:37
Spring Boot Webflux Open API 3 Swagger Specs pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath />
<!-- lookup parent from repository -->
</parent>
<groupId>in.neuw</groupId>
@Bean
fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain? {
val httpClient = HttpClient.create()
.tcpConfiguration{client -> client.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)}
.secure { sslContextSpec: SslProvider.SslContextSpec -> sslContextSpec.sslContext(sslContextBuilder(keyStoreContent, keyStorePassword, trustStoreContent, trustStorePassword)) }
val httpConnector: ClientHttpConnector = ReactorClientHttpConnector(httpClient)
val builder = NimbusReactiveJwtDecoder
.withJwkSetUri("https://<host>/.well-known/jwks.json")
// pass the base 64 encoded String of the Keystore and keystore password
KeyManagerFactory keyManagerFactory = SSLContextHelper.getKeyStore(encodedKeystoreString, keystorePassword);
// pass the base 64 encoded String of the Truststore and truststore password
TrustManagerFactory trustManagerFactory = SSLContextHelper.getTrustStore(encodedTruststoreString, truststorePassword);
// Construct the SslContext using keyManagerFactory & trustManagerFactory
SslContext sslContext = SSLContextHelper.sslContext(keyManagerFactory, trustManagerFactory);
HttpClient resourceServerHttpClient = HttpClient.create()
.tcpConfiguration(client -> client.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000))
.secure(sslContextSpec -> {
@krnbr
krnbr / application-mtls.properties
Created July 19, 2020 09:52
mutual TLS based properties
server.port=8533
spring.security.oauth2.client.registration.local.authorization-grant-type=client_credentials
spring.security.oauth2.client.registration.local.client-id=client_id
spring.security.oauth2.client.registration.local.client-secret=client_secret
oauth2.client.provider.local.token-uri.base-path=https://localhost:8353
spring.security.oauth2.client.provider.local.token-uri=${oauth2.client.provider.local.token-uri.base-path}/oauth/token
oauth2.client.registration.local.ssl-enabled=true
\--oauth2-spring-boot-client - Root
| .gitignore - Git management for ignoring not required files
| pom.xml - Maven pom.xml
+---src
| +---main
| | +---java
| | | \---in
| | | \---neuw
| | | \---oauth2
| | | | Oauth2SpringBootClientApplication.java - The SpringBootApplication main class.
@krnbr
krnbr / application.properties
Created July 18, 2020 17:34
Oauth2 Client Important Properties
spring.security.oauth2.client.registration.<identifier>.authorization-grant-type=client_credentials
spring.security.oauth2.client.registration.<identifier>.client-id=client_id
spring.security.oauth2.client.registration.<identifier>.client-secret=client_secret
spring.security.oauth2.client.provider.<identifier>.token-uri=http://localhost:8353/oauth/token
@krnbr
krnbr / TestClientConfig.java
Last active July 19, 2020 03:17
Oauth2 Client Configuration
@Configuration
public class TestClientConfig {
@Value("${test.client.base.url}")
private String testClientBaseUrl;
private Logger testWebClientLogger = LoggerFactory.getLogger("TEST_WEB_CLIENT");
/**
* The authorizedClientManager for required by the webClient
\--oauth2-spring-boot-client - Root
| .gitignore - Git management for ignoring not required files
| pom.xml - Maven pom.xml
+---src
| +---main
| | +---java
| | | \---in
| | | \---neuw
| | | \---oauth2
| | | | Oauth2SpringBootClientApplication.java - The SpringBootApplication main class.
public class JKStoBase64String {
private static final int BUFFER_SIZE = 65535;
public static byte[] convertFileToByteArray(String certificateFilePath) throws Exception {
if (certificateFilePath == null || certificateFilePath.isEmpty()) {
throw new Exception("file path should not be null or empty");
}
File file = new File(certificateFilePath);
if (!file.exists()) {
throw new Exception("file not exist : " + file.getAbsolutePath());
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-jose</artifactId>
</dependency>