# spring-security-oauth2 **Repository Path**: bluesky2006/spring-security-oauth2 ## Basic Information - **Project Name**: spring-security-oauth2 - **Description**: spring security oauth2 learning - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 1 - **Created**: 2022-03-21 - **Last Updated**: 2023-10-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 1 ```shell http://localhost:8090/oauth/authorize?client_id=client&response_type=code http://localhost:8090/oauth/authorize?client_id=client1&response_type=code https://www.baidu.com/?code=YyxCj0 http://client:secret@localhost:8090/oauth/token http://localhost:8080/contents/?access_token=04b003bd-21ad-4747-aeb8-15b368964080 http://localhost:8090/logout /Users/zouw/work/git/spring-boot-samples/spring-security-oauth2 1. oauth_client_detail表中的autoapprove字段要设置成0 2. oauth_approvals表中存在对应的数据,不会在去授权页面.expiresAt字段过期之后才会需要重新授权. https://www.jianshu.com/p/c1c6c966c3a7 注意需要做的: 1 前端应判断系统是否已登陆,如没有登陆,需forward到 http://localhost:8090/oauth/authorize?client_id=client&response_type=code 2 获取code后, 应自动去获取access token, 然后构建自身应用的session. ->8080 ->8090 -> 8080(previous request url) request.forward -> http://xx:8080/contents(session,request) -> 8090/authorize -> login ui -> http://xx:8080/codeTransferToToken?code=xx -> http post(code ) -> access token -> (http://xx:8080/contents?access_token)-> result www.baidu.com?code=xx http://localhost:8080/codeTransferToToken?code=xx -> http post -> http://localhost:8090/oauth/token -> access token ->access token -> local storage , cookie ,session storage -> cookie -> http request -> access token (request header,body) ``` oauth_client_detail ```shell oauth_client_details 字段名 字段约束 详细描述 范例 client_id 主键,必须唯一,不能为空 用于唯一标识每一个客户端(client);注册时必须填写(也可以服务端自动生成),这个字段是必须的,实际应用也有叫app_key OaH1heR2E4eGnBr87Br8FHaUFrA2Q0kE8HqZgpdg8Sw resource_ids 不能为空,用逗号分隔 客户端能访问的资源id集合,注册客户端时,根据实际需要可选择资源id,也可以根据不同的额注册流程,赋予对应的额资源id order-resource,pay-resource client_secret 必须填写 注册填写或者服务端自动生成,实际应用也有叫app_secret, 必须要有前缀代表加密方式 {bcrypt}gY/Hauph1tqvVWiH4atxteSH8sRX03IDXRIQi03DVTFGzKfz8ZtGi scope 不能为空,用逗号分隔 指定client的权限范围,比如读写权限,比如移动端还是web端权限 read,write / web,mobile authorized_grant_types 不能为空 可选值 授权码模式:authorization_code,密码模式:password,刷新token: refresh_token, 隐式模式: implicit: 客户端模式: client_credentials.支持多个用逗号分隔 password,refresh_token web_server_redirect_uri 可为空 客户端重定向uri,authorization_code和implicit需要该值进行校验,注册时填写, httt://baidu.com authorities 可为空 指定用户的权限范围,如果授权的过程需要用户登陆,该字段不生效,implicit和client_credentials需要 ROLE_ADMIN,ROLE_USER access_token_validity 可空 设置access_token的有效时间(秒),默认(606012,12小时) 3600 refresh_token_validity 可空 设置refresh_token有效期(秒),默认(606024*30, 30填) 7200 additional_information 可空 值必须是json格式 {"key", "value"} autoapprove false/true/read/write 默认false,适用于authorization_code模式,设置用户是否自动approval操作,设置true跳过用户确认授权操作页面,直接跳到redirect_uri false ``` ![img.png](img.png)