日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

全新Spring Security安全管理配置使用詳解

來源: 責編: 時間:2024-02-02 17:00:41 239觀看
導讀環境:SpringBoot2.7.12 + JDK211. 簡介Spring Security 是一個提供身份驗證、授權和防護常見攻擊的框架。它為確保命令式和反應式應用程序的安全提供一流的支持,是確保基于 Spring 的應用程序安全的事實標準。Spring Sc

環境:SpringBoot2.7.12 + JDK211EP28資訊網——每日最新資訊28at.com

1. 簡介

Spring Security 是一個提供身份驗證、授權和防護常見攻擊的框架。它為確保命令式和反應式應用程序的安全提供一流的支持,是確保基于 Spring 的應用程序安全的事實標準。1EP28資訊網——每日最新資訊28at.com

Spring Scurity核心分為2大模塊:1EP28資訊網——每日最新資訊28at.com

  1. 認證(Authentication):認證是建立一個他聲明的主體的過程(一個主體一般是指用戶、設備或一些可以在你的應用程序中執行的其他系統)。常見的身份認證一般要求用戶提供用戶名和密碼。系統通過校驗用戶名和密碼來完成認證過程。
  2. 授權(Authorization):當身份認證通過后,去訪問系統的資源,系統會判斷用戶是否擁有訪問該資源的權限,只允許訪問有權限的系統資源,沒有權限的資源將無法訪問,這個過程叫用戶授權。比如會員管理模塊有增刪改查功能,有的用戶只能進行查詢,而有的用戶可以進行修改、刪除。一般來說,系統會為不同的用戶分配不同的角色,而每個角色則對應一系列的權限。

從Spring Security5.7開始之前的安全配置方式及Web授權處理方式發生了變化,接下來將詳細介紹配置的變化。1EP28資訊網——每日最新資訊28at.com

2. 安全配置

2.1 配置方式

在5.7之前的版本自定義安全配置通過如下方式:1EP28資訊網——每日最新資訊28at.com

