一、场景

三个角色:用户(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

三、过程分析

1)2)用户访问client首页,检测到用户未登录,重定向到login

3)4)点击授权登录,输入admin/123456后点击登录并授权按钮

// 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);
}
}

访问过程

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

最新文章

  1. Eclipse取消设置项目默认空间
  2. flash性能优化方案整理(最全)
  3. selenium python (一) 开发环境搭建
  4. linux系统目录架构
  5. .Net Core 学习 (1) - ASP.NET Core 总览
  6. CentOS虚拟机不能联网状况下yum方式从本地安装软件包(转载的)
  7. ASP.NET文件上传和下载
  8. form表单直接传文件
  9. IIS下访问网络驱动器(网络位置)
  10. Android - Fragment(二)加载Fragment
  11. Kinect2.0 MultiSourceFrameReader 的 AcquireLatestFrame 方法获取不到帧的解决方案
  12. 【自然语言处理篇】--以NLTK为基础讲解自然语⾔处理的原理和基础知识
  13. 命名自我规约manual
  14. Nginx防压力测试
  15. java.lang.IllegalArgumentException: An invalid domain [.test.com] was specified for this cookie解决方法
  16. python中使用OpenCV处理图片
  17. PowerShell工作流学习-1-嵌套工作流和嵌套函数
  18. MySQL(九)插入、更新和删除
  19. Windows Server 2008 R2之六活动目录域服务的卸载
  20. 文件的概念以及VC里的一些文件操作API简介

热门文章

  1. Python学习--21天Python基础学习之旅(Day05、Day06、Day07)
  2. C++ | 虚拟地址空间
  3. 体验js之美第八课-面向对象创建和继承终结篇
  4. java Web开发实现手机拍照上传到服务器
  5. ES6-11学习笔记--Reflect
  6. python-班级人员信息统计
  7. 搭建 LNMP 环境
  8. 解决webpack项目中打包时候内存溢出的bug JavaScript heap out of memory
  9. 《头号玩家》AI电影调研报告(三)
  10. Java---变量和基本数据类型