22
33import java .util .UUID ;
44
5- import de .codecentric .boot .admin .server .config .AdminServerProperties ;
65import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
7- import jakarta .servlet .DispatcherType ;
86import org .springframework .beans .factory .annotation .Value ;
97import org .springframework .boot .autoconfigure .security .SecurityProperties ;
108import org .springframework .context .annotation .Bean ;
2422import org .springframework .security .web .csrf .CsrfTokenRequestAttributeHandler ;
2523import org .springframework .security .web .util .matcher .AntPathRequestMatcher ;
2624
27- import static org .springframework .http .HttpMethod .DELETE ;
28- import static org .springframework .http .HttpMethod .POST ;
29-
3025/** Secures the endpoints of this application. */
3126@ Configuration (proxyBeanMethods = false )
3227@ EnableWebSecurity
3328public class AppSecurityConfiguration {
3429
35- private final AdminServerProperties adminServer ;
36-
3730 @ Value ("${app.spring-boot-admin.role.user.name}" )
3831 private String roleUserName ;
3932
@@ -47,9 +40,7 @@ public class AppSecurityConfiguration {
4740 private String roleAdminPassword ;
4841
4942 @ SuppressFBWarnings ("EI_EXPOSE_REP2" )
50- public AppSecurityConfiguration (AdminServerProperties adminServer , SecurityProperties security ) {
51- this .adminServer = adminServer ;
52- }
43+ public AppSecurityConfiguration (SecurityProperties security ) {}
5344
5445 /**
5546 * Applies security policies such as authentication requirements to endpoints.
@@ -63,7 +54,7 @@ protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
6354 SavedRequestAwareAuthenticationSuccessHandler successHandler =
6455 new SavedRequestAwareAuthenticationSuccessHandler ();
6556 successHandler .setTargetUrlParameter ("redirectTo" );
66- successHandler .setDefaultTargetUrl (this . adminServer . path ( "/" ) );
57+ successHandler .setDefaultTargetUrl ("/" );
6758
6859 // NOTE: In this project, the Spring Boot Admin server and client are colocated in the same
6960 // application for demonstration purposes. In production, you would typically not do that
@@ -77,20 +68,6 @@ protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
7768 http .authorizeHttpRequests (
7869 (authorizeRequests ) ->
7970 authorizeRequests
80- //// For the Spring Boot Admin server.
81- .requestMatchers (
82- // Permit public access to all static assets.
83- new AntPathRequestMatcher (this .adminServer .path ("/assets/**" )),
84- // Permit public access to the login page.
85- new AntPathRequestMatcher (this .adminServer .path ("/login" )))
86- .permitAll ()
87- // Permit asynchronous processing of a request without requiring authentication.
88- // FIXME: Permitting any async requests as a workaround appears dangerous.
89- // https://github.com/spring-projects/spring-security/issues/11027 (from 2022)
90- .dispatcherTypeMatchers (DispatcherType .ASYNC )
91- .permitAll ()
92-
93- //// For the Spring Boot Admin client (the "real" app being developed).
9471 .requestMatchers (
9572 new AntPathRequestMatcher ("/" ),
9673 // Permit public access to this app's example endpoint at `/welcome`.
@@ -103,16 +80,9 @@ protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
10380 new AntPathRequestMatcher ("/actuator/info" ),
10481 new AntPathRequestMatcher ("/actuator/prometheus" ))
10582 .permitAll ()
106-
107- //// Applies to both SBA server and clients.
10883 // All other requests must be authenticated.
10984 .anyRequest ()
11085 .authenticated ())
111- // For Spring Boot Admin server: enables form-based login and logout.
112- .formLogin (
113- (formLogin ) ->
114- formLogin .loginPage (this .adminServer .path ("/login" )).successHandler (successHandler ))
115- .logout ((logout ) -> logout .logoutUrl (this .adminServer .path ("/logout" )))
11686 // Enables HTTP Basic Authentication support.
11787 .httpBasic (Customizer .withDefaults ());
11888
@@ -121,20 +91,7 @@ protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
12191 .csrf (
12292 (csrf ) ->
12393 csrf .csrfTokenRepository (CookieCsrfTokenRepository .withHttpOnlyFalse ())
124- .csrfTokenRequestHandler (new CsrfTokenRequestAttributeHandler ())
125- .ignoringRequestMatchers (
126- //// For the Spring Boot Admin server.
127- // Disables CSRF-Protection for the SBA server's endpoints that the SBA
128- // client uses to (de-)register.
129- new AntPathRequestMatcher (
130- this .adminServer .path ("/instances" ), POST .toString ()),
131- new AntPathRequestMatcher (
132- this .adminServer .path ("/instances/*" ), DELETE .toString ()),
133-
134- //// For the Spring Boot Admin client.
135- // Disables CSRF-Protection for the SBA client's actuator endpoints that
136- // the SBA server uses to collect metrics.
137- new AntPathRequestMatcher ("/actuator/**" )));
94+ .csrfTokenRequestHandler (new CsrfTokenRequestAttributeHandler ()));
13895
13996 http .rememberMe (
14097 (rememberMe ) -> rememberMe .key (UUID .randomUUID ().toString ()).tokenValiditySeconds (1209600 ));
@@ -145,14 +102,6 @@ protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
145102 /** Required to provide UserDetailsService for "remember functionality". */
146103 @ Bean
147104 public InMemoryUserDetailsManager userDetailsService (PasswordEncoder passwordEncoder ) {
148- // NOTE: Because this example project runs the Spring Boot Admin server and client in the same
149- // application, both the server's secured (with HTTP Basic Authentication) SBA API
150- // endpoint and the client's Spring actuator endpoints coincidentally require exactly the
151- // same username/password combination.
152- // In production, this is not recommended. See the recommendations of Spring Boot Admin at
153- // https://docs.spring-boot-admin.com/current/faq.html.
154- // Instead, in production you would separate clients from the server, and thus different
155- // username/password combinations can be used.
156105 // NOTE: HTTP Basic Authentication itself is not recommended for production.
157106 UserDetails user =
158107 User .withUsername (roleUserName )
0 commit comments