إعداد SecurityFilterChain
في هذا الدرس 4 موضوعات
هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
write request rules in a SecurityFilterChain bean.
الموضوعات المخطط لها
- 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.
تفاصيل المخطط المبدئي
عرض عملي
@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();
}اختبر فهمك
Where must anyRequest() go?
أظهر الإجابة
Last.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.