spring-security-oauth2 使用示例

git地址奉上

https://gitee.com/luck/oauth2.git

#SpringBoot2 + spring-security-oauth2 使用示例,实现了以下四和授权模式。

(1)授权码模式(Authorization Code)

(2)授权码简化模式(Implicit)

(3)Pwd模式(Resource Owner Password Credentials)

(4)Client模式(Client Credentials)

项目提供的所有用户和client 的密码都为123456

#安装运行

导入oauth2.sql

修改 application.yml的数据源

运行

mvn spring-boot:run

授权码模式(Authorization Code)使用说明

  1. 尝试直接访问用户信息
http://localhost:8080/user/info

提示需要认证:

<oauth>
	<error_description>
		Full authentication is required to access this resource
	</error_description>
	<error>
		unauthorized
	</error>
</oauth>

  1. 尝试获取授权码
http://localhost:8080/oauth/authorize?client_id=client_3&response_type=code&scope=read&redirect_uri=http://localhost:8080/code?client_id=client_3

因为这里会弹出 HTTP Basic认证,必须登录的用户才能申请 code。

  1. 输入用户名和密码

username=user_1

passpord=123456

如上用户名密码是交给 SpringSecurity 的主过滤器用来认证的

  1. 登录成功后,真正进行授权码的申请

oauth/authorize 认证成功,会根据 redirect_uri 执行 302 重定向,并且带上生成的 code,注意重定向到的是 8080 端口,这个时候已经是另外一个应用了。

http://localhost:8080/code?client_id=client_3&code=c3FbHM

代码中封装了一个 http 请求, 使用 restTemplate 向 认证服务器发送 token 的申请,当然是使用 code 来申请的,并最终成功获取到 access_token

{
    "access_token":"5db93d64-2252-4349-90a3-e4d6637f90ae",
    "refresh_token":"5a67faae-38ed-4e5c-a809-c9d07c16abcb",
    "scope":"read",
    "token_type":"bearer",
    "expires_in":42494
}

  1. 携带 access_token 访问 用户 信息
http://localhost:8080/user/info?access_token=5db93d64-2252-4349-90a3-e4d6637f90ae

正常返回信息

{
    "password":null,
    "username":"user_1",
    "authorities":[{"authority":"ROLE_USER"}],
    "accountNonExpired":true,
    "accountNonLocked":true,
    "credentialsNonExpired":true,
    "enabled":true
}

授权码简化模式(Implicit) 使用说明

Implicit与Authorization_code 区别是 Implicit不需要验证client_secret,请求如果成功会直接返回 token

获取授权码

 http://localhost:8080/oauth/authorize?response_type=token&client_id=client_4&scope=read&redirect_uri=http://localhost:8080/param

如果成功会重定向到URL(token在URL里)

http://localhost:8080/param#access_token=85090391-2c33-4a75-a989-116bb06b0c5a&token_type=bearer&expires_in=42962&scope=read

密码模式(Resource Owner Password Credentials)使用说明

请求 Access Token:

http://localhost:8080/oauth/token?username=user_1&password=123456&grant_type=client_credentials&scope=read&client_id=client_1&client_secret=123456

正常返回信息

{
    "access_token":"fb1a1d03-9658-4d92-822a-d988c9f7a923",
    "token_type":"bearer",
    "expires_in":43148,
    "scope":"read"
}

Client模式(Client Credentials)使用说明

请求 Access Token:

http://localhost:8080/oauth/token?grant_type=client_credentials&scope=read&client_id=client_1&client_secret=123456

正常返回信息

{
  "access_token": "fb1a1d03-9658-4d92-822a-d988c9f7a923",
  "token_type": "bearer",
  "expires_in": 42811,
  "scope": "read"
}

转载于:https://my.oschina.net/u/560755/blog/3083813

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值