Spring Security oAuth2.0——项目演示

详细演示oAuth项目

一、获取访问授权码

打开浏览器,输入地址:

1
http://localhost:8080/oauth/authorize?client_id=client&response_type=code

第一次访问会跳转到登陆页面。

验证成功后会询问用户是否授权客户端。

点击Authorize进行授权,然后会跳转到我的博客,此时浏览器的地址变成http://www.kadind.cn/?code=u3zUqa包含一个授权码code=u3zUqa,使用这个授权码可以获得访问令牌。

二、通过授权码向服务器申请令牌

CRUL 方式:

1
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'grant_type=authorization_code&code=u3zUqa' "http://client:secret@localhost:8080/oauth/token"

Postman 方式:

Postman

如上图,按照步骤操作后得到响应结果:

1
2
3
4
5
6
{
"access_token": "c7752237-e066-4f8e-ba88-7a598462257c",
"token_type": "bearer",
"expires_in": 39166,
"scope": "app"
}

三、使用令牌访问资源服务器

此处以获取全部资源为例

  • 使用 Headers 方式:需要在请求头增加 Authorization: Bearer yourAccessToken,通过 CURL 或是 Postman 请求。

    CRUL 方式:

    1
    curl --location --request GET "http://localhost:8081/contents" --header "Content-Type: application/json" --header "Authorization: Bearer yourAccessToken"

    Postman 方式:

    postman

  • 直接请求带参数方式:

    1
    http://localhost:8081/contents?access_token=yourAccessToken

    这里可能会需要登陆资源服务器

    用户名为:user

    密码:在资源服务器中自动生成,位置如下图:

    password

  • 获得全部资源,如下图:

    resource

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×