Rbac php

Spring Security cung cấp nhiều lựa chọn để phân quyền. Trong bài viết này, tôi chỉ tập trung chia sẻ kỹ thuật phần bản quyền mới nhất và linh hoạt nhất sử dụng các chú thích

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
5,
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
6,
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
7. Mã nguồn minh họa trực quan ở đây

0. Chạy thử ứng dụng

Để các bạn không phải đợi lâu, tôi trình bày các mẫu thử ứng dụng luôn luôn

  1. Ứng dụng này sử dụng CSDL SQL trong bộ nhớ là H2 nhúng sẵn bên trong ứng dụng nên không cần cài đặt DB bên ngoài
  2. Chỉ cần biên dịch rồi truy cập địa chỉ http. //máy chủ cục bộ. 8080. Nếu JDK trên máy của bạn không phải là phiên bản 16 thì hãy vào tệp pom. xml sửa đổi
    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
      @Bean
        //Cấu hình Password encoder. Cần phải có !
      public PasswordEncoder encoder[] {
        return new BCryptPasswordEncoder[];
      }
    
      @Override
        //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
      protected void configure[HttpSecurity http] throws Exception {
        http.authorizeRequests[]
          .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
          .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
      }
    }
    8
  3. Ứng dụng có 4 vai trò. QUẢN TRỊ VIÊN, NGƯỜI DÙNG, BIÊN TẬP và TÁC GIẢ. Một người có thể được giao nhiều vai trò
  4. Ứng dụng có 4 địa chỉ /admin, /user , /editor, /author. Mọi người có vai trò tương ứng sẽ xem được

Lỗi khi không có quyền truy cập

Đường http. //máy chủ cục bộ. 8080/api/post filter bài viết theo tác giả đăng nhập

1. Tổng quan về phân quyền trong Spring Security

Phân quyền [Authorization] là bước tiếp theo của xác thực [Authentication]. Sau khi biết người đăng nhập là ai, thì hệ thống sẽ biết được vai trò [vai trò] của người đó trong ứng dụng và cấp quyền hạn tương ứng

Ủy quyền độc lập với kỹ thuật xác thực. Chức năng phân quyền có thể hoạt động tốt với bất kỳ cơ chế xác thực nào http basic, form login, digest, session less token [JWT Token] để xác thực

Spring Security có 2 khái niệm Vai trò và Quyền hạn thường là đồng nhất trong phần lớn trường hợp, nên rất dễ nhầm lẫn. This Unknown is error design or is the idea of ​​the author Spring Security. Spring Security cũng không thiết kế hoàn toàn giống theo mô hình RBAC [Điều khiển truy cập dựa trên vai trò]. Mô hình RBAC có 3 đối tượng. Chủ đề[chủ thể], Vai trò[vai trò chơi], Quyền[quyền hạn]. Còn trong Spring Security, có User ~ chủ thể, Role ~ vai trò. Ứng với từng phương thức cụ thể trong Bộ điều khiển, Bộ điều khiển REST, Dịch vụ, thành viên lập trình sẽ sử dụng chú thích để cấp quyền thực thi phương thức cho một số trò chơi hoặc ai đó cụ thể có thể sử dụng Ngôn ngữ biểu thức Spring

EntityRBACSpring SecurityUserLưu trong CSDLLưu trong CSDLRoleLưu trong CSDLTuỳ có thể lưu hoặc không lưuPermissionLưu trong CSDLDùng chú thích để gán trực tiếp lên phương thức

RBAC chuẩn hóa việc lưu thông tin Người dùng, Vai trò, Quyền. theo định dạng bảng quan hệ trong cơ sở dữ liệu

Spring Security chỉ cung cấp Giao diện và một số chú thích cần thiết, thiết lập trình viên tự thực thi mã. Đánh giá của tôi Spring Security thực hiện và sử dụng đủ linh hoạt trong hoàn cảnh cụ thể của Spring Boot. Danh sách vai trò trong Spring Security buộc phải xác định trước khi biên dịch. Còn RBAC là tiêu chuẩn phân quyền cho phép vai trò có thể tạo động khi ứng dụng đang chạy. Tính năng này trong RBAC cũng không thực sự quá cần thiết và đối với khi làm ứng dụng trở nên bị rối, khó kiểm tra

2. Configure application Spring Boot to partition

Yêu cầu thư viện Spring Security

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
9


    org.springframework.boot
    spring-boot-starter-security

