Spring Security — כותרות אבטחה
אמ;לק
כותרות הגנה ותצורה מותאמת ב-Spring Security.
סיכום בעברית: מערכת VibeSec · מדיניות המקורות
נוסף ל-VibeSec:
>כותרות הגנה ותצורה מותאמת ב-Spring Security.
התוכן המלא בעברית
תרגום אוטומטי מהמקור; מונחים טכניים נשארו באנגלית. לנוסח המחייב — הכתבה המקורית.
ניתן להשתמש בכותרות HTTP מאובטחות (Security HTTP Response Headers) כדי להגביר את אבטחת יישומי האינטרנט. סעיף זה מוקדש לתמיכה מבוססת servlet בכותרות אלו.
ברירת מחדל של כותרות אבטחה (Security Headers)
Spring Security מספק ערכת ברירת מחדל של כותרות HTTP מאובטחות כדי להבטיח הגדרות בסיס בטוחות. בעוד שכל אחת מכותרות אלו נחשבת לשיטת עבודה מומלצת, יש לציין שלא כל הלקוחות משתמשים בהן, ולכן מומלץ לבצע בדיקות נוספות.
ניתן להתאים אישית כותרות ספציפיות. לדוגמה, נניח שאתה מעוניין בברירות המחדל אך רוצה להגדיר SAMEORIGIN עבור X-Frame-Options.
אתה יכול לעשות זאת עם התצורה הבאה:
@Configuration @EnableWebSecurity public class WebSecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http // ... .headers((headers) -> headers .frameOptions((frameOptions) -> frameOptions .sameOrigin() ) ); return http.build(); } }
<http> <!-- ... --> <headers> <frame-options policy="SAMEORIGIN" /> </headers> </http>
@Configuration @EnableWebSecurity class SecurityConfig { @Bean open fun filterChain(http: HttpSecurity): SecurityFilterChain { http { // ... headers { frameOptions { sameOrigin = true } } } return http.build() } }
אם אינך מעוניין שברירות המחדל יתווספו ואתה זקוק לשליטה מפורשת על אילו כותרות יש להשתמש, תוכל להשבית את ברירות המחדל. רישום הקוד הבא מראה כיצד לעשות זאת.
אם אתה משתמש בתצורה של Spring Security, הדוגמה הבאה מוסיפה רק Cache Control:
@Configuration @EnableWebSecurity public class WebSecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http // ... .headers((headers) -> headers // do not use any default headers unless explicitly listed .defaultsDisabled() .cacheControl(withDefaults()) ); return http.build(); } }
<http> <!-- ... --> <headers defaults-disabled="true"> <cache-control/> </headers> </http>
@Configuration @EnableWebSecurity class SecurityConfig { @Bean open fun filterChain(http: HttpSecurity): SecurityFilterChain { http { // ... headers { // do not use any default headers unless explicitly listed defaultsDisabled = true cacheControl { } } } return http.build() } }
במידת הצורך, ניתן להשבית את כל כותרות האבטחה של HTTP באמצעות התצורה הבאה:
@Configuration @EnableWebSecurity public class WebSecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http // ... .headers((headers) -> headers.disable()); return http.build(); } }
<http> <!-- ... --> <headers disabled="true" /> </http>
@Configuration @EnableWebSecurity class SecurityConfig { @Bean open fun filterChain(http: HttpSecurity): SecurityFilterChain { http { // ... headers { disable() } } return http.build() } }
Cache Control
Spring Security כולל כותרות Cache Control כברירת מחדל.
עם זאת, אם אתה באמת רוצה לשמור תגובות ספציפיות במטמון, היישום שלך יכול להפעיל באופן סלקטיבי את HttpServletResponse.setHeader(String,String) כדי לעקוף את הכותרת שהוגדרה על ידי Spring Security. אתה יכול להשתמש בזה כדי להבטיח שתוכן (כגון CSS, JavaScript ותמונות) שמור כהלכה.
כאשר אתה משתמש ב-Spring Web MVC, זה נעשה בדרך כלל במסגרת התצורה שלך. תוכל למצוא פרטים כיצד לעשות זאת בחלק Static Resources בתיעוד Spring Reference
במידת הצורך, תוכל גם להשבית את כותרות התגובה של בקרת המטמון HTTP של Spring Security.
@Configuration @EnableWebSecurity public class WebSecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http // ... .headers((headers) -> headers .cacheControl((cache) -> cache.disable()) ); return http.build(); } }
<http> <!-- ... --> <headers> <cache-control disabled="true"/> </headers> </http>
@Configuration @EnableWebSecurity class SecurityConfig { @Bean open fun filterChain(http: HttpSecurity): SecurityFilterChain { http { headers { cacheControl { disable() } } } return http.build() } }
Content Type Options
Spring Security כולל כותרות Content-Type כברירת מחדל. עם זאת, ניתן להשבית אותן:
@Configuration @EnableWebSecurity public class WebSecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http // ... .headers((headers) -> headers .contentTypeOptions((contentTypeOptions) -> contentTypeOptions.disable()) ); return http.build(); } }
<http> <!-- ... --> <headers> <content-type-options disabled="true"/> </headers> </http>
@Configuration @EnableWebSecurity class SecurityConfig { @Bean open fun filterChain(http: HttpSecurity): SecurityFilterChain { http { headers { contentTypeOptions { disable() } } } return http.build() } }
HTTP Strict Transport Security (HSTS)
כברירת מחדל, Spring Security מספק את הכותרת Strict Transport Security. עם זאת, ניתן להתאים אישית את התוצאות באופן מפורש. הדוגמה הבאה מגדירה HSTS במפורש:
@Configuration @EnableWebSecurity public class WebSecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http // ... .headers((headers) -> headers .httpStrictTransportSecurity((hsts) -> hsts .includeSubDomains(true) .preload(true) .maxAgeInSeconds(31536000) ) ); return http.build(); } }
<http> <!-- ... --> <headers> <hsts include-subdomains="true" max-age-seconds="31536000" preload="true" /> </headers> </http>
@Configuration @EnableWebSecurity class SecurityConfig { @Bean open fun filterChain(http: HttpSecurity): SecurityFilterChain { http { headers { httpStrictTransportSecurity { includeSubDomains = true preload = true maxAgeInSeconds = 31536000 } } } return http.build() } }
HTTP Public Key Pinning (HPKP)
Spring Security מספק תמיכת servlet עבור HTTP Public Key Pinning, אך היא אינה מומלצת עוד.
אתה יכול להפעיל כותרות HPKP עם התצורה הבאה:
@Configuration @EnableWebSecurity public class WebSecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http // ... .headers((headers) -> headers .httpPublicKeyPinning((hpkp) -> hpkp .includeSubDomains(true) .reportUri("https://example.net/pkp-report") .addSha256Pins("d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=", "E9CZ9INDbd+2eRQozYqqbQ2yXLVKB9+xcprMF+44U1g=") ) ); return http.build(); } }
<http> <!-- ... --> <headers> <hpkp include-subdomains="true" report-uri="https://example.net/pkp-report"> <pins> <pin algorithm="sha256">d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=</pin> <pin algorithm="sha256">E9CZ9INDbd+2eRQozYqqbQ2yXLVKB9+xcprMF+44U1g=</pin> </pins> </hpkp> </headers> </http>
@Configuration @EnableWebSecurity class SecurityConfig { @Bean open fun filterChain(http: HttpSecurity): SecurityFilterChain { http { headers { httpPublicKeyPinning { includeSubDomains = true reportUri = "https://example.net/pkp-report" pins = mapOf("d6qzRu9zOECb90Uez27xWltNsj0e1Md7GkYYkVoZWmM=" to "sha256", "E9CZ9INDbd+2eRQozYqqbQ2yXLVKB9+xcprMF+44U1g=" to "sha256") } } } return http.build() } }
X-Frame-Options
כברירת מחדל, Spring Security מורה לדפדפנים לחסום התקפות XSS משתקפות באמצעות הכותרת X-Frame-Options.
לדוגמה, התצורה הבאה מציינת ש-Spring Security לא יורה עוד לדפדפנים לחסום את התוכן:
@Configuration @EnableWebSecurity public class WebSecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http // ... .headers((headers) -> headers .frameOptions((frameOptions) -> frameOptions .sameOrigin() ) ); return http.build(); } }
<http> <!-- ... --> <headers> <frame-options policy="SAMEORIGIN" /> </headers> </http>
@Configuration @EnableWebSecurity class SecurityConfig { @Bean open fun filterChain(http: HttpSecurity): SecurityFilterChain { http { headers { frameOptions { sameOrigin = true } } } return http.build() } }
X-XSS-Protection
כברירת מחדל, Spring Security מורה לדפדפנים להשבית את ה-XSS Auditor באמצעות הכותרת X-XSS-Protection. עם זאת, ניתן לשנות ברירת מחדל זו. לדוגמה, התצורה הבאה מורה לדפדפנים תואמים לאפשר סינון ולחסום את התוכן:
@Configuration @EnableWebSecurity public class WebSecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http // ... .headers((headers) -> headers .xssProtection((xss) -> xss .headerValue(XXssProtectionHeaderWriter.HeaderValue.ENABLED_MODE_BLOCK) ) ); return http.build(); } }
<http> <!-- ... --> <headers> <xss-protection headerValue="1; mode=block"/> </headers> </http>
@Configuration @EnableWebSecurity class SecurityConfig { @Bean open fun filterChain(http: HttpSecurity): SecurityFilterChain { // ... http { headers { xssProtection { headerValue = XXssProtectionHeaderWriter.HeaderValue.ENABLED_MODE_BLOCK } } } return http.build() } }
Content Security Policy (CSP)
Spring Security אינו מוסיף Content Security Policy כברירת מחדל, מכיוון שלא ניתן לקבוע מדיניות סבירה ללא הכרת הקשר היישום. על מפתח יישום האינטרנט להצהיר על מדיניות האבטחה (או המדיניות) לאכיפה או לניטור עבור המשאבים המוגנים.
Content-Security-Policy: script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/
בהתחשב במדיניות האבטחה הקודמת, אתה יכול להפעיל את הכותרת CSP:
@Configuration @EnableWebSecurity public class WebSecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http // ... .headers((headers) -> headers .contentSecurityPolicy((csp) -> csp .policyDirectives("script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/") ) ); return http.build(); } }
<http> <!-- ... --> <headers> <content-security-policy policy-directives="script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/" /> </headers> </http>
@Configuration @EnableWebSecurity class SecurityConfig { @Bean open fun filterChain(http: HttpSecurity): SecurityFilterChain { http { // ... headers { contentSecurityPolicy { policyDirectives = "script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/" } } } return http.build() } }
כדי להפעיל את כותרת הדיווח בלבד (report-only) של CSP, השתמש בתצורה הבאה:
@Configuration @EnableWebSecurity public class WebSecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http // ... .headers((headers) -> headers .contentSecurityPolicy((csp) -> csp .policyDirectives("script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/") .reportOnly() ) ); return http.build(); } }
<http> <!-- ... --> <headers> <content-security-policy policy-directives="script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/" report-only="true" /> </headers> </http>
@Configuration @EnableWebSecurity class SecurityConfig { @Bean open fun filterChain(http: HttpSecurity): SecurityFilterChain { http { // ... headers { contentSecurityPolicy { policyDirectives = "script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/" reportOnly = true } } } return http.build() } }
Referrer Policy
Spring Security אינו מוסיף כותרות Referrer Policy כברירת מחדל. ניתן להפעיל את הכותרת Referrer Policy באמצעות התצורה הבאה:
קריאה וכלים קשורים
אבטחת Java / Spring- Bearer — SAST עם התמקדות בפרטיות ו-PII
- CodeQL — מנוע ניתוח סטטי של GitHub
- Dependabot — עדכונים אוטומטיים לתלויות פגיעות
- חולשת Java4Shell: סכנת השתלטות מרחוק על שרתים
- פרצת אבטחה קריטית ב-Apache Roller תחת מתקפה
- חולשת אבטחה קריטית ב-Java: הנחיה לעדכון מיידי