亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring Security實(shí)現(xiàn)不同接口安全策略方法詳解

 更新時間:2020年09月04日 09:40:48   作者:碼農(nóng)小胖哥  
這篇文章主要介紹了Spring Security實(shí)現(xiàn)不同接口安全策略方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

1. 前言

歡迎閱讀 Spring Security 實(shí)戰(zhàn)干貨 系列文章 。最近有開發(fā)小伙伴提了一個有趣的問題。他正在做一個項(xiàng)目,涉及兩種風(fēng)格,一種是給小程序出接口,安全上使用無狀態(tài)的JWT Token;另一種是管理后臺使用的是Freemarker,也就是前后端不分離的Session機(jī)制。用Spring Security該怎么辦?

2. 解決方案

我們可以通過多次繼承WebSecurityConfigurerAdapter構(gòu)建多個HttpSecurity。HttpSecurity 對象會告訴我們?nèi)绾悟?yàn)證用戶的身份,如何進(jìn)行訪問控制,采取的何種策略等等。

我們是這么配置的:

/**
 * 單策略配置
 *
 * @author felord.cn
 * @see org.springframework.boot.autoconfigure.security.servlet.SpringBootWebSecurityConfiguration
 * @since 14 :58 2019/10/15
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, jsr250Enabled = true, securedEnabled = true)
@EnableWebSecurity
@ConditionalOnClass(WebSecurityConfigurerAdapter.class)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class CustomSpringBootWebSecurityConfiguration {

  /**
   * The type Default configurer adapter.
   */
  @Configuration
  @Order(SecurityProperties.BASIC_AUTH_ORDER)
  static class DefaultConfigurerAdapter extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
      super.configure(auth);
    }

    @Override
    public void configure(WebSecurity web) {
      super.configure(web);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
      // 配置 httpSecurity

    }
  }
}

上面的配置了一個HttpSecurity,我們?nèi)绶ㄅ谥圃僭黾右粋€WebSecurityConfigurerAdapter的子類來配置另一個HttpSecurity。伴隨而來的還有不少的問題要解決。

2.1 如何路由不同的安全配置
我們配置了兩個HttpSecurity之后,程序如何讓小程序接口和后臺接口走對應(yīng)的HttpSecurity?

HttpSecurity.antMatcher(String antPattern)可以提供過濾機(jī)制。比如我們配置:

   @Override
    protected void configure(HttpSecurity http) throws Exception {
      // 配置 httpSecurity
      http.antMatcher("/admin/v1");

    }

那么該HttpSecurity將只提供給以/admin/v1開頭的所有URL。這要求我們針對不同的客戶端指定統(tǒng)一的URL前綴。

舉一反三只要HttpSecurity提供的功能都可以進(jìn)行個性化定制。比如登錄方式,角色體系等。

2.2 如何指定默認(rèn)的 HttpSecurity

我們可以通過在WebSecurityConfigurerAdapter實(shí)現(xiàn)上使用@Order注解來指定優(yōu)先級,數(shù)值越大優(yōu)先級越低,沒有@Order注解將優(yōu)先級最低。

2.3 如何配置不同的 UserDetailsService

很多情況下我們希望普通用戶和管理用戶完全隔離,我們就需要多個UserDetailsService,你可以在下面的方法中對AuthenticationManagerBuilder進(jìn)行具體的設(shè)置來配置UserDetailsService,同時也可以配置不同的密碼策略。

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
  daoAuthenticationProvider.setUserDetailsService(new UserDetailsService() {
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
      // 自行實(shí)現(xiàn)
      return null ;
    }
  });
  // 也可以設(shè)計(jì)特定的密碼策略
  BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
  daoAuthenticationProvider.setPasswordEncoder(bCryptPasswordEncoder);
  auth.authenticationProvider(daoAuthenticationProvider);
}

2.4 最終的配置模板

上面的幾個問題解決之后,我們基本上掌握了在一個應(yīng)用中執(zhí)行多種安全策略。配置模板如下:

