spring security(三)oauth2

oauth2.0:
阮一峰理解OAuth 2.0:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html
https://www.kancloud.cn/kancloud/oauth_2_0/63340
认证 (authentication) 和授权 (authorization) :先认证和授权

理解OAuth 2.0:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html
1.OAuth 2.0用于什么场景
2.客户端的授权模式哪4个
客户端必须得到用户的授权(authorization grant),才能获得令牌(access token)。OAuth 2.0定义了四种授权方式。
授权码模式(authorization code)
简化模式(implicit)
密码模式(resource owner password credentials)
客户端模式(client credentials)


例子:OAuth 2.0 的四种方式
注意:appkey和app密钥相当于用户名和密码,请不要随意泄露
支付宝只有一个支付宝开放平台

分析百度产品来理解oauth2
授权码模式(authorization code):适用于第三方登录场景
1.先获取code
response_type=code
client_id=
redirect_uri=
scope=
state=开发者自定义参数
示例:
请求:http://localhost:8080/oauth/authorize?client_id=client_id&response_type=code&redirect_uri=https://www.baidu.com&scope=all
响应:https://www.baidu.com?code=REAC334_343TGA


2.获取access_token
请求参数:
grant_type=authorization_code
code
client_id
client_secret
redirect_uri:回调地址
响应结果:
{
    "expires_in":2592000, // Access Token的有效期为30天(以秒为单位)
    "refresh_token":"122.b8c5b89d815424442d8704638bc343c0.Y3CUnCwiS52HHUJQGZIQW2zcY3s4hRVLgn2UnP-.osbSkQ",
    "access_token":"121.6bab3e059aadff2d0cc7c829f38fac6e.YH2aq-DUYn0jjkeK5xv2SpDHzAqqtJAJt0-qOGL.gJZvlA",
    "token_type":"bearer",
    "session_secret":"", // 可无
    "session_key":"", // 可无
    "scope":"basic super_msg"
}
token_type:表示令牌类型,该值大小写不敏感,必选项,可以是bearer类型或mac类型。
场景:第三方授权,如gitee、百度、微信、qq、支付宝、微博等授权登录、微信开放平台 https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/Official_Accounts/official_account_website_authorization.html
第三方平台授权登录:涵盖十多个开发平台 https://blog.csdn.net/qq_40065776/category_9747247.html
基本流程:
    1.配置:去第三方应用创建待授权的应用,提供应用名称、头像、回调url以及需要获取哪些权限等参数,然后获取client_id,client_secret,回调url
    2.应用登录页选择第三方应用登录,跳转到第三方授权页,授权后自动调回调url,后端进行处理
    3.方案一:使用websocket实现前后端交互,虎牙如此
    方案二:回调url写前端地址
    如何使用其他方案?发现战旗、斗鱼回调的后端接口,没有使用websocket也能实现?
    用户扫码后后会跳到你配置的回调地址上!!!但是因为代码中返回是success,用户收到的只是个字符串。所以此处因该是配置支付宝去回调前端地址 然后参数让前端原封不动传向后端 后端解析成功后,前端引导用户进行下一步操作
例子:
使用gitee集成第三方登录:https://www.jb51.net/article/218808.htm
使用支付宝集成第三方登录:https://www.jb51.net/article/216554.htm
一般第三方可信知名大公司才使用,小公司不用


简化模式
这种模式比授权码模式少了 code 环节,回调 url 直接携带 token。授权之后直接带着access_token访问回调接口,而不是像授权模式带着code访问回调接口
如:springboot-demo项目在支持第三方登录,第三方确认授权回调后,直接带着token访问vue-demo的登录成功后的页面
示例:
请求:http://localhost:8080/oauth/authorize?client_id=client_id&response_type=token 注意和授权模式第一步的response_type不一样
响应:https://www.baidu.com/#access_token=5cd97af4-4ccb-41a1-ad6f-14c8c11c0cc7&token_type=bearer&expires_in=1799&scope=all


