一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解使用Spring Security OAuth 實現OAuth 2.0 授權

詳解使用Spring Security OAuth 實現OAuth 2.0 授權

2021-03-19 10:47liuyatao Java教程

本篇文章主要介紹了詳解使用Spring Security OAuth 實現OAuth 2.0 授權,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

oauth 2.0 是一種工業級的授權協議。oauth 2.0是從創建于2006年的oauth 1.0繼承而來的。oauth 2.0致力于幫助開發者簡化授權并為web應用、桌面應用、移動應用、嵌入式應用提供具體的授權流程。

oauth 2.0 is the industry-standard protocol for authorization. oauth 2.0 supersedes the work done on the original oauth protocol created in 2006. oauth 2.0 focuses on client developer simplicity while providing specific authorization flows for web applications, desktop applications, mobile phones, and living room devices.

oauth 2.0的四個角色

為了方便理解,以常用的 使用微信登錄 為例

resource owner

資源擁有者,對應微信的每個用戶微信上設置的個人信息是屬于每個用戶的,不屬于騰訊。

resource server

資源服務器,一般就是用戶數據的一些操作(增刪改查)的rest api,比如微信的獲取用戶基本信息的接口。

client application

第三方客戶端,對比微信中就是各種微信公眾號開發的應用,第三方應用經過 認證服務器 授權后即可訪問 資源服務器 的rest api來獲取用戶的頭像、性別、地區等基本信息。

authorization server

認證服務器,驗證第三方客戶端是否合法。如果合法就給客戶端頒布token,第三方通過token來調用資源服務器的api。

四種授權方式(grant type)

anthorization_code

授權碼類型,適用于web server application。模式為:客戶端先調用 /oauth/authorize/ 進到用戶授權界面,用戶授權后返回 code ,客戶端然后根據code和 appsecret 獲取 access token 。

implicit簡化類型,相對于授權碼類型少了授權碼獲取的步驟。客戶端應用授權后認證服務器會直接將access token放在客戶端的url。客戶端解析url獲取token。這種方式其實是不太安全的,可以通過 https安全通道 和 縮短access token的有效時間 來較少風險。

password

密碼類型,客戶端應用通過用戶的username和password獲access token。適用于資源服務器、認證服務器與客戶端具有完全的信任關系,因為要將用戶要將用戶的用戶名密碼直接發送給客戶端應用,客戶端應用通過用戶發送過來的用戶名密碼獲取token,然后訪問資源服務器資源。比如支付寶就可以直接用淘寶用戶名和密碼登錄,因為它們屬于同一家公司,彼此 充分信任 。

client_credentials

客戶端類型,是不需要用戶參與的一種方式,用于不同服務之間的對接。比如自己開發的應用程序要調用短信驗證碼服務商的服務,調用地圖服務商的服務、調用手機消息推送服務商的服務。當需要調用服務是可以直接使用服務商給的 appid 和 appsecret 來獲取token,得到token之后就可以直接調用服務。

其他概念

  1. scope :訪問資源服務器的哪些作用域。
  2. refresh token :當access token 過期后,可以通過refresh token重新獲取access token。

實現

有的時候資源服務器和認證服務器是兩個不同的應用,有的時候資源服務器和認證服務器在通一個應用中,不同之處在于資源服務器是否需要檢查token的有效性,前者需要檢查,后者不需要。這里實現后者。

application的安全配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@configuration
public class securityconfiguration extends websecurityconfigureradapter {
 
  @override
  protected void configure(httpsecurity http) throws exception {
    http.formlogin()
        .and().csrf().disable()
        .authorizerequests().anyrequest().authenticated();
  }
 
  @override
  public void configure(websecurity web) throws exception {
    super.configure(web);
  }
 
  @override
  protected void configure(authenticationmanagerbuilder auth) throws exception {
    auth.inmemoryauthentication().withuser("lyt").password("lyt").authorities("role_user")
        .and().withuser("admin").password("admin").authorities("role_admin");
  }
 
