温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

spring-security-oauth2的使用方法

发布时间:2021-07-08 17:43:37 来源:亿速云 阅读:173 作者:chen 栏目:大数据

这篇文章主要介绍“spring-security-oauth2的使用方法”,在日常操作中,相信很多人在spring-security-oauth2的使用方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”spring-security-oauth2的使用方法”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

git地址奉上

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

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

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

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

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

(4)Client模式(Client Credentials)

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

#安装运行

导入oauth3.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"
}

到此,关于“spring-security-oauth2的使用方法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI