From a6afd1d269b19853fe0761cb449ea75c2490347f Mon Sep 17 00:00:00 2001 From: Anonymous User Date: Sun, 24 Aug 2025 13:08:31 +0530 Subject: [PATCH] feat: add OAuth2 sample applications and configurations --- spring-boot-admin-samples/pom.xml | 3 +- .../pom.xml | 54 ++++++++ .../client/OAuth2ClientApplication.java | 32 +++++ .../client/OAuth2ResourceServerConfig.java | 36 +++++ .../src/main/resources/application.yml | 21 +++ .../spring-boot-admin-sample-oauth2/pom.xml | 131 ++++++++++++++++++ .../sample/OAuth2RestTemplateConfig.java | 32 +++++ .../admin/sample/OAuth2SecurityConfig.java | 36 +++++ .../SpringBootAdminOAuth2Application.java | 36 +++++ .../src/main/resources/application-oauth2.yml | 15 ++ .../src/main/resources/application.yml | 21 +++ .../sample/OAuth2RestTemplateConfig.java | 38 +++++ .../admin/sample/OAuth2SecurityConfig.java | 35 +++++ .../SpringBootAdminOAuth2Application.java | 35 +++++ 14 files changed, 524 insertions(+), 1 deletion(-) create mode 100644 spring-boot-admin-samples/spring-boot-admin-sample-oauth2-client/pom.xml create mode 100644 spring-boot-admin-samples/spring-boot-admin-sample-oauth2-client/src/main/java/de/codecentric/boot/admin/sample/client/OAuth2ClientApplication.java create mode 100644 spring-boot-admin-samples/spring-boot-admin-sample-oauth2-client/src/main/java/de/codecentric/boot/admin/sample/client/OAuth2ResourceServerConfig.java create mode 100644 spring-boot-admin-samples/spring-boot-admin-sample-oauth2-client/src/main/resources/application.yml create mode 100644 spring-boot-admin-samples/spring-boot-admin-sample-oauth2/pom.xml create mode 100644 spring-boot-admin-samples/spring-boot-admin-sample-oauth2/src/main/java/de/codecentric/boot/admin/sample/OAuth2RestTemplateConfig.java create mode 100644 spring-boot-admin-samples/spring-boot-admin-sample-oauth2/src/main/java/de/codecentric/boot/admin/sample/OAuth2SecurityConfig.java create mode 100644 spring-boot-admin-samples/spring-boot-admin-sample-oauth2/src/main/java/de/codecentric/boot/admin/sample/SpringBootAdminOAuth2Application.java create mode 100644 spring-boot-admin-samples/spring-boot-admin-sample-oauth2/src/main/resources/application-oauth2.yml create mode 100644 spring-boot-admin-samples/spring-boot-admin-sample-oauth2/src/main/resources/application.yml create mode 100644 spring-boot-admin-samples/spring-boot-admin-sample-servlet/src/main/java/de/codecentric/boot/admin/sample/OAuth2RestTemplateConfig.java create mode 100644 spring-boot-admin-samples/spring-boot-admin-sample-servlet/src/main/java/de/codecentric/boot/admin/sample/OAuth2SecurityConfig.java create mode 100644 spring-boot-admin-samples/spring-boot-admin-sample-servlet/src/main/java/de/codecentric/boot/admin/sample/SpringBootAdminOAuth2Application.java diff --git a/spring-boot-admin-samples/pom.xml b/spring-boot-admin-samples/pom.xml index 1860153702f..3bfb6e69a87 100644 --- a/spring-boot-admin-samples/pom.xml +++ b/spring-boot-admin-samples/pom.xml @@ -35,10 +35,11 @@ spring-boot-admin-sample-custom-ui - spring-boot-admin-sample-servlet spring-boot-admin-sample-reactive spring-boot-admin-sample-war spring-boot-admin-sample-hazelcast + spring-boot-admin-sample-oauth2 + spring-boot-admin-sample-oauth2-client diff --git a/spring-boot-admin-samples/spring-boot-admin-sample-oauth2-client/pom.xml b/spring-boot-admin-samples/spring-boot-admin-sample-oauth2-client/pom.xml new file mode 100644 index 00000000000..1906b5fd379 --- /dev/null +++ b/spring-boot-admin-samples/spring-boot-admin-sample-oauth2-client/pom.xml @@ -0,0 +1,54 @@ + + + + de.codecentric + spring-boot-admin-samples + ${revision} + ../pom.xml + + 4.0.0 + + spring-boot-admin-sample-oauth2-client + Spring Boot Admin OAuth2 Client Sample + Sample monitored app configured as OAuth2 Resource Server + jar + + + + de.codecentric + spring-boot-admin-starter-client + + + org.springframework.boot + spring-boot-starter-oauth2-resource-server + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + de.codecentric.boot.admin.sample.client.OAuth2ClientApplication + + + + + diff --git a/spring-boot-admin-samples/spring-boot-admin-sample-oauth2-client/src/main/java/de/codecentric/boot/admin/sample/client/OAuth2ClientApplication.java b/spring-boot-admin-samples/spring-boot-admin-sample-oauth2-client/src/main/java/de/codecentric/boot/admin/sample/client/OAuth2ClientApplication.java new file mode 100644 index 00000000000..228e55be675 --- /dev/null +++ b/spring-boot-admin-samples/spring-boot-admin-sample-oauth2-client/src/main/java/de/codecentric/boot/admin/sample/client/OAuth2ClientApplication.java @@ -0,0 +1,32 @@ +/* + * Copyright 2014-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package de.codecentric.boot.admin.sample.client; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup; + +@SpringBootApplication +public class OAuth2ClientApplication { + + public static void main(String[] args) { + SpringApplication app = new SpringApplication(OAuth2ClientApplication.class); + app.setApplicationStartup(new BufferingApplicationStartup(1500)); + app.run(args); + } + +} diff --git a/spring-boot-admin-samples/spring-boot-admin-sample-oauth2-client/src/main/java/de/codecentric/boot/admin/sample/client/OAuth2ResourceServerConfig.java b/spring-boot-admin-samples/spring-boot-admin-sample-oauth2-client/src/main/java/de/codecentric/boot/admin/sample/client/OAuth2ResourceServerConfig.java new file mode 100644 index 00000000000..7745f811c6f --- /dev/null +++ b/spring-boot-admin-samples/spring-boot-admin-sample-oauth2-client/src/main/java/de/codecentric/boot/admin/sample/client/OAuth2ResourceServerConfig.java @@ -0,0 +1,36 @@ +/* + * Copyright 2014-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package de.codecentric.boot.admin.sample.client; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.web.SecurityFilterChain; + +@Configuration +public class OAuth2ResourceServerConfig { + + @Bean + SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { + http.authorizeHttpRequests( + (auth) -> auth.requestMatchers("/actuator/**").authenticated().anyRequest().permitAll()) + .oauth2ResourceServer((oauth2) -> oauth2.jwt((jwt) -> { + })); + return http.build(); + } + +} diff --git a/spring-boot-admin-samples/spring-boot-admin-sample-oauth2-client/src/main/resources/application.yml b/spring-boot-admin-samples/spring-boot-admin-sample-oauth2-client/src/main/resources/application.yml new file mode 100644 index 00000000000..1fcd493d843 --- /dev/null +++ b/spring-boot-admin-samples/spring-boot-admin-sample-oauth2-client/src/main/resources/application.yml @@ -0,0 +1,21 @@ +spring: + application: + name: spring-boot-admin-sample-oauth2-client + + security: + oauth2: + resourceserver: + jwt: + issuer-uri: https://YOUR_ISSUER_URL + +management: + endpoints: + web: + exposure: + include: "*" + +spring: + boot: + admin: + client: + url: http://localhost:8080 diff --git a/spring-boot-admin-samples/spring-boot-admin-sample-oauth2/pom.xml b/spring-boot-admin-samples/spring-boot-admin-sample-oauth2/pom.xml new file mode 100644 index 00000000000..c2e1a0cc144 --- /dev/null +++ b/spring-boot-admin-samples/spring-boot-admin-sample-oauth2/pom.xml @@ -0,0 +1,131 @@ + + + + + 4.0.0 + + spring-boot-admin-sample-servlet + + Spring Boot Admin Sample Servlet + Spring Boot Admin Sample Servlet + + + de.codecentric + spring-boot-admin-samples + ${revision} + ../pom.xml + + + + + de.codecentric + spring-boot-admin-sample-custom-ui + + + de.codecentric + spring-boot-admin-starter-server + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.cloud + spring-cloud-starter + + + org.springframework.boot + spring-boot-starter-mail + + + de.codecentric + spring-boot-admin-starter-client + + + org.springframework.session + spring-session-core + + + org.springframework.session + spring-session-jdbc + + + org.springframework.cloud + spring-cloud-starter-config + + + org.hsqldb + hsqldb + + + org.jolokia + jolokia-support-spring + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + ${project.artifactId} + + + org.cyclonedx + cyclonedx-maven-plugin + + + generate-resources + + makeAggregateBom + + + application + ${project.build.outputDirectory} + bom + json + true + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + build-info + + + + + de.codecentric.boot.admin.sample.SpringBootAdminServletApplication + false + + + + + diff --git a/spring-boot-admin-samples/spring-boot-admin-sample-oauth2/src/main/java/de/codecentric/boot/admin/sample/OAuth2RestTemplateConfig.java b/spring-boot-admin-samples/spring-boot-admin-sample-oauth2/src/main/java/de/codecentric/boot/admin/sample/OAuth2RestTemplateConfig.java new file mode 100644 index 00000000000..696a4ead518 --- /dev/null +++ b/spring-boot-admin-samples/spring-boot-admin-sample-oauth2/src/main/java/de/codecentric/boot/admin/sample/OAuth2RestTemplateConfig.java @@ -0,0 +1,32 @@ +/* + * Copyright 2014-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package de.codecentric.boot.admin.sample; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.client.ClientHttpRequestFactory; +import org.springframework.web.client.RestTemplate; + +@Configuration(proxyBeanMethods = false) +public class OAuth2RestTemplateConfig { + + @Bean + public RestTemplate restTemplate(ClientHttpRequestFactory factory) { + return new RestTemplate(factory); + } + +} diff --git a/spring-boot-admin-samples/spring-boot-admin-sample-oauth2/src/main/java/de/codecentric/boot/admin/sample/OAuth2SecurityConfig.java b/spring-boot-admin-samples/spring-boot-admin-sample-oauth2/src/main/java/de/codecentric/boot/admin/sample/OAuth2SecurityConfig.java new file mode 100644 index 00000000000..3a96b455951 --- /dev/null +++ b/spring-boot-admin-samples/spring-boot-admin-sample-oauth2/src/main/java/de/codecentric/boot/admin/sample/OAuth2SecurityConfig.java @@ -0,0 +1,36 @@ +/* + * Copyright 2014-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package de.codecentric.boot.admin.sample; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.web.SecurityFilterChain; + +import static org.springframework.security.config.Customizer.withDefaults; + +@Configuration(proxyBeanMethods = false) +public class OAuth2SecurityConfig { + + @Bean + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { + http.authorizeHttpRequests((authorizeRequests) -> authorizeRequests.anyRequest().authenticated()) + .oauth2Login(withDefaults()); + return http.build(); + } + +} diff --git a/spring-boot-admin-samples/spring-boot-admin-sample-oauth2/src/main/java/de/codecentric/boot/admin/sample/SpringBootAdminOAuth2Application.java b/spring-boot-admin-samples/spring-boot-admin-sample-oauth2/src/main/java/de/codecentric/boot/admin/sample/SpringBootAdminOAuth2Application.java new file mode 100644 index 00000000000..1d618f490a5 --- /dev/null +++ b/spring-boot-admin-samples/spring-boot-admin-sample-oauth2/src/main/java/de/codecentric/boot/admin/sample/SpringBootAdminOAuth2Application.java @@ -0,0 +1,36 @@ +/* + * Copyright 2014-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package de.codecentric.boot.admin.sample; + +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +public class SpringBootAdminOAuth2Application { + + public static void main(String[] args) { + SpringApplication.run(SpringBootAdminOAuth2Application.class, args); + } + + @Bean + public ApplicationRunner runner() { + return (args) -> System.out.println("Spring Boot Admin OAuth2 Sample started."); + } + +} diff --git a/spring-boot-admin-samples/spring-boot-admin-sample-oauth2/src/main/resources/application-oauth2.yml b/spring-boot-admin-samples/spring-boot-admin-sample-oauth2/src/main/resources/application-oauth2.yml new file mode 100644 index 00000000000..3420a4bcd46 --- /dev/null +++ b/spring-boot-admin-samples/spring-boot-admin-sample-oauth2/src/main/resources/application-oauth2.yml @@ -0,0 +1,15 @@ +spring: + profiles: oauth2 + + security: + oauth2: + client: + registration: + admin: + client-id: ${OAUTH_CLIENT_ID} + client-secret: ${OAUTH_CLIENT_SECRET} + authorization-grant-type: client_credentials + scope: openid + provider: + auth0: + token-uri: ${OAUTH_TOKEN_URI} diff --git a/spring-boot-admin-samples/spring-boot-admin-sample-oauth2/src/main/resources/application.yml b/spring-boot-admin-samples/spring-boot-admin-sample-oauth2/src/main/resources/application.yml new file mode 100644 index 00000000000..476fc8e10ac --- /dev/null +++ b/spring-boot-admin-samples/spring-boot-admin-sample-oauth2/src/main/resources/application.yml @@ -0,0 +1,21 @@ +spring: + application: + name: spring-boot-admin-sample-oauth2 + security: + oauth2: + client: + registration: + admin: + client-id: your-client-id + client-secret: your-client-secret + authorization-grant-type: client_credentials + scope: openid + provider: + auth0: + token-uri: https://YOUR_ISSUER/oauth/token + +management: + endpoints: + web: + exposure: + include: "*" diff --git a/spring-boot-admin-samples/spring-boot-admin-sample-servlet/src/main/java/de/codecentric/boot/admin/sample/OAuth2RestTemplateConfig.java b/spring-boot-admin-samples/spring-boot-admin-sample-servlet/src/main/java/de/codecentric/boot/admin/sample/OAuth2RestTemplateConfig.java new file mode 100644 index 00000000000..ec18c7d356c --- /dev/null +++ b/spring-boot-admin-samples/spring-boot-admin-sample-servlet/src/main/java/de/codecentric/boot/admin/sample/OAuth2RestTemplateConfig.java @@ -0,0 +1,38 @@ + +/* + * Copyright 2014-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package de.codecentric.boot.admin.sample; + +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager; +import org.springframework.security.oauth2.client.web.reactive.function.client.ServletOAuth2AuthorizedClientExchangeFilterFunction; +import org.springframework.web.client.RestTemplate; + +@Configuration(proxyBeanMethods = false) +public class OAuth2RestTemplateConfig { + + @Bean + public RestTemplate restTemplate(OAuth2AuthorizedClientManager authorizedClientManager) { + return new RestTemplateBuilder() + .additionalRequestCustomizers(new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager) + .getRequestCustomizer()) + .build(); + } + +} diff --git a/spring-boot-admin-samples/spring-boot-admin-sample-servlet/src/main/java/de/codecentric/boot/admin/sample/OAuth2SecurityConfig.java b/spring-boot-admin-samples/spring-boot-admin-sample-servlet/src/main/java/de/codecentric/boot/admin/sample/OAuth2SecurityConfig.java new file mode 100644 index 00000000000..904fcc1d364 --- /dev/null +++ b/spring-boot-admin-samples/spring-boot-admin-sample-servlet/src/main/java/de/codecentric/boot/admin/sample/OAuth2SecurityConfig.java @@ -0,0 +1,35 @@ + +/* + * Copyright 2014-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package de.codecentric.boot.admin.sample; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.web.SecurityFilterChain; + +@Configuration(proxyBeanMethods = false) +public class OAuth2SecurityConfig { + + @Bean + public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { + http.authorizeHttpRequests(authorize -> authorize.anyRequest().authenticated()) + .oauth2Login(oauth2Login -> oauth2Login.defaultSuccessUrl("/", true)); + return http.build(); + } + +} diff --git a/spring-boot-admin-samples/spring-boot-admin-sample-servlet/src/main/java/de/codecentric/boot/admin/sample/SpringBootAdminOAuth2Application.java b/spring-boot-admin-samples/spring-boot-admin-sample-servlet/src/main/java/de/codecentric/boot/admin/sample/SpringBootAdminOAuth2Application.java new file mode 100644 index 00000000000..b97c669d4aa --- /dev/null +++ b/spring-boot-admin-samples/spring-boot-admin-sample-servlet/src/main/java/de/codecentric/boot/admin/sample/SpringBootAdminOAuth2Application.java @@ -0,0 +1,35 @@ + +/* + * Copyright 2014-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package de.codecentric.boot.admin.sample; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.Configuration; + +import de.codecentric.boot.admin.server.config.EnableAdminServer; + +@Configuration(proxyBeanMethods = false) +@EnableAutoConfiguration +@EnableAdminServer +public class SpringBootAdminOAuth2Application { + + public static void main(String[] args) { + SpringApplication.run(SpringBootAdminOAuth2Application.class, args); + } + +}