Spring Security OAuth2学习
文章目录
认证方式
客户端授权(Client Credentials Grant)
1 2 3
POST /oauth/token?grant_type=client_credentials HTTP/1.1 Host: server.example.com Authorization: Basic Y2xpZW50X2F1dGhfbW9kZToxMjM0NTY=
- 在mysql中建立表:oauth_client_details
- 请求头Authorization,key=Authorization;value=Basic+空格+Base64(username:password),Basic后面的信息由
username:password
内的字符Base64加密而成。 - username和password分别为oauth_client_details表中的client_id和client_secret,也就是客户端模式下的标识客户端的凭证(用以区别是哪种受信任的客户端),对应OAuth2映射为ClientDetails对象。
密码授权
1 2 3 4 5
POST /token HTTP/1.1 Host: server.example.com Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW Content-Type: application/x-www-form-urlencoded grant_type=password&username=johndoe&password=A3ddj3w
- 请求头Authorization,key=Authorization;value=Basic+空格+Base64(username:password),Basic后面的信息由
username:password
内的字符Base64加密而成。 - username和password依旧为oauth_client_details表中的client_id和client_secret,也就是客户端模式下的标识客户端的凭证(用以区别是哪种受信任的客户端),对应OAuth2映射为DetailDetails对象。
- 与客户端模式相比多参数:username和password,在于密码模式下,Spring-Security-Oauth2中,有个叫做UserDetails的对象。
- 在客户端模式下,需要对ClientDetails对象进行认证;而在密码模式下,则既需要对ClientDetails对象认证,也需要对UserDetails对象认证。
- 请求头Authorization,key=Authorization;value=Basic+空格+Base64(username:password),Basic后面的信息由
认证配置
认证管理信息的配置主要是针对ClientDetails和UserDetails对象的检查,客户端模式针对ClientDetails检查,而密码模式则先检查ClientDetails后检查UserDetails对象。 认证授权配置如下
|
|
AccessToken
客户端认证
请求的报文信息:
1 2 3 4 5 |
POST /oauth2-server/oauth/token?grant_type=client_credentials HTTP/1.1 Host: server.example.com Authorization: Basic Y2xpZW50X2F1dGhfbW9kZToxMjM0NTY= Cache-Control: no-cache Content-Type: multipart/form-data |
返回结果:
1 2 3 4 5 6 |
{ "access_token": "afef641c-62de-4f5d-a5b8-7864ac2b7127", "token_type": "bearer", "expires_in": 3463, "scope": "APP" } |
用户名密码认证
请求的报文信息:
1 2 3 4 5 |
POST /oauth2-server/oauth/token?username=admin&password=e10adc3949ba59abbe56e057f20f883e&grant_type=password&client_id=webapp&client_secret=123456 HTTP/1.1 Host: server.example.com Authorization: Basic cGFzc3dvcmRfYXV0aF9tb2RlOjEyMzQ1Ng== Cache-Control: no-cache Content-Type: multipart/form-data |
返回结果:
1 2 3 4 5 6 7 |
{ "access_token": "a83ba33f-9f1a-4f9a-ba65-99e7fc905ba2", "token_type": "bearer", "refresh_token": "89f724d6-8553-4838-b4ff-7f6c8fb4d88b", "expires_in": 3378, "scope": "APP" } |
两者区别
- 客户端授权返回结果比密码模式返回结果少了refresh_token,因为客户模式不支持refresh_token认证。
- 客户端授权(client_credentials)是受信任的认证模式,即可以设置为永久性的AccessToken,而不需要刷新重新获取AccessToken。
获取AccessToken
|
|
AccessToken有效性
拦截器
|
|
注册拦截器
|
|
根据token获取OAuth2AccessToken与OAuth2Authentication
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
@RestController @RequestMapping("/oauth") public class TokenController { /** * 覆盖了 spring-security-oauth2 内部的 endpoint oauth2/check_token * spring-security-oauth2 内部原有的该控制器 CheckTokenEndpoint,返回值,不符合自身业务要求,故覆盖之。 */ @GetMapping("/check_token") public OAuth2AccessToken getToken( @RequestParam(value = "token") String token){ OAuth2AccessToken oAuth2AccessToken = Oauth2Utils.checkTokenInOauth2Server(token); return oAuth2AccessToken; } /** * 获取当前token对应的用户主体的凭证信息(认证对象) */ @GetMapping("/getAuth") public OAuth2Authentication getAuth(@RequestParam(value = "token") String token){ OAuth2Authentication oAuth2Authentication = Oauth2Utils.getAuthenticationInOauth2Server(token); return oAuth2Authentication; } |
工具类
Oauth2Utils
|
|
ApplicationSupport
|
|
配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
security: basic: enabled: false # 是否开启基本的鉴权,默认为true。 true:所有的接口默认都需要被验证,将导致 拦截器[对于 excludePathPatterns()方法失效] server: context-path: /oauth2-client port: 8061 --- spring: application: name: oauth2-client datasource: #数据源的配置 url: jdbc:mysql://127.0.0.1:3306/oauth2?useUnicode=true&characterEncoding=UTF-8 username: root password: 123456 jpa: #jpa的支持:hibernate的相关配置 database-platform: org.hibernate.dialect.MySQL5InnoDBDialect database: MYSQL openInView: true show_sql: true generate-ddl: true #(false) hibernate: ddl-auto: update #(none) oauth: #oauth2-server认证授权服务器的url配置,在获取AccessToken以及检测AccessToken中会用到 token: http://127.0.0.1:8050/oauth2-server/oauth/token check_token: http://localhost:8050/oauth2-server/oauth/check_token #检查AccessToken有效性的url(认证授权服务器的url地址),获取 AccessToken 对象。 |
本文为学习记录,因能力有限,如有错误请赐教……如需转载,请注明出处!
文章作者 binbin wen
上次更新 2018-08-13