SpringBoot集成OAuth2

  1. 后续对资源服务的请求,附带access_token即可

http://localhost:8000/private/hi?access_token=d69bc334-3d5c-4c86-972c-11826f44c4af

2021-01-11-RequestCode.png

2021-01-11-TokenCode

源码在master分支:https://github.com/heartsuit/demo-springboot-oauth2/tree/master/in-memory

implicit 简化模式 不支持refresh token 需结合浏览器测试

GET http://localhost:9000/oauth/authorize?grant_type=implicit&response_type=token&scope=read&client_id=client0&client_secret=secret0

2021-01-11-RequestImplicit.png

2021-01-11-TokenImplicit.png

源码在implicit分支:https://github.com/heartsuit/demo-springboot-oauth2/tree/implicit/in-memory

password 密码模式

POST http://localhost:9000/oauth/token?username=admin&password=123456&grant_type=password&scope=read&client_id=client1&client_secret=secret1

2021-01-11-RequestPassword.png

2021-01-11-TokenPassword.png

源码在password分支:https://github.com/heartsuit/demo-springboot-oauth2/tree/password/in-memory

client_credentials 客户端模式 不支持refresh token

POST http://localhost:9000/oauth/token?grant_type=client_credentials&scope=read&client_id=client2&client_secret=secret2

2021-01-11-RequestClient.png

2021-01-11-TokenClient.png

源码在client分支:https://github.com/heartsuit/demo-springboot-oauth2/tree/client/in-memory

四种方式的授权流程

以下流程来自:https://tools.ietf.org/html/rfc6749

authorization_code 授权码模式

±---------+

| Resource |

| Owner |

| |

±---------+

^

|

(B)

±—|-----+ Client Identifier ±--------------+

| -±—(A)-- & Redirection URI ---->| |

| User- | | Authorization |

| Agent -±—(B)-- User authenticates —>| Server |

| | | |

| -±—©-- Authorization Code —<| |

±|----|—+ ±--------------+

| | ^ v

(A) © | |

| | | |

^ v | |

±--------+ | |

| |>—(D)-- Authorization Code ---------’ |

| Client | & Redirection URI |

| | |

| |<—(E)----- Access Token -------------------’

±--------+ (w/ Optional Refresh Token)

Note: The lines illustrating steps (A), (B), and © are broken into

two parts as they pass through the user-agent.

Figure 3: Authorization Code Flow

The flow illustrated in Figure 3 includes the following steps:

(A) The client initiates the flow by directing the resource owner’s user-agent to the authorization endpoint. The client includes its client identifier, requested scope, local state, and a redirection URI to which the authorization server will send the user-agent back once access is granted (or denied).

(B) The authorization server authenticates the resource owner (via the user-agent) and establishes whether the resource owner grants or denies the client’s access request.

© Assuming the resource owner grants access, the authorization server redirects the user-agent back to the client using the redirection URI provided earlier (in the request or during client registration). The redirection URI includes an authorization code and any local state provided by the client earlier.

(D) The client requests an access token from the authorization server’s token endpoint by including the authorization code received in the previous step. When making the request, the client authenticates with the authorization server. The client includes the redirection URI used to obtain the authorization code for verification.

(E) The authorization server authenticates the client, validates the authorization code, and ensures that the redirection URI received matches the URI used to redirect the client in step ©. If valid, the authorization server responds back with an access token and, optionally, a refresh token.

implicit 简化模式

±---------+

| Resource |

| Owner |

| |

±---------+

^

|

(B)

±—|-----+ Client Identifier ±--------------+

| -±—(A)-- & Redirection URI —>| |

| User- | | Authorization |

| Agent -|----(B)-- User authenticates -->| Server |

| | | |

| |<—©— Redirection URI ----<| |

| | with Access Token ±--------------+

| | in Fragment

| | ±--------------+

| |----(D)— Redirection URI ---->| Web-Hosted |

| | without Fragment | Client |

| | | Resource |

| (F) |<—(E)------- Script ---------<| |

| | ±--------------+

±|--------+

| |

(A) (G) Access Token

| |

^ v

±--------+

| |

| Client |

| |

±--------+

Note: The lines illustrating steps (A) and (B) are broken into two

parts as they pass through the user-agent.

Figure 4: Implicit Grant Flow

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

最后

小编在这里分享些我自己平时的学习资料,由于篇幅限制,pdf文档的详解资料太全面,细节内容实在太多啦,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!

程序员代码面试指南 IT名企算法与数据结构题目最优解

