2014-06-05 32 views
5

giả sử ví dụ thế giới xin chào làm việc về bảo mật mùa xuân và mvc mùa xuân.Nội dung-Bảo mật-Chính sách Bảo mật mùa xuân

khi tôi tham gia một dấu vết với Wireshark tôi thấy những lá cờ sau theo yêu cầu http

X-Content-Type-Options: nosniff 
X-XSS-Protection: 1; mode=block 
Cache-Control: no-cache, no-store, max-age=0, must-revalidate 
Pragma: no-cache 
Expires: 0 
Strict-Transport-Security: max-age=31536000 ; includeSubDomains 
X-Frame-Options: DENY 
Set-Cookie: JSESSIONID=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX; Path=/; Secure; HttpOnly 

tôi muốn thêm video này vào tiêu đề của tôi:

Content-Security-Policy: script-src 'self' 

Tôi biết rằng X- Frame-Options đang thực hiện gần như cùng một công việc, nhưng nó vẫn khiến tôi ngủ ngon hơn. Bây giờ tôi đoán rằng tôi sẽ cần phải làm điều đó theo chức năng cấu hình của cấu hình bảo mật mùa xuân của tôi tuy nhiên tôi không biết làm thế nào chính xác, tức là tôi giả sử .headers(). Something.something (tự)

@Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
//   .csrf().disable() 
//   .headers().disable() 
      .authorizeRequests() 
       .antMatchers( "/register", 
           "/static/**", 
           "/h2/**", 
           "/resources/**", 
          "/resources/static/css/**", 
           "/resources/static/img/**" , 
           "/resources/static/js/**", 
           "/resources/static/pdf/**"        
           ).permitAll() 
       .anyRequest().authenticated() 
       .and() 
      .formLogin() 
       .loginPage("/login") 
       .permitAll() 
       .and() 
      .logout() 
       .permitAll(); 
    } 

Trả lời

11

Chỉ cần sử dụng phương thức addHeaderWriter như sau:

@EnableWebSecurity 
@Configuration 
public class WebSecurityConfig extends 
    WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
    http 
     // ... 
     .headers() 
     .addHeaderWriter(new StaticHeadersWriter("X-Content-Security-Policy","script-src 'self'")) 
     // ... 
    } 
} 

Lưu ý rằng ngay sau khi bạn chỉ định bất kỳ tiêu đề nào cần được bao gồm, thì chỉ những tiêu đề đó sẽ được bao gồm.

Để bao gồm các tiêu đề mặc định, bạn có thể làm:

http 
    .headers() 
    .contentTypeOptions() 
    .xssProtection() 
    .cacheControl() 
    .httpStrictTransportSecurity() 
    .frameOptions() 
    .addHeaderWriter(new StaticHeadersWriter("X-Content-Security-Policy","script-src 'self'")) 
    // ... 

Bạn có thể tham khảo các spring security documentation.

+0

công trình như một say mê, Christophe cảm ơn bạn . Một điều mà tôi đã lưu ý là chromium/rekonq/opera làm hiển thị css khi trang được tải, nhưng firefox không hiển thị css khi ".addHeaderWriter (new StaticHeaderWriter (" X-Content-Security-Policy "," script-src 'self' "))" được bật. Cũng lưu ý các "s" còn thiếu trong tên của phương thức "StaticHeaderWriter" tức là "StaticHeadersWriter". Tôi đã thấy rằng đây cũng là trường hợp với tài liệu tham chiếu mà bạn đã đăng. – Tito

+0

ok tôi tìm thấy lý do tại sao điều này không hoạt động trong firefox. tôi cần sử dụng các tiêu đề đã mua "Giải thích về chuyển đổi nội dung X-Nội dung-Bảo mật-Polic" và "Nội dung-Bảo mật-Chính sách" ở đây https://developer.mozilla.org/en-US/docs/Web/Security/CSP/Using_Content_Security_Policy – Tito

Các vấn đề liên quan