@Componentpublic class SecurityConfig extends WebSecurityConfigurerAdapter {  protected void configure(HttpSecurity http) throws Exception {    // ...  }}

在5.7之后推薦如下配置方式1EP28資訊網——每日最新資訊28at.com

@Configurationpublic class SecurityConfig {  @Bean  @Order(Ordered.HIGHEST_PRECEDENCE)  SecurityFilterChain controllerFilterChain(HttpSecurity http) throws Exception {    // ...    return http.build() ;  }}

通過配置安全過濾器鏈的方式配置,具體內部的配置細節還都是圍繞著HttpSecurity進行配置。1EP28資訊網——每日最新資訊28at.com

2.2 配置不同的過濾器鏈

在一個配置文件中我們可以非常方便的配置多個過濾器鏈,針對不同的請求進行攔截。1EP28資訊網——每日最新資訊28at.com

@Configurationpublic class SecurityConfig {  @Bean  @Order(1)  SecurityFilterChain controllerFilterChain(HttpSecurity http) {    // 當前過濾器鏈只對/demos/**, /login, /logout進行攔截    http.requestMatchers(matchers -> matchers.antMatchers("/demos/**", "/login", "/logout")) ;    http.authorizeHttpRequests().antMatchers("/**").authenticated() ;    http.formLogin(Customizer.withDefaults()) ;    // ...    return http.build() ;  }  @Bean  @Order(2)  SecurityFilterChain managementSecurityFilterChain(HttpSecurity http) throws Exception {    // 該過濾器只會對/ac/**的請求進行攔截    http.requestMatchers(matchers -> matchers.antMatchers("/ac/**")) ;    // ...    http.formLogin(Customizer.withDefaults());    return http.build();  }}

以上配置了2個過濾器鏈,根據配置的@Order值,優先級分別:controllerFilterChain,managementSecurityFilterChain。當訪問除上面指定的uri模式以為的請求都將自動放行。1EP28資訊網——每日最新資訊28at.com

2.3 用戶驗證配置

在5.7版本之前,我們通過如下配置配置內存用戶1EP28資訊網——每日最新資訊28at.com

public class SecurityConfig extends WebSecurityConfigurerAdapter {  @Override  protected void configure(AuthenticationManagerBuilder auth) throws Exception {    auth.inMemoryAuthentication()      .passwordEncoder(NoOpPasswordEncoder.getInstance())      .withUser("admin")      .password("123123")      .authorities(Arrays.asList(new SimpleGrantedAuthority("ROLE_ADMIN"))) ;  }}

5.7 只有由于推薦的是通過自定義SecurityFilterChain的方式,所以我們需要通過如下的方式進行配置:1EP28資訊網——每日最新資訊28at.com

@Configurationpublic class SecurityConfig {  @Bean  @Order(0)  SecurityFilterChain controllerFilterChain(HttpSecurity http) throws Exception {    AuthenticationManagerBuilder builder = http.getSharedObject(AuthenticationManagerBuilder.class) ;    builder.inMemoryAuthentication()      .passwordEncoder(NoOpPasswordEncoder.getInstance())      .withUser("admin")      .password("123123")      .authorities(Arrays.asList(new SimpleGrantedAuthority("ROLE_ADMIN"))) ;    // ...  }}

2.4 授權方式

在5.7之后推薦配置認證授權的方式如下1EP28資訊網——每日最新資訊28at.com

@Beanpublic SecurityFilterChain apiSecurityFilterChain(HttpSecurity http) throws Exception {  http.authorizeHttpRequests().antMatchers("/users/**").hasAnyRole("ADMIN") ;  // ...  return http.build() ;}

通過上面的authorizeHttpRequests方式進行授權配置,會向過濾器鏈中添加AuthorizationFilter過濾器。在該過濾器中會進行權限的驗證。1EP28資訊網——每日最新資訊28at.com

2.5 自定義授權決策

如果需要對請求的uri進行更加精確的匹配驗證,如:/users/{id},需要驗證只有這里的id值為666才方向。1EP28資訊網——每日最新資訊28at.com

@Beanpublic SecurityFilterChain apiSecurityFilterChain(HttpSecurity http) throws Exception {  http.authorizeHttpRequests(registry -> {    registry.antMatchers("/users/{id}").access(new AuthorizationManager<RequestAuthorizationContext>() {      @Override      public AuthorizationDecision check(Supplier<Authentication> authentication,          RequestAuthorizationContext object)        // 獲取路徑上的值信息,其中key=id,value=xxx        Map<String, String> variables         // 這里的第一個參數是boolean,確定了授權是否通過        return new AuthorityAuthorizationDecision(variables.get("id").equals("666"), Arrays.asList(new SimpleGrantedAuthority("D"))) ;      }    }) ;  }) ;}

2.6 全局認證

如果配置了多個不同的SecurityFilterChain,而每個認證都使用相同的用戶體系,那么我們可以定義AuthenticationProvider或者UserDetailsService 類型的Bean即可。1EP28資訊網——每日最新資訊28at.com

@BeanUserDetailsService userDetailsService() {  return new UserDetailsService() {    @Override    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {      return null;    }  } ;}@BeanAuthenticationProvider authenticationProvider() {  return new AuthenticationProvider() {    @Override    public Authentication authenticate(Authentication authentication) throws AuthenticationException {      return null;    }    @Override    public boolean supports(Class<?> authentication) {      return false;    }  } ;}


1EP28資訊網——每日最新資訊28at.com

以上是本篇文章的全部內容, 希望對你有所幫助。1EP28資訊網——每日最新資訊28at.com

完畢!!!1EP28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-71943-0.html全新Spring Security安全管理配置使用詳解

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 我們一起聊聊如何提高API性能的綜合策略

下一篇: Nodejs - 九步開啟JWT身份驗證

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 法库县| 平邑县| 青神县| 五台县| 渑池县| 和硕县| 开鲁县| 东台市| 宣化县| 石嘴山市| 凤台县| 华阴市| 高碑店市| 肥城市| 阿鲁科尔沁旗| 丽水市| 年辖:市辖区| 平度市| 威宁| 安义县| 通化市| 鄱阳县| 肇东市| 舞钢市| 栾川县| 土默特右旗| 合水县| 清水县| 灵武市| 吉水县| 远安县| 休宁县| 福安市| 饶河县| 曲松县| 高碑店市| 防城港市| 调兵山市| 民和| 西华县| 体育|