  @bean
  @override
  public authenticationmanager authenticationmanagerbean() throws exception {
    return super.authenticationmanagerbean();
  }
}

認證服務器配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@enableauthorizationserver
@configuration
public class authorizationserverconfiguration extends authorizationserverconfigureradapter {
 
  @override
  public void configure(clientdetailsserviceconfigurer clients) throws exception {
    clients.inmemory().withclient("client")
        .scopes("read","write")
        .secret("secret")
        .authorizedgranttypes("authorization_code","password","implicit","client_credentials");}
 
  @override
  public void configure(authorizationserversecurityconfigurer security) throws exception {
    super.configure(security);
  }
 
  @override
  public void configure(authorizationserverendpointsconfigurer endpoints) throws exception {
    endpoints.authenticationmanager(authenticationmanager);
  }
 
  @autowired
  @qualifier("authenticationmanagerbean")
  private authenticationmanager authenticationmanager;
}

資源服務器配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@enableglobalmethodsecurity(prepostenabled = true)
@enableresourceserver
@configuration
public class resourceserverconfiguration extends resourceserverconfigureradapter {
 @override
 public void configure(httpsecurity http) throws exception {
 http.antmatcher("/oauth2/api/**").authorizerequests()
  .antmatchers(httpmethod.get, "/read/**").access("#oauth2.hasscope('read')")
  .antmatchers(httpmethod.post, "/write/**").access("#oauth2.hasscope('write')")
  .antmatchers(httpmethod.put, "/write/**").access("#oauth2.hasscope('write')")
  .antmatchers(httpmethod.delete, "/write/**").access("#oauth2.hasscope('write')");
 }
 
}

資源服務器 filter-order 設置

需要在 application.yml 中將filter-order設置成3,具體原因參考鏈接

防止cookie沖突

為了避免認證服務器的cookie和客戶端的cookie沖突,出現錯誤,最好修改 cookie name 或者設置 contextpath 。

測試

postman 中提供oauth 2.0的認證方式,可以獲取到token之后再把認證加入http請求中,即可請求資源服務器的rest api

客戶端信息

詳解使用Spring Security OAuth 實現OAuth 2.0 授權

授權

詳解使用Spring Security OAuth 實現OAuth 2.0 授權

獲取的token

詳解使用Spring Security OAuth 實現OAuth 2.0 授權

訪問資源服務器api

詳解使用Spring Security OAuth 實現OAuth 2.0 授權

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://juejin.im/post/5a580e726fb9a01caf3757a1

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 无套日出白浆在线播放 | 久久精品中文字幕 | 国产精品视频第一区二区三区 | 午夜精品网站 | 精品国产免费第一区二区三区日韩 | 天堂成人在线视频 | 网址在线观看你懂我意思吧免费的 | 国产小嫩模好紧 | 99精品视频免费 | 色先锋影音资源 | 亚洲人成网站在线观看90影院 | 大胆人gogo888体艺术在线 | 香港日本三级亚洲三级 | 久久这里只有精品无码3D | 校草让我脱了内裤给全班看 | 日本美女视频韩国视频网站免费 | 欧美一区不卡二区不卡三区 | 韩国久久精品 | 黑人biglackon10十 | 亚洲色图综合网 | 2022国产麻豆剧传媒古装 | 国产午夜亚洲精品理论片不卡 | 99精品视频只99有精品 | 色无月| 亚洲精品第五页中文字幕 | 夫妻性生活影院 | 学校捏奶揉下面污文h | 男人的私人影院 | 成人国产一区 | www.色呦呦.com | 日本视频高清免费观看xxx | 欧美a级在线观看 | 男人天堂网址 | 国产在线欧美日韩精品一区二区 | 无码任你躁久久久久久久 | 特黄视频 | 久久中文字幕乱码免费 | 色综合网亚洲精品久久 | 男女乱淫真视频播放网站 | 四虎影院在线免费 | 1986葫芦兄弟全集免费观看第十集 |