Skip to content

Commit 345e0e4

Browse files
docs: fix OAuth2 SSO setup and sample project guidance (#5751)
Co-authored-by: ulrichschulte <ulrich.schulte@codecentric.de>
1 parent 4190bb7 commit 345e0e4

4 files changed

Lines changed: 77 additions & 20 deletions

File tree

‎spring-boot-admin-docs/src/site/docs/02-server/02-security.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,11 @@ credentials.
134134

135135
:::tip
136136
When using this approach the SBA Server decides whether the user can access the registered applications. There are more
137-
complex solutions possible (using OAuth2) to let the clients decide if the user can access the endpoints. For that
138-
please have a look at the samples
139-
in [joshiste/spring-boot-admin-samples](https://github.com/joshiste/spring-boot-admin-samples).
137+
complex solutions possible (using OAuth2) to let the clients decide if the user can access the endpoints. Implement
138+
this with your own `HttpHeadersProvider` that acquires a token and adds it as a bearer token. See
139+
[Server Authentication](../05-security/10-server-authentication.md) for the server side and
140+
[Actuator Security](../05-security/20-actuator-security.md) for the client side. Runnable example projects live in
141+
[spring-boot-admin-runtime-playground](https://github.com/codecentric/spring-boot-admin-runtime-playground).
140142
:::
141143

142144
### SBA Client

‎spring-boot-admin-docs/src/site/docs/05-security/10-server-authentication.md‎

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -445,25 +445,72 @@ public SecurityFilterChain filterChain(HttpSecurity http,
445445
.requestMatchers(PathPatternRequestMatcher.withDefaults()
446446
.matcher(adminServer.path("/assets/**")))
447447
.permitAll()
448-
.requestMatchers(PathPatternRequestMatcher.withDefaults()
449-
.matcher(adminServer.path("/login")))
450-
.permitAll()
451448
.anyRequest().authenticated()
452449
)
453-
.oauth2Login(oauth2 -> oauth2
454-
.loginPage(adminServer.path("/login"))
455-
)
450+
// No custom loginPage: Spring Security redirects browsers to the IdP.
451+
.oauth2Login(Customizer.withDefaults())
452+
// Lets Spring Boot Admin Clients register with Basic credentials.
453+
.httpBasic(Customizer.withDefaults())
456454
.logout(logout -> logout
457455
.logoutUrl(adminServer.path("/logout"))
458456
.logoutSuccessUrl(adminServer.path("/"))
457+
)
458+
.csrf(csrf -> csrf
459+
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
460+
.csrfTokenRequestHandler(new CsrfTokenRequestAttributeHandler())
461+
.ignoringRequestMatchers(
462+
PathPatternRequestMatcher.withDefaults()
463+
.matcher(POST, adminServer.path("/instances")),
464+
PathPatternRequestMatcher.withDefaults()
465+
.matcher(DELETE, adminServer.path("/instances/*")),
466+
PathPatternRequestMatcher.withDefaults()
467+
.matcher(adminServer.path("/actuator/**"))
468+
)
459469
);
460470
461-
// CSRF and other configurations...
471+
// CustomCsrfFilter: see "Custom CSRF Filter" above
472+
http.addFilterAfter(new CustomCsrfFilter(), BasicAuthenticationFilter.class);
462473
463474
return http.build();
464475
}
476+
477+
// Spring Boot's UserDetailsServiceAutoConfiguration backs off once a
478+
// ClientRegistrationRepository exists, so spring.security.user.* is ignored.
479+
// Define the Basic-auth user for registering clients explicitly.
480+
@Bean
481+
public InMemoryUserDetailsManager userDetailsService(PasswordEncoder passwordEncoder) {
482+
UserDetails client = User.withUsername("client")
483+
.password(passwordEncoder.encode(System.getenv("SBA_CLIENT_PASSWORD")))
484+
.roles("CLIENT")
485+
.build();
486+
return new InMemoryUserDetailsManager(client);
487+
}
488+
489+
@Bean
490+
public PasswordEncoder passwordEncoder() {
491+
return new BCryptPasswordEncoder();
492+
}
465493
```
466494

495+
:::warning
496+
Do not set `oauth2Login(oauth2 -> oauth2.loginPage(adminServer.path("/login")))`: `/login` is Spring Boot Admin's
497+
local username/password form and has no link to your identity provider.
498+
:::
499+
500+
Keep `httpBasic()` and the `userDetailsService` bean, otherwise Spring Boot Admin Clients cannot register with their
501+
Basic credentials: with OAuth2 on the classpath, Spring Boot no longer creates a user from `spring.security.user.*`.
502+
Keep the CSRF exemptions for `POST /instances`, `DELETE /instances/*` and `/actuator/**`, otherwise client
503+
registration is rejected with `403` (or `401` when the error page itself requires authentication). See
504+
[CSRF Protection](./30-csrf-protection.md).
505+
506+
:::note
507+
Browser requests (`Accept: text/html`) are redirected to the identity provider. Requests without `Accept: text/html`,
508+
such as plain `curl` or XHR calls, get `401` by design. When a UI session expires, the UI redirects to `login` relative
509+
to the Admin Server's context path. Without a context path this is Spring Security's generated `/login` page, which
510+
links to the identity provider. With `spring.boot.admin.context-path` set, it is Spring Boot Admin's own login form
511+
instead; users then need to reload the UI to be redirected to the identity provider.
512+
:::
513+
467514
---
468515

469516
## Role-Based Access Control

‎spring-boot-admin-docs/src/site/docs/05-security/30-csrf-protection.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,9 @@ server:
478478
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
479479
```
480480

481-
### Issue: Client registration fails with 403
481+
### Issue: Client registration fails with 403 or 401
482482

483-
**Cause**: `/instances` endpoint not exempted from CSRF.
483+
**Cause**: `/instances` endpoint not exempted from CSRF. The `CsrfFilter` rejects the request with `403`; if the forwarded `/error` page also requires authentication, the client sees `401` instead.
484484

485485
**Solution**: Add to `ignoringRequestMatchers`:
486486

‎spring-boot-admin-docs/src/site/docs/09-samples/index.md‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ patterns. These samples provide working examples you can use as starting points
3131

3232
## Repository Location
3333

34-
All samples are available in
34+
The samples listed above live in
3535
the [Spring Boot Admin GitHub repository](https://github.com/codecentric/spring-boot-admin/tree/master/spring-boot-admin-samples):
3636

3737
```
@@ -47,6 +47,11 @@ spring-boot-admin-samples/
4747
└── spring-boot-admin-sample-mcp/
4848
```
4949

50+
Additional example projects covering specific use cases and infrastructure (discovery setups, proxies and similar) live
51+
in the separate
52+
[spring-boot-admin-runtime-playground](https://github.com/codecentric/spring-boot-admin-runtime-playground)
53+
repository.
54+
5055
## Running the Samples
5156

5257
### Prerequisites
@@ -264,14 +269,17 @@ Update Spring Boot Admin version in parent POM and rebuild.
264269

265270
## Contributing
266271

267-
To add a new sample:
272+
The samples in `spring-boot-admin-samples/` demonstrate core Spring Boot Admin features. New example projects for
273+
specific use cases and infrastructure belong in
274+
the [spring-boot-admin-runtime-playground](https://github.com/codecentric/spring-boot-admin-runtime-playground)
275+
repository instead.
276+
277+
To add a new example there:
268278

269-
1. Create directory under `spring-boot-admin-samples/`
270-
2. Follow existing sample structure
271-
3. Add `README.md` with specific instructions
272-
4. Include `docker-compose.yml` if applicable
273-
5. Add tests
274-
6. Update samples documentation
279+
1. Create a directory for your use case, following the structure of the existing examples
280+
2. Add a `README.md` with step-by-step instructions
281+
3. Include a `docker-compose.yml` if the example needs infrastructure
282+
4. Add the example to the playground's `README.md`
275283

276284
## See Also
277285

0 commit comments

Comments
 (0)