OAuth2簡易實戰(一)-四種模式

1. OAuth2簡易實戰(一)-四種模式

1.1. 受權碼受權模式(Authorization code Grant)

1.1.1. 流程圖

1.1.2. 受權服務器配置

  1. 配置受權服務器中 client,secret,redirectUri,受權模式,權限配置
//受權服務器配置
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServer extends
        AuthorizationServerConfigurerAdapter {

    @Override
    public void configure(ClientDetailsServiceConfigurer clients)
            throws Exception {
        clients.inMemory()
            .withClient("clientapp")
            .secret("112233")
            .redirectUris("http://localhost:9001/callback")
            // 受權碼模式
            .authorizedGrantTypes("authorization_code")
            .scopes("read_userinfo", "read_contacts");
    }

}

1.1.3. 資源服務器配置

  1. 配置須要資源受權的接口地址
//資源服務配置
@Configuration
@EnableResourceServer
public class OAuth2ResourceServer extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .anyRequest()
            .authenticated()
        .and()
            .requestMatchers()
            .antMatchers("/api/**");
    }

}

1.1.4. 操做步驟

  1. 瀏覽器請求下列地址,獲取受權code,請求參數client_id,redirect_uri回調地址,response_type響應類型,scope權限
http://localhost:8080/oauth/authorize?client_id=clientapp&redirect_uri=http://localhost:9001/callback&response_type=code&scope=read_userinfo
  1. 輸入用戶名密碼,該密碼爲Spring Security的登路密碼,application.properties裏配置

# Spring Security Setting
security.user.name=bobo
security.user.password=xyz
  1. 登錄後顯示

  1. 選擇Approve,點擊Authorize,會調用回調地址並返回code參數

  1. 在得到受權碼後,接下去獲取訪問令牌,訪問
http://localhost:8080/oauth/token?code=ghN0hF&grant_type=authorization_code&redirect_uri=http://localhost:9001/callback&scope=read_userinfo

注意:須要在headers裏添加認證

認證參數就是受權服務器配置的client和secrethtml

  1. 獲取token後訪問
http://localhost:8080/api/userinfo?access_token=f4345f3a-34a3-4887-bc02-e95150c54bf4

若是token錯誤,則
git

1.1.5. 使用場景

  1. 受權碼模式是最多見的一種受權模式,在oauth2.0內是最安全和最完善的。
  2. 適用於全部有Server端的應用,如Web站點、有Server端的手機客戶端。
  3. 能夠獲得較長期限受權。

1.2. 隱式受權模式(Implicit Grant)

1.2.1. 流程圖

1.2.2. 改動 authorizedGrantTypes

@Configuration
@EnableAuthorizationServer
public class OAuth2AuthoriationServer extends AuthorizationServerConfigurerAdapter{
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

        clients.inMemory()
                .withClient("clientapp")
                .secret("112233")
                .accessTokenValiditySeconds(60)
                .redirectUris("http://localhost:9001/callback")
                .authorizedGrantTypes("implicit")
                .scopes("admin", "visitor");
    }
}

1.2.3. 操做步驟

  1. 申請受權token,參數和申請受權碼相似,client_id,redirect_uri回調地址,response_type有變更,改成直接獲取token,scope權限,state用於認證標記,傳過去什麼回調時傳回來什麼
http://localhost:8080/oauth/authorize?client_id=clientapp&redirect_uri=http://localhost:9001/callback&response_type=token&scope=admin&state=abc

  1. 操做同上,輸入密碼跳轉認證確認,選Approve後點Authorize,跳轉

  1. 能夠看到直接返回了access_token,state也是原樣返回
  2. 以後按受權碼模式第六步操做,把access_token參數帶上,進行接口調用就能夠了

1.2.4. 使用場景

  1. 適用於全部無Server端配合的應用
  2. 如手機/桌面客戶端程序、瀏覽器插件。
  3. 基於JavaScript等腳本客戶端腳本語言實現的應用。

注意:由於Access token是附着在 redirect_uri 上面被返回的,因此這個 Access token就可能會暴露給資源全部者或者設置內的其它方(對資源全部者來講,能夠看到redirect_uri,對其它方來講,能夠經過監測瀏覽器的地址變化來獲得 Access token)。github

1.3. 密碼模式(Resource Owner Password Credentials Grant)

1.3.1. 流程圖

1.3.2. 改動

  1. 受權服務器配置,須要添加用戶認證管理端點authenticationManager,修改模式authorizedGrantTypes爲password
// 受權服務器配置
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthoriationServer extends AuthorizationServerConfigurerAdapter{
    
    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.authenticationManager(authenticationManager);
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

        clients.inMemory()
                .withClient("clientapp")
                .secret("112233")
                .accessTokenValiditySeconds(60)
                .redirectUris("http://localhost:9001/callback")
                .authorizedGrantTypes("password")
                .scopes("admin", "visitor");
    }
}

1.3.3. 操做步驟

  1. 調用如下連接,向客戶端和服務器提供用戶名密碼
http://localhost:8080/oauth/token?password=123456&grant_type=password&username=lll&scope=admin

注意:和受權碼模式同樣,須要在headers裏添加認證spring

  1. 結果:
    api

  2. 獲取token後,步驟同1.1和1.2模式瀏覽器

1.3.4. 使用場景

  1. 這種模式適用於用戶對應用程序高度信任的狀況。好比是用戶操做系統的一部分。
  2. 認證服務器只有在其餘受權模式沒法執行的狀況下,才能考慮使用這種模式。

1.4. 客戶端憑證模式(Client Credentials Grant)

1.4.1. 流程圖

1.4.2. 改動

  1. 只需修改受權服務器,authorizedGrantTypes類型client_credentials

1.4.3. 操做步驟

http://localhost:8080/oauth/token?grant_type=client_credentials&scope=admin

  1. 能夠看到客戶端憑證模式也須要在header裏添加認證帳戶密碼
  2. 得到token後操做同上

1.4.4. 使用場景

  1. 客戶端模式應用於應用程序想要以本身的名義與受權服務器以及資源服務器進行互動。
  2. 例如使用了第三方的靜態文件服務

1.5. 刷新TOKEN

1.5.1. 流程圖

1.5.2. 改動

1.5.3. 操做步驟

  1. 以受權碼模式爲例,步驟同受權碼模式,取得受權碼後,去取token時,返回

  1. 在token過時後,調用
http://localhost:8080/oauth/token?grant_type=refresh_token&refresh_token=ad3941d1-c6dd-4a2e-a9c8-eac6a9a59dd2

  1. 返回
  2. 就能夠拿新的access_token繼續調用了
  3. 建議將access_token和refresh_token的過時時間保存下來,每次調用平臺方的業務api前先對access_token和refresh_token進行一下時間判斷,若是過時則執行刷新access_token或從新受權操做。refersh_token若是過時就只能讓用戶從新受權。

參考 https://www.cnblogs.com/maoxiaolv/p/5838680.html安全

代碼學習地址 https://github.com/spring2go/oauth2lab服務器

相關文章
相關標籤/搜索