Configuring SecurityFilterChain
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
write request rules in a SecurityFilterChain bean.
Planned topics
- Define a
@Bean SecurityFilterChainthat takes anHttpSecurity. - Use
authorizeHttpRequestswithrequestMatchers(...)to say who may call what. - Rules are checked top to bottom; put specific rules first and
anyRequest()last. - End with
anyRequest().authenticated()ordenyAll(), notpermitAll(). - For a stateless JSON API, you usually turn off sessions and form login; CSRF is covered in a later lecture.
Draft outline details
Demonstration
@Bean
SecurityFilterChain api(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(a -> a
.requestMatchers(HttpMethod.GET, "/api/products/**").permitAll()
.requestMatchers(HttpMethod.POST, "/api/products/**").hasRole("ADMIN")
.anyRequest().authenticated())
.httpBasic(Customizer.withDefaults());
return http.build();
}Check your understanding
Where must anyRequest() go?
Show answer
Last.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.