这是” 本程序员面试宝典!书中对IT名企代码面试各类题目的最优解进行了总结,并提供了相关代码实现。针对当前程序员面试缺乏权威题目汇总这一-痛点, 本书选取将近200道真实出现过的经典代码面试题,帮助广“大程序员的面试准备做到万无一失。 “刷”完本书后,你就是“题王”!

image.png

《TCP-IP协议组(第4版)》

本书是介绍TCP/IP协议族的经典图书的最新版本。本书自第1版出版以来,就广受读者欢迎。

本书最新版进行」护元,以体境计算机网络技不的最新发展,全书古有七大部分共30草和7个附录:第一部分介绍一些基本概念和基础底层技术:第二部分介绍网络层协议:第三部分介绍运输层协议;第四部分介绍应用层协议:第五部分介绍下一代协议,即IPv6协议:第六部分介绍网络安全问题:第七部分给出了7个附录。

image.png

Java开发手册(嵩山版)

这个不用多说了,阿里的开发手册,每次更新我都会看,这是8月初最新更新的**(嵩山版)**

image.png

MySQL 8从入门到精通

本书主要内容包括MySQL的安装与配置、数据库的创建、数据表的创建、数据类型和运算符、MySQL 函数、查询数据、数据表的操作(插入、更新与删除数据)、索引、存储过程和函数、视图、触发器、用户管理、数据备份与还原、MySQL 日志、性能优化、MySQL Repl ication、MySQL Workbench、 MySQL Utilities、 MySQL Proxy、PHP操作MySQL数据库和PDO数据库抽象类库等。最后通过3个综合案例的数据库设计,进步讲述 MySQL在实际工作中的应用。

image.png

Spring5高级编程(第5版)

本书涵盖Spring 5的所有内容,如果想要充分利用这一领先的企业级 Java应用程序开发框架的强大功能,本书是最全面的Spring参考和实用指南。

本书第5版涵盖核心的Spring及其与其他领先的Java技术(比如Hibemate JPA 2.Tls、Thymeleaf和WebSocket)的集成。本书的重点是介绍如何使用Java配置类、lambda 表达式、Spring Boot以及反应式编程。同时,将与企业级应用程序开发人员分享一些见解和实际经验,包括远程处理、事务、Web 和表示层,等等。

image.png

JAVA核心知识点+1000道 互联网Java工程师面试题

image.png

image.png

企业IT架构转型之道 阿里巴巴中台战略思想与架构实战

本书讲述了阿里巴巴的技术发展史,同时也是-部互联网技 术架构的实践与发展史。

image.png
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门即可获取!
核心知识点+1000道 互联网Java工程师面试题**

[外链图片转存中…(img-WP4WYZnV-1712206879125)]

[外链图片转存中…(img-Zn0X1MD3-1712206879125)]

企业IT架构转型之道 阿里巴巴中台战略思想与架构实战

本书讲述了阿里巴巴的技术发展史,同时也是-部互联网技 术架构的实践与发展史。

[外链图片转存中…(img-M2BHVMe0-1712206879126)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门即可获取!

好的,针对您的问题,我可以给您提供以下步骤: 1. 添加依赖 在 pom.xml 文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.security.oauth.boot</groupId> <artifactId>spring-security-oauth2-autoconfigure</artifactId> <version>2.1.0.RELEASE</version> </dependency> ``` 2. 配置认证服务器 在 Spring Boot 应用程序中,您可以使用 @EnableAuthorizationServer 注解来启用 OAuth2 认证服务器。以下是一个示例配置: ``` @Configuration @EnableAuthorizationServer public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired private AuthenticationManager authenticationManager; @Autowired private UserDetailsService userDetailsService; @Autowired private DataSource dataSource; @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.jdbc(dataSource); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager) .userDetailsService(userDetailsService); } } ``` 3. 配置资源服务器 在 Spring Boot 应用程序中,您可以使用 @EnableResourceServer 注解来启用 OAuth2 资源服务器。以下是一个示例配置: ``` @Configuration @EnableResourceServer public class ResourceServerConfig extends ResourceServerConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/api/**").authenticated() .anyRequest().permitAll(); } } ``` 4. 配置安全 在 Spring Boot 应用程序中,您可以使用 application.yml 或 application.properties 文件来配置安全。以下是一个示例配置: ``` security: oauth2: client: client-id: my-client client-secret: my-secret access-token-uri: http://localhost:8080/oauth/token user-authorization-uri: http://localhost:8080/oauth/authorize resource: user-info-uri: http://localhost:8080/user ``` 以上就是集成 OAuth2 的基本步骤,您可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值