Các lớp quan trọng

  • Configure
    .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
          .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
    0 kế thừa lớp
    .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
          .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
    1
  • .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
          .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
    2 mô tả đối tượng, hoàn toàn tùy biến miễn phí là giao diện thủ thuật
    .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
          .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
    3
  • With Role you can use String or Enum type. Một Người dùng nhận 1 hoặc nhiều Vai trò, lưu dưới dạng
    .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
          .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
    4 hoặc
    .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
          .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
    5. Tuy nhiên để trả lời liệt kê danh sách chính xác Người dùng có Vai trò X, Vai trò Y thì tốt nhất là lưu Vai trò trong đó có quan hệ nhiều - nhiều với Cơ sở dữ liệu Người dùng

Phần còn lại là các thư viện thường đi kèm

  • Nhật Bản. quản lý User, Role qua ORM Hibernate
  • H2, MySQL, Postgresql, Oracle, MongoDB. là cơ sở dữ liệu lưu trữ thông tin Người dùng, Vai trò

3. Người dùng - Vai trò - Bài đăng

Để minh họa chức năng phần quyền, tôi tạo ra 3 Thực thể

  1. Người dùng sẽ có một hoặc nhiều vai trò. Đảo ngược một vai trò có thể phân chia cho nhiều người dùng. Quan hệ nhiều - nhiều
  2. Vai trò bao gồm ADMIN, USER, AUTHOR, EDITOR
  3. Bài viết do một người dùng viết. Một người dùng có thể viết nhiều bài đăng

Quy tắc phân quyền của một trang Blog công nghệ như sau

  • Tác giả là người được phép viết, sửa, xóa Bài đăng của chính mình
  • Editor được phép sửa, xóa bài viết của người khác
  • Người dùng được xem bài viết của mọi người, được bình luận vào bài viết. Nhưng không được cấp quyền tạo bài mới cho đến khi anh ta nhận đủ một lượng điểm thưởng thú vị
  • Quản trị viên được phép thêm bớt Vai trò cho người dùng hoặc vô hiệu hóa tài khoản Người dùng

Xin mời vào công việc Spring Security cho phép một người dùng nhận nhiều vai trò. Do that application with a Role they ta chỉ cấp đủ quyền cho Vai trò đó. Tổ hợp nhiều vai trò lại sẽ cho người dùng nhiều quyền. Không nhất thiết phải tạo ra Vai trò có rất nhiều quyền, sau đó để tinh chỉnh quyền, ta lại phải tạo ra rất nhiều Vai trò gây khó khăn trong cuộc kiểm tra

4. Cấu hình bảo mật

Để tích hợp Spring Security vào dự án Spring Boot, điều quan trọng nhất là bạn phải có ít nhất một tệp cấu hình. Trong dự án này là SecurityConfig. java kế thừa

.antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
6

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}

Giải thích các chú thích

  1. .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
          .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
    7 đánh dấu đây là lớp có các phương thức tạo thành phần Bean
  2. .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
          .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
    8 đây là lớp cấu hình cho Spring Security, hãy sử dụng cấu hình mặc định nếu người dùng không yêu cầu cụ thể thay đổi gì
  3. .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
          .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
    9 kích hoạt cả 3 cách phân quyền phương thức. Thực tế trong bài viết này, tôi chỉ ví dụ cách mới nhất là
    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
      @Bean
        //Cấu hình Password encoder. Cần phải có !
      public PasswordEncoder encoder[] {
        return new BCryptPasswordEncoder[];
      }
    
      @Override
        //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
      protected void configure[HttpSecurity http] throws Exception {
        http.authorizeRequests[]
          .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
          .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
      }
    }
    70 sử dụng. Xem thêm so sánh giữa 3 phương pháp tại đây. @RolesAllowed so với. @PreAuthorize so với. @Bảo mật

Dòng cấu hình bên dưới chỉ áp dụng cho môi trường thử nghiệm, cho phép thiết lập trình viên truy cập h2-console để xem dữ liệu trong H2 mà không cần đăng nhập

.antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];

5. Sinh dữ liệu mẫu để thử nghiệm

Vui lòng xem kỹ file SecurityService. java, has an a phương thức tạo dữ liệu mẫu Vai trò, Người dùng, cùng các bài viết

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
7

6. Giao diện UserDetailSservice

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
71 là một giao diện có duy nhất một mẫu phương thức
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
72

Trong ứng dụng Spring Boot, chỉ cần một Component hay Bean nào triển khai giao diện

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
71 thì trong quá trình xác thực, phương thức
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
72 của Component hay Bean đó sẽ được gọi ra khi cần tìm kiếm người dùng theo tên người dùng. Bản chất của cơ chế này là Auto Injection, khi quét [quét thành phần] và tải các Thành phần hay Bean vào Bối cảnh ứng dụng, Spring Boot đã biết rõ, thành phần nào được sử dụng để tìm kiếm tên người dùng theo tên người dùng thông qua kiểu Giao diện mà bạn không cần . Nếu có nhiều hơn một Component implements interface
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
71 thì sao?

