Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation for configuring public endpoints #16345

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions docs/modules/ROOT/pages/servlet/architecture.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,136 @@ In the event that you are unable to reconfigure `HttpSecurity` to not add a cert
----
====

[[servlet-public-endpoints]]
== Configuring Public Endpoints

There are often endpoints that need to be accessible without authentication, such as login pages, public assets, or public APIs. The `@SecurityFilterChain` API allows you to configure which endpoints should be publicly accessible.

Let's look at how to configure endpoints to allow public access:

[tabs]
======
Java::
+
[source,java,role="primary"]
----
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf(Customizer.withDefaults())
.httpBasic(Customizer.withDefaults())
.authorizeHttpRequests(authorize -> authorize
// Public endpoints
.requestMatchers("/public/**", "/error").permitAll()
.requestMatchers(HttpMethod.GET, "/actuator/health").permitAll()
// Secure everything else
.anyRequest().authenticated()
);
return http.build();
}
----

Kotlin::
+
[source,kotlin,role="secondary"]
----
@Bean
fun filterChain(http: HttpSecurity): SecurityFilterChain {
http {
csrf { }
httpBasic { }
authorizeHttpRequests {
// Public endpoints
requestMatchers("/public/**", "/error").permitAll()
requestMatchers(HttpMethod.GET, "/actuator/health").permitAll()
// Secure everything else
anyRequest().authenticated()
}
}
return http.build()
}
----

======

[NOTE]
====
If a request presents credentials (tokens, Basic Auth, etc.), Spring Security will attempt to authenticate the user even when an endpoint is configured with `permitAll()`.
====


[TIP]
By using the `@Order` annotation, you can define separate security filter chains for public and secured endpoints.
Filter chains with lower order numbers are evaluated first.


[tabs]
======
Java::
+
[source,java,role="primary"]
----
@Bean
@Order(1)
public SecurityFilterChain publicFilterChain(HttpSecurity http) throws Exception {
http
.securityMatchers(matchers -> matchers
.requestMatchers("/public/**", "/error", "/actuator/health")
)
.authorizeHttpRequests(authorize -> authorize
.anyRequest().permitAll()
);
return http.build();
}

@Bean
@Order(2)
public SecurityFilterChain protectedFilterChain(HttpSecurity http) throws Exception {
http
.csrf(Customizer.withDefaults())
.httpBasic(Customizer.withDefaults())
.authorizeHttpRequests(authorize -> authorize
.anyRequest().authenticated()
);
return http.build();
}

----

Kotlin::
+
[source,kotlin,role="secondary"]
----
@Bean
@Order(1)
fun publicFilterChain(http: HttpSecurity): SecurityFilterChain {
http {
securityMatchers {
requestMatchers("/public/**", "/error", "/actuator/health")
}
authorizeHttpRequests {
anyRequest().permitAll()
}
}
return http.build()
}

@Bean
@Order(2)
fun protectedFilterChain(http: HttpSecurity): SecurityFilterChain {
http {
csrf { }
httpBasic { }
authorizeHttpRequests {
anyRequest().authenticated()
}
}
return http.build()
}
----

======

[[servlet-exceptiontranslationfilter]]
== Handling Security Exceptions

Expand Down