客户端模式(client credentials):客户端就是用户,自身申请认证。无需登录服务型接口
百度AI开放平台(https://ai.baidu.com/ ):语音识别、文字识别、人脸与人体、图像技术、文字翻译等等
百度地图开放平台:创建一个应用(有ak),请求直接带着 ak=你的密钥 即可,ak即app_key(不属于客户端模式)
示例:百度图文识别
1.获取accesss_token
grant_type=client_credentials
client_id
client_secret
返回结果
{
    "refresh_token": "25.f817a0151c84c64cb1b2bf50f5b64cb8.315360000.1990575555.282335-26242661",
    "expires_in": 2592000,
    "access_token": "24.c16bfe6b799b2a4e00b5637dae7ae7e6.2592000.1677807555.282335-26242661",
    "scope": "public vis-ocr_ocr brain_ocr_scope brain_ocr_general brain_ocr_general_basic vis-ocr_business_license brain_ocr_webimage brain_all_scope brain_ocr_idcard brain_ocr_driving_license brain_ocr_vehicle_license vis-ocr_plate_number brain_solution brain_ocr_plate_number brain_ocr_accurate brain_ocr_accurate_basic brain_ocr_receipt brain_ocr_business_license brain_solution_iocr brain_qrcode brain_ocr_handwriting brain_form brain_ocr_passport brain_ocr_vat_invoice brain_numbers brain_ocr_business_card brain_ocr_train_ticket brain_ocr_taxi_receipt vis-ocr_household_register vis-ocr_vis-classify_birth_certificate vis-ocr_台湾通行证 vis-ocr_港澳通行证 vis-ocr_机动车购车发票识别 vis-ocr_机动车检验合格证识别 vis-ocr_车辆vin码识别 vis-ocr_定额发票识别 vis-ocr_保单识别 vis-ocr_机打发票识别 vis-ocr_行程单识别 brain_ocr_vin brain_ocr_quota_invoice brain_ocr_birth_certificate brain_ocr_household_register brain_ocr_HK_Macau_pass brain_ocr_taiwan_pass brain_ocr_vehicle_invoice brain_ocr_vehicle_certificate brain_ocr_air_ticket brain_ocr_invoice brain_ocr_insurance_doc brain_formula brain_seal brain_ocr_facade brain_ocr_meter brain_doc_analysis brain_ocr_webimage_loc brain_ocr_medical_paper brain_ocr_doc_analysis_office brain_vat_invoice_verification brain_ocr_medical_detail brain_vehicle_registration_certificate brain_ocr_online_taxi_itinerary brain_ocr_multi_idcard brain_ocr_mixed_multi_vehicle brain_ocr_weigth_note brain_ocr_ multiple_invoice brain_form_table wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx",
    "session_key": "9mzdWEHj+3F3+Ju0LJPiwE8K6c/dLwBjaBoWcKQyFzjr/YHzGbOGLs6NAxqHOBj6Tqyiy/gQ6+MX2zSZ2GSFJayZ8V9KUA==",
    "session_secret": "ce189b0cb1ec5f9ff6257c8a5c3263d3"
}

说明:
用户端界面展示AppID、API Key、Secret Key 多款产品采用这种方式
请求参数:grant_type=client_credentials(写死)、client_id、client_secret
向授权服务地址https://aip.baidubce.com/oauth/2.0/token发送请求(推荐使用POST),并在URL中带上以上参数。
服务器将返回json文本参数
{
    "refresh_token": "25.1c1a200aa7f67da1c1adb9f5d22a31ee.315360000.1968769092.282335-26242661",
    "expires_in": 2592000,
    "access_token": "24.35b36685af61834759443ef99b5ce830.2592000.1656001092.282335-26242661",
    "scope": "public vis-ocr_ocr brain_ocr_scope brain_ocr_general brain_ocr_general_basic vis-ocr_business_license",
    "session_key": "9mzdCPGMqkNZ1wGN7HePHhNoHdwd8lZv+jOE5e8eVRGOV3JpMjv0BsqSpVYjan5KCWUdP8xHtpxIbbY/qMQ14aoXMDdzgg==",
    "session_secret": "c4fc1e842004527ea446a7fcc6b6441b"
}
access_token:要获取的Access Token;
expires_in:Access Token的有效期(秒为单位,有效期30天);
其他参数忽略,暂时不用;
若请求错误,服务器将返回的JSON文本包含以下参数:
error: 错误码;关于错误码的详细信息请参考下方鉴权认证错误码。
error_description: 错误描述信息,帮助理解和解决发生的错误。
例如,认证失败返回:
{
    "error": "invalid_client",
    "error_description": "unknown client id"
}

如:百度智能云
疑问:为什么需要两个参数:API Key、Secret Key?一个不行吗?
API Key如同账号、Secret Key如同密码
如果只提供API Key,可能会被暴力获取。Secret Key如同签名,证明你就是API Key
场景:通用型接口提供给可需要的用户使用,无需登录,如地图服务接口、天气预报接口



密码模式
就是为了解决遗留项目升级为 oauth2 的适配方案
场景:通用的账号密码登录场景,用户向客户端提供自己的用户名和密码,这通常用在用户对客户端高度信任的情况。这种情况大多是内部系统之间进行访问。



security实现oauth2:
Spring Security Oauth2:
授权码模式,密码模式,客户端模式:https://blog.csdn.net/qq_22624361/article/details/88557279
SpringSecurity-OAuth2万文详解:https://zhuanlan.zhihu.com/p/496695229
密码模式:https://blog.csdn.net/Andy2019/article/details/106582226/、https://blog.csdn.net/xiaoxiaole0313/article/details/118484750
授权模式:https://blog.csdn.net/qq_48078182/article/details/126318028、https://blog.csdn.net/Cwh_971111/article/details/118208759
    访问http://localhost:8080/oauth/authorize?client_id=client_id_demo&response_type=code获取授权码的时候,提示403,是因为授权的时候,需要用户登录,否则会跳转到登录页面,如果跳转到登录没有访问权限,则会返回403,需要加一句.and().formLogin().permitAll()
    解决:https://gitee.com/log4j/pig/issues/I15YFL 
客户端模式:https://blog.csdn.net/qq_36221788/article/details/119740450


在spring-security-oauth2中有这几个默认接口,按照以下接口访问顺序获取用户信息:
/oauth/authorize:获取授权码
/oauth/token:根据授权码获取令牌
/user/getUser:根据令牌获取user资源信息
Springsecurity-oauth2之/oauth/token的处理:https://blog.csdn.net/weixin_34257076/article/details/92315818
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值