7. Phân quyền tập trung hay phân quyền bằng chú thích?

Spring Security for the program 2 options

  1. Tập trung cấu hình phần quyền theo phương thức
    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
      @Bean
        //Cấu hình Password encoder. Cần phải có !
      public PasswordEncoder encoder[] {
        return new BCryptPasswordEncoder[];
      }
    
      @Override
        //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
      protected void configure[HttpSecurity http] throws Exception {
        http.authorizeRequests[]
          .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
          .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
      }
    }
    78 sử dụng
    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
      @Bean
        //Cấu hình Password encoder. Cần phải có !
      public PasswordEncoder encoder[] {
        return new BCryptPasswordEncoder[];
      }
    
      @Override
        //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
      protected void configure[HttpSecurity http] throws Exception {
        http.authorizeRequests[]
          .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
          .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
      }
    }
    79 hoặc
    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
      @Bean
        //Cấu hình Password encoder. Cần phải có !
      public PasswordEncoder encoder[] {
        return new BCryptPasswordEncoder[];
      }
    
      @Override
        //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
      protected void configure[HttpSecurity http] throws Exception {
        http.authorizeRequests[]
          .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
          .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
      }
    }
    90 cho từng đường dẫn
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
9
  1. Sử dụng chú thích để phân quyền tại từng phương thức hoặc bộ điều khiển
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
1Phân quyền tập trungPhần quyền sử dụng chú thích Nhìn toàn cảnh logic Phân quyềnPhân quyền tản mát theo từng phương thức, bộ điều khiểnKhi số lượng phương thức rất nhiều. Nhìn sẽ khá phức tạp. Đôi khi khó gỡ lỗi và dễ đọc và gỡ rối Phù hợp cấu hình chung cho cả trang web và các đường dẫn chính Phù hợp cấu hình chi tiết cho từng động từ HTTP và đường dẫn Không sử dụng Biểu thức ngôn ngữ mùa xuân Sử dụng Biểu thức ngôn ngữ mùa xuân và tùy biến trước, sau

Thật tuyệt vời là chúng ta có thể kết hợp cả 2 phương pháp này. Những logic cấu hình xác thực và những yêu cầu bảo mật chung như yêu cầu đăng nhập với yêu cầu đến đường dẫn

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
91 tôi luôn để trong hàm
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
78. Tôi chỉ sử dụng chú thích phân quyền khi quy định đó dành riêng cho phương thức đó mà không áp dụng được cho phương thức khác

8. Kiểm soát truy cập dựa trên biểu thức

Spring Security có tính năng Kiểm soát truy cập dựa trên biểu thức [kiểm soát truy cập theo dõi bằng biểu thức]. Để sử dụng tính năng này, cần bật

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
70 trong chú thích
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
94. Vui lòng xem thêm tài liệu chuẩn của Spring Boot về

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
6

Kiểm soát truy cập dựa trên biểu thức có sẵn một số chức năng rất hữu ích

Biểu thứcGiải thíchhasRole[Vai trò chuỗi]Cần có vai trò mới được thực thihasAnyRole[Chuỗi… vai trò]Cần có một trong những vai trò mới được thực thihasAuthority[Chuỗi quyền]Cần có quyền mới được thực thihasAnyAuthority[Chuỗi… quyền]Cần có một trong những vai trò này

9. Phân quyền ở cấp Controller

Ví dụ ở một REST Controller. APIControllerV2. java, chúng ta yêu cầu tối thiểu mọi người dùng đều phải đăng nhập. Sau đó với từng phương thức chi tiết sẽ có quyền kiểm tra riêng. Việc đặt

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
95 ở cấp độ Bộ điều khiển sẽ rất hợp lý

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
8

10. Phần quyền ở cấp độ phương thức

10. 1 @PreAuthorize kiểm tra trước khi chạy phương thức

Ví dụ kiểm tra quyền phải là ADMIN mới được truy cập

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
9

