@@ -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
0 commit comments