学习Spring Security OAuth2
OAuth 是协议,Security是安全框架
OAuth2 官方提供的初始化SQL
通过授权码发放令牌
通过令牌访问资源,如果没有令牌,无法访问资源,如下图:
打开浏览器,输入地址:
http://localhost:8080/oauth/authorize?client_id=客户端ID&response_type=code
第一次访问会跳转到登录页面
登录成功,选择授权后会跳转我的个人网站,浏览器地址上还会包含一个授权码(code=3FKELV
),浏览器地址栏会显示如下地址:
有了这个授权码就可以获取访问令牌了
地址:
http://客户端ID:客户端密码@localhost:8080/oauth/token
headers:
Content-Type
: application/x-www-form-urlencoded
参数:
grant_type
: authorization_code
code
: 3FKELV
截图如下:
http://localhost:8081/contents/?access_token=0b2d44da-cd2f-4608-9f8c-063f5e50393f
效果
如果令牌不正确,就会有如下错误: