关于java:Spring-Authorization-Server授权服务器入门

27次阅读

共计 2974 个字符,预计需要花费 8 分钟才能阅读完成。

11 月 8 日 Spring 官网曾经强烈建议应用 Spring Authorization Server 替换曾经过期的 Spring Security OAuth2.0,间隔 Spring Security OAuth2.0 完结生命周期还有小半年的工夫,是时候做出扭转了。目前 Spring Authorization Server 曾经进入生产就绪阶段。明天跟着胖哥的节奏搞一搞 Spring Authorization Server 受权服务器框架。

目前 Spring Security 的体系

在目前的 Spring Security 5.x 中将 OAuth2.0 ClientOAuth2.0 Resource Server进行了模块化。
Spring Security 是肯定要引入的。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

如果你要减少 OAuth2.0 Client 反对,能够引入:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
        </dependency>

如果须要 OAuth2.0 Resource Server 反对,能够引入:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-oauth2-resource-server</artifactId>
        </dependency>

当初如果你要减少 OAuth2.0 Authorization Server 反对的话,额定引入上面的依赖就能够了:

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-oauth2-authorization-server</artifactId>
        <!--  截至当初版本  -->
            <version>0.2.0</version>
        </dependency>

至此 OAuth2.0 三大模块齐活了。

Spring Authorization Server

咱们的重点还是回到 Spring Authorization Server 上,目前该我的项目曾经具备生产就绪能力。钻研了几天后,简略出了一个 DEMO,来帮忙心愿学习该框架的同学来了解它。

DEMO 的流程

本 DEMO 将对 OAuth 2.0 的受权码模式(authorization_code)进行演示。这里分两个我的项目;

  • oauth2-client我的项目,顾名思义作为 OAuth2.0 Client,发动对受权服务器的申请受权。
  • oauth2-server我的项目,基于 Spring Authorization Server 搭建的受权服务器,提供受权服务。

用户首先通过 /oauth2/authorization/{registrationId} 端点向 oauth2-client 发动申请:

GET /oauth2/authorization/felord HTTP/1.1
Host: 127.0.0.1:8080

OAuth2AuthorizationRequestRedirectFilter 拦挡后组装成上面的申请链接向受权服务器 oauth2-server 发动受权码受权:

GET /oauth2/authorize?response_type=code&client_id=felord-client&scope=message.read%20message.write&state=0CI0ziUDEnqMgqW0nzRNRCzLrs-9IMbqJzGZ47Zb0gY%3D&redirect_uri=http://127.0.0.1:8080/foo/bar HTTP/1.1
Host: localhost:9000

受权服务器 oauth2-server 拦挡到该申请后,会先查看发动该申请的以后用户是否认证。如果没有认证就抛出 401,跳到受权服务器的登录页面,而后用户执行了登录:

POST /login HTTP/1.1
Host: localhost:9000
Content-Type: application/x-www-form-urlencoded

username=felord&password=password&_csrf=301a7baf-9e9a-4b17-acd4-613c809bf7f5

胜利登录后进行了 302 跳转,继续执行 /oauth2/authorize 受权申请。这时会判断受权申请是否须要用户受权确认,在本 DEMO 中用户受权是须要二次确认的,会跳转到上面这个页面:

批准受权后,受权服务器会调用 redirect_uri 并携带一个 codestateoauth2-client 发动申请:

GET /foo/bar?code=MCSJnvhXNyjilBaCyw1sCrrArWk1bzsEdxe5Z3EFbkdLwp8ASmum62n4M7Tz45VNpp_16IWboBnXlgG3LEfgN7MQqkf0-vVZufGrQpvRioRcBbesAiawMt4cspTk06ca&state=-fRunxjpG0aziPXnfcW1Iw1Fy_5_NwlUAgxABPOfAb8= HTTP/1.1 
Host: 127.0.0.1:8080

oauth2-clientOAuth2AuthorizationCodeGrantFilter 拦挡到 redirect_uri 后向受权服务器发动 /oauth2/token 申请:

POST /oauth2/token?grant_type=authorization_code&code=MCSJnvhXNyjilBaCyw1sCrrArWk1bzsEdxe5Z3EFbkdLwp8ASmum62n4M7Tz45VNpp_16IWboBnXlgG3LEfgN7MQqkf0-vVZufGrQpvRioRcBbesAiawMt4cspTk06ca&redirect_uri=https://127.0.0.1:8080/foo/bar HTTP/1.1Host: localhost:9000Authorization: Basic bWVzc2FnaW5nLWNsaWVudDpzZWNyZXQ=

这里采纳的认证形式是 client-authentication-method: client_secret_basic 形式,详见 OAuth2.0 协定。

受权服务器将 Token 返回给客户端,实现申请,认证客户端信息如下:

到此基于 Spring Authorization Server 整个受权码流程实现了。残缺 DEMO 可关注 GZH:码农小胖哥 回复 oauthserver 获取。原创不易还请多多点赞、转发、再看。更多细节前面会继续跟进。

关注公众号:Felordcn 获取更多资讯

集体博客:https://felord.cn

正文完
 0