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

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美日韩国产一区二区三区不卡 | 高h全肉np触手| 九色PORNY真实丨国产免费 | 久久99国产精品二区不卡 | 大学生按摩黄a级中文片 | 男人天堂色 | 99九九成人免费视频精品 | 亚洲精品一二区 | 青草午夜精品视频在线观看 | 欧美三级小说 | 国产免费看黄的私人影院 | 亚洲人成影院午夜网站 | 日本不卡在线观看免费v | 热久久免费视频 | 国产精品久久久久久久久久久威 | 热99re久久精品精品免费 | 性导航h | 色先锋影音先锋 | 免费370理论片中文字幕 | a级影视 | 四虎成人免费观看在线网址 | 国产一区二区三区毛片 | 包射屋 | 国产乱码免费卡1卡二卡3卡四 | 五月婷婷在线免费观看 | 暖暖免费观看高清在线 | 免费在线公开视频 | 偷偷狠狠的日日高清完整视频 | 青青久久久| 狠狠涩| 国产精品二区高清在线 | 日本不卡不码高清免费观看 | 国产亚洲精品九九久在线观看 | 娇喘嗯嗯 轻点啊视频福利 九九九九在线精品免费视频 | 996免费视频国产在线播放 | 91噜噜噜在线观看 | 国产成人影院一区二区 | 91禁漫| 欧美一区二区三 | se01在线看片 | 免费的强动漫人物的 |