Viết kiểm tra logic bao gồm 2 điều kiện

  • Yêu cầu đăng nhập
    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
      @Bean
        //Cấu hình Password encoder. Cần phải có !
      public PasswordEncoder encoder[] {
        return new BCryptPasswordEncoder[];
      }
    
      @Override
        //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
      protected void configure[HttpSecurity http] throws Exception {
        http.authorizeRequests[]
          .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
          .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
      }
    }
    96
  • The value value of the path
    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
      @Bean
        //Cấu hình Password encoder. Cần phải có !
      public PasswordEncoder encoder[] {
        return new BCryptPasswordEncoder[];
      }
    
      @Override
        //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
      protected void configure[HttpSecurity http] throws Exception {
        http.authorizeRequests[]
          .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
          .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
      }
    }
    97 by with the login name
    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
      @Bean
        //Cấu hình Password encoder. Cần phải có !
      public PasswordEncoder encoder[] {
        return new BCryptPasswordEncoder[];
      }
    
      @Override
        //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
      protected void configure[HttpSecurity http] throws Exception {
        http.authorizeRequests[]
          .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
          .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
      }
    }
    98. Chú ý
    @Configuration
    @EnableWebSecurity
    @EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
      @Bean
        //Cấu hình Password encoder. Cần phải có !
      public PasswordEncoder encoder[] {
        return new BCryptPasswordEncoder[];
      }
    
      @Override
        //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
      protected void configure[HttpSecurity http] throws Exception {
        http.authorizeRequests[]
          .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
          .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
      }
    }
    99 là một biến mặc định mà Spring Security cung cấp để lấy thông tin về người dùng đang đăng nhập
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
4

10. 2 Tại sao không phải là @PreAuthorize["hasRole['ADMIN']"] mà lại là @PreAuthorize["hasAuthority['ADMIN']"]?

Trong dự án này, tôi chuyển 1. 1 từ Vai trò sang Quyền hạn. Người dùng tệp. java sàng thủ

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
10 write over a method. Đọc kỹ logic bạn sẽ thấy ứng với từng vai trò, tôi sẽ tạo ra một đối tượng
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
11. Sau khi chuyển sang cơ chế RBAC, bạn có thể viết lại, mỗi vai trò, trả về một tập tin thuộc thẩm quyền của vai trò đó

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
0

Nếu bạn muốn sử dụng

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
12, thì chuỗi mô tả tên vai trò sẽ phải có thêm tiền tố ROLE_

Tạo ROLE_ADMIN

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
1

Kiểm tra ROLE_ADMIN

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
2

10. 3 @PostAuthorize

Nếu như

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
5 kiểm tra quyền trước khi phương thức thực thi thì có những vấn đề quyết định cho phép truy cập tiếp hoặc không chỉ sau khi phương thức được thực thi. Ví dụ GET /post/{id} sẽ trả về bài viết. Tôi muốn rằng chỉ tác giả của chính bài viết đó mới có thể được xem để chỉnh sửa. Spring Boot has user. id đang đăng nhập, nhưng để biết tác giả của bài viết thì cần phải lấy bài viết đó từ cơ sở dữ liệu đã có.
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
6

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
3

10. 4 @PostFilter

Để lọc dữ liệu trước khi trả về, có 2 cách. để lập trình viên tự viết mã theo phương thức hoặc cấu hình bằng chú thích

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
7

Trong ví dụ này, phương thức

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
16 sẽ trả về một danh sách tất cả bài viết. Tôi muốn lọc ra chỉ trả về những bài viết do chính người dùng đang đăng nhập
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
99 viết mà thôi

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
4

10. 5 @Bộ lọc trước

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
18 lại cho phép lọc dữ liệu truyền vào phương thức trước khi thực thi. Tôi chưa có ví dụ thực tế nào để minh họa cho chú thích. Nhưng về cơ bản nó giống với
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity[prePostEnabled = true, securedEnabled = true, jsr250Enabled = true]
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  @Bean
    //Cấu hình Password encoder. Cần phải có !
  public PasswordEncoder encoder[] {
    return new BCryptPasswordEncoder[];
  }

  @Override
    //Cấu hình authentication theo đường dẫn, crsf, cors, session cookie hay stateless.
  protected void configure[HttpSecurity http] throws Exception {
    http.authorizeRequests[]
      .antMatchers["/h2-console/**"].permitAll[].and[].csrf[].ignoringAntMatchers["/h2-console/**"]
      .and[].headers[].frameOptions[].sameOrigin[].and[].formLogin[];
  }
}
7

11. Kết luận

Bạn đã đọc một bài viết rất dài và có hại. Tôi cũng phải mất 3 ngày toàn thời gian để mã ví dụ này, và thêm 1. 5 ngày nữa để viết ra những gì đã trải nghiệm sau khi đã đọc rất nhiều tài liệu liên quan. Một vài điểm bạn cần nhớ

Chủ Đề