/**
 * 多個策略配置
 *
 * @author felord.cn
 * @see org.springframework.boot.autoconfigure.security.servlet.SpringBootWebSecurityConfiguration
 * @since 14 :58 2019/10/15
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, jsr250Enabled = true, securedEnabled = true)
@EnableWebSecurity
@ConditionalOnClass(WebSecurityConfigurerAdapter.class)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class CustomSpringBootWebSecurityConfiguration {

  /**
   * 后臺接口安全策略. 默認(rèn)配置
   */
  @Configuration
  @Order(1)
  static class AdminConfigurerAdapter extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
      DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
      //用戶詳情服務(wù)個性化
      daoAuthenticationProvider.setUserDetailsService(new UserDetailsService() {
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
          // 自行實(shí)現(xiàn)
          return null;
        }
      });
      // 也可以設(shè)計(jì)特定的密碼策略
      BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
      daoAuthenticationProvider.setPasswordEncoder(bCryptPasswordEncoder);
      auth.authenticationProvider(daoAuthenticationProvider);
    }

    @Override
    public void configure(WebSecurity web) {
      super.configure(web);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
      // 根據(jù)需求自行定制
      http.antMatcher("/admin/v1")
          .sessionManagement(Customizer.withDefaults())
          .formLogin(Customizer.withDefaults());

    }
  }

  /**
   * app接口安全策略. 沒有{@link Order}注解優(yōu)先級比上面低
   */
  @Configuration
  static class AppConfigurerAdapter extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
      DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
      //用戶詳情服務(wù)個性化
      daoAuthenticationProvider.setUserDetailsService(new UserDetailsService() {
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
          // 自行實(shí)現(xiàn)
          return null;
        }
      });
      // 也可以設(shè)計(jì)特定的密碼策略
      BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
      daoAuthenticationProvider.setPasswordEncoder(bCryptPasswordEncoder);
      auth.authenticationProvider(daoAuthenticationProvider);
    }

    @Override
    public void configure(WebSecurity web) {
      super.configure(web);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
      // 根據(jù)需求自行定制
      http.antMatcher("/app/v1")
          .sessionManagement(Customizer.withDefaults())
          .formLogin(Customizer.withDefaults());

    }
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • "Method?Not?Allowed"405問題分析以及解決方法

    "Method?Not?Allowed"405問題分析以及解決方法

    項(xiàng)目中在提交表單時,提示“HTTP 405”錯誤——“Method Not Allowed”這里顯示的是,方法不被允許,下面這篇文章主要給大家介紹了關(guān)于"Method?Not?Allowed"405問題分析以及解決方法的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • javaWeb自定義標(biāo)簽用法實(shí)例詳解

    javaWeb自定義標(biāo)簽用法實(shí)例詳解

    這篇文章主要介紹了javaWeb自定義標(biāo)簽用法,結(jié)合實(shí)例形式分析了javaweb自定義標(biāo)簽的功能、定義方法及執(zhí)行原理,需要的朋友可以參考下
    2017-04-04
  • Open?Feign之非SpringCloud方式使用示例

    Open?Feign之非SpringCloud方式使用示例

    這篇文章主要為大家介紹了Open?Feign之非SpringCloud方式使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • 如何讓java只根據(jù)數(shù)據(jù)庫表名自動生成實(shí)體類

    如何讓java只根據(jù)數(shù)據(jù)庫表名自動生成實(shí)體類

    今天給大家?guī)淼闹R是關(guān)于Java的,文章圍繞著如何讓java只根據(jù)數(shù)據(jù)庫表名自動生成實(shí)體類展開,文中有非常詳細(xì)的介紹,需要的朋友可以參考下
    2021-06-06
  • 通過實(shí)例了解java spring使用構(gòu)造器注入的原因

    通過實(shí)例了解java spring使用構(gòu)造器注入的原因

    這篇文章主要介紹了通過實(shí)例了解spring使用構(gòu)造器注入的原因,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Java中StringBuilder常用構(gòu)造方法解析

    Java中StringBuilder常用構(gòu)造方法解析

    這篇文章主要介紹了Java中StringBuilder常用構(gòu)造方法解析,StringBuilder是一個可標(biāo)的字符串類,我們可以吧它看成是一個容器這里的可變指的是StringBuilder對象中的內(nèi)容是可變的,需要的朋友可以參考下
    2024-01-01
  • Java常用時間工具類總結(jié)(珍藏版)

    Java常用時間工具類總結(jié)(珍藏版)

    這篇文章主要為大家詳細(xì)介紹了Java中一些常用時間工具類的使用示例代碼,文中的代碼簡潔易懂,對我們學(xué)習(xí)Java有一定幫助,需要的可以參考一下
    2022-07-07
  • Java多線程開發(fā)工具之CompletableFuture的應(yīng)用詳解

    Java多線程開發(fā)工具之CompletableFuture的應(yīng)用詳解

    做Java編程,難免會遇到多線程的開發(fā),但是JDK8這個CompletableFuture類很多開發(fā)者目前還沒聽說過,但是這個類實(shí)在是太好用了,本文就來聊聊它的應(yīng)用吧
    2023-03-03
  • Java實(shí)現(xiàn)基于token認(rèn)證的方法示例

    Java實(shí)現(xiàn)基于token認(rèn)證的方法示例

    這篇文章主要介紹了Java實(shí)現(xiàn)基于token認(rèn)證的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • 基于Spring Boot的Logback日志輪轉(zhuǎn)配置詳解

    基于Spring Boot的Logback日志輪轉(zhuǎn)配置詳解

    本篇文章主要介紹了基于Spring Boot的Logback日志輪轉(zhuǎn)配置詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10

最新評論