4 Star 11 Fork 1

yuezht/spring-security-oauth2-example

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Spring Security OAuth project provides all the necessary API we might need in order to develop an OAuth2 compliant implementation using Spring. Official Spring security oauth project provides a comprehensive example for implementing OAuth2. The code samples of this post is inspired by that examples itself. The intention of this post is to just use bare-minimum functionality required in order to secure our REST API, nothing more. As you, I too am still learning it, so feel free to correct me if something seems not right.

At minimum, you should be aware of four key concepts in OAuth2:

  1. OAuth2 Roles OAuth2 defines four roles:

resource owner: Could be you. An entity capable of granting access to a protected resource. When the resource owner is a person, it is referred to as an end-user. resource server: The server hosting the protected resources, capable of accepting and responding to protected resource requests using access tokens. client: An application making protected resource requests on behalf of the resource owner and with its authorization. It could be a mobile app asking your permission to access your Facebook feeds, a REST client trying to access REST API, a web site [Stackoverflow e.g.] providing an alternative login option using Facebook account. authorization server: The server issuing access tokens to the client after successfully authenticating the resource owner and obtaining authorization. In our example, our REST API can only be accessed via Resource server which will require an access token to be present with request

  1. OAuth2 Authorization Grant types An authorization grant is a credential representing the resource owner’s authorization (to access its protected resources) used by the client to obtain an access token. The specification defines four grant types:

authorization code implicit resource owner password credentials client credentials We will be using resource owner password credentials grant type. The reason is simple, we are not implementing a view which redirects us to a login page. Only the usage where a client [Postman or RestTemplate based Java client e.g.] have the Resource owner’s credentials and they provide those credential [along with client credentials] to authorization server in order to eventually receive the access-token[and optionally refresh token], and then use that token to actually access the resources.

A common example is the GMail app [a client] on your smartphone which takes your credentials and use them to connect to GMail servers. It also shows that ‘Password Credentials Grant’ is best suited when both the client and the servers are from same company as the trust is there, you don’t want to provide your credentials to a third party.

  1. OAuth2 Tokens Tokens are implementation specific random strings, generated by the authorization server and are issued when the client requests them.

Access Token : Sent with each request, usually valid for a very short life time [an hour e.g.] Refresh Token : Mainly used to get a new access token, not sent with each request, usually lives longer than access token. A Word on HTTPS : For any sort of Security implementation, ranging from Basic authentication to a full fledged OAuth2 implementation, HTTPS is a must have. Without HTTPS, no matter what your implementation is, security is vulnerable to be compromised. 4. OAuth2 Access Token Scope Client can ask for the resource with specific access rights using scope [want to access feeds & photos of this users facebook account], and authorization server in turn return scope showing what access rights were actually granted to the client [Resource owner only allowed feeds access, no photos e.g.].

空文件

简介

secure spring rest api using oauth2.0 密码模式(Resource Owner Password Credentials Grant) 展开 收起
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/yuezht/spring-security-oauth2-example.git
git@gitee.com:yuezht/spring-security-oauth2-example.git
yuezht
spring-security-oauth2-example
spring-security-oauth2-example
master

搜索帮助