OAuth2.0之OLTU實現舉例

一、場景

三個角色:用戶(user),web應用(client),資源服務器和授權服務器合為服務器(server)

用戶登錄登錄后可查看自己的信息

二、準備

2.1 數據庫

schema

drop table if exists oauth2_client;
drop table if exists oauth2_user;

create table oauth2_user (
  id bigint auto_increment,
  username varchar(100),
  password varchar(100),
  salt varchar(100),
  constraint pk_oauth2_user primary key(id)
) charset=utf8 ENGINE=InnoDB;
create unique index idx_oauth2_user_username on oauth2_user(username);

create table oauth2_client (
  id bigint auto_increment,
  client_name varchar(100),
  client_id varchar(100),
  client_secret varchar(100),
  constraint pk_oauth2_client primary key(id)
) charset=utf8 ENGINE=InnoDB;
create index idx_oauth2_client_client_id on oauth2_client(client_id);

data

DELIMITER ;
delete from oauth2_user;
delete from oauth2_client;

insert into oauth2_user values(1,'admin','d3c59d25033dbf980d29554025c23a75','8d78869f470951332959580424d4bf4f');
insert into oauth2_client values(1,'chapter17-client','c1ebe466-1cdc-4bd3-ab69-77c3561b9dee','d8346ea2-6017-43ed-ad68-19c0f971738b');

2.2 Server

zetark-oauth2-server

修改數據庫鏈接 resources.properties

#dataSource configure
connection.url=jdbc:mysql://mysql-server:3306/shiro
connection.username=r00t
connection.password=r00t

2.3 Client

zetark-oauth2-client

三、過程分析

image

1)2)用戶訪問client首頁,檢測到用戶未登錄,重定向到login

image

3)4)點擊授權登錄,輸入admin/123456后點擊登錄并授權按鈕

image
image
// 3)授權請求  http://localhost:8080/zetark-oauth2-server/oauth2login
if (!isLogin && servletPath.startsWith("/login_authorize")) {
    String authorizeUrl = ClientParams.OAUTH_SERVER_AUTHORIZE_URL;
    authorizeUrl += "?client_id=c1ebe466-1cdc-4bd3-ab69-77c3561b9dee";
    authorizeUrl += "&response_type=code";
    authorizeUrl += "&&redirect_uri=" + ClientParams.OAUTH_SERVER_REDIRECT_URI;
    response.sendRedirect(authorizeUrl);
    return;
}
// 4)授權響應
if (!isLogin && servletPath.startsWith("/login_response")) {
    String code = request.getParameter("code");
    if (code != null) {
        
        // 6)7)令牌請求及響應 http://localhost:8080/zetark-oauth2-server/accessToken
        OAuthAccessTokenResponse tokenResponse = null;
        try {
            tokenResponse = OauthClient.makeTokenRequestWithAuthCode(code);
        } catch (OAuthProblemException e) {
            e.printStackTrace();
        } catch (OAuthSystemException e) {
            e.printStackTrace();
        }
        if (tokenResponse != null) {
            session.setAttribute("isLogin", true);
            session.setAttribute("token", tokenResponse.getAccessToken());
            session.setMaxInactiveInterval(tokenResponse.getExpiresIn().intValue());
            // 10)11) 根據token調用api
             String userInfoJson = OauthClient.getAuthedService(tokenResponse.getAccessToken());
            Map<String, Object> userInfo = new Gson().fromJson(userInfoJson, Map.class);
            System.out.println(userInfo);
            session.setAttribute("user", userInfo);
            response.sendRedirect("index");
            return;
        }
    } else {
        String errorDesc = request.getParameter("error_description");
        System.out.println("登錄失敗:" + errorDesc);
    }
}

訪問過程

image
client_uri:/
client_uri:/login
# 用戶訪問client首頁/,由于未登錄被重定向到/login頁面

client_uri:/login_authorize
server_uri:/oauth2login
# 用戶在/login頁面點擊授權登錄后,向server發起授權請求,server返回登錄頁面/oauth2login

server_uri:/authorize
client_uri:/login_response
# 用戶在/oauth2login填寫用戶名密碼后點擊授權登錄后,server驗證后重定向到/login_resposne

server_uri:/accessToken
server_uri:/checkAccessToken
# client在處理/login_response時接收code并再發起令牌請求,server返回令牌

server_uri:/v1/openapi/userInfo
# client根據令牌信息請求api服務

client_uri:/index
# 向用戶返回/index頁面

四、參考

https://github.com/ameizi/oltu-oauth2-example

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,933評論 18 139
  • 1. 引言 周末逛簡書,看了一篇寫的極好的文章,點擊大紅心點贊,就直接給我跳轉到登錄界面了,原來點贊是需要登錄的。...
    圣杰閱讀 4,417評論 3 83
  • 有時候,面對老師布置的周末親子活動會略感煩躁,有點有失家長風范,不可置否。心態很重要,充分理解,陪伴孩子是多...
    奈乖閱讀 567評論 0 50
  • 前言 這個系列是java spring mvc 源碼閱讀與分析的一個系列閱讀源碼分支為 spring初始化流程 ...
    金發萌音閱讀 823評論 0 1
  • 1.鏈家專享15個點,其他公司沒有15個點。 2.順帶解釋下,點數一下放完的原因。 3.強調房子馬上漲價,還有樓盤...
    6eae61b1a4e8閱讀 167評論 0 0