8月17日,Spring正式宣布,Spring授权服务器正式脱离实验状态,进入Spring-Project系列!
背景
Spring Authorization Server(SAS)是Spring团队为取代现有的Spring security OAuth server而为OAuth协议开发的最新许可服务器项目。
开始上手
1. 核心依赖
- 这里需要 SAS 、Security, 注意看注释
2. 配置 security 安全认证
- 定义用户来源及其 form 认证的信息
3. 配置 SAS 服务器
@Configuration
@EnableWebSecurity
public class AuthServerConfiguration {
// security 挂载 SAS 【最重要的一步】
@Bean
@Order)
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAu(http);
return ()).build();
}
// 客户端来源
@Bean
public RegisteredClientRepository registeredClientRepository() {
RegisteredClient client = Regi("pig")
.clientId("pig")
.clientSecret("{noop}pig")
.clientAuthenticationMethod)
.authorizationGrantTypes(authorizationGrantTypes -> {
au);
au);
})
.redirectUri(";)
.build();
return new InMemoryRegisteredClientRepository(client);
}
// 以下两个bean 定义 生成jwt 的配置,可以直接参考文末源码介绍,这里就不在截图
@Bean
@SneakyThrows
public JWKSource<SecurityContext> jwkSource() {
....
}
@Bean
public static JwtDecoder jwtDecoder(JWKSource<SecurityContext> jwkSource) {
...
}
}
测试运行
通过以上配置即可搭建完成 SAS 服务端,我们以授权码模式测试
- 浏览器访问如下链接,会重定向至登录页
- 输入账号密码后,会携带 code 自动回调至目标页面
- 使用 code 换 token
curl --location --request POST 'http://localhost:3000/oauth2/token'
> --header 'Authorization: Basic cGlnOnBpZw=='
> --header 'Content-Type: application/x-www-form-urlencoded'
> --data-urlencode 'grant_type=authorization_code'
> --data-urlencode 'code=dn0GmDB-4hAfg-Kc9luUkuqZn4keJF9ZkUTlmcSRnYn8uzfEV9Ih429MH-9O77TPEVqPxXAJLPgxq-znOpiI-28Sek305db8Rezd46ods95FrjCSMq_HAswCtAJV4Vrt'
> --data-urlencode 'redirect_uri=;
{"access_token":"eyJraWQiOiI2YmU4Yz;,"refresh_token":"YlxCAnSyvtq1HcKqE3D3o-P_lT90wxdRQ6jfWbwQoKQaeFUZr51gQQQawSfpUUH4yf9kW51v7ENH2o4pDot7yIeN2tljVpKU6zuolj6gFKq0uDA6KkDDz54cDzfx1aw4","token_type":"Bearer","expires_in":"299"}
- 刷新 token
curl --location --request POST 'http://localhost:3000/oauth2/token'
> --header 'Authorization: Basic cGlnOnBpZw=='
> --header 'Content-Type: application/x-www-form-urlencoded'
> --data-urlencode 'grant_type=authorization_code'
> --data-urlencode 'code=dn0GmDB-4hAfg-Kc9luUkuqZn4keJF9ZkUTlmcSRnYn8uzfEV9Ih429MH-9O77TPEVqPxXAJLPgxq-znOpiI-28Sek305db8Rezd46ods95FrjCSMq_HAswCtAJV4Vrt'
> --data-urlencode 'redirect_uri=;
{"access_token":"eyJraWQiOiI2YmU4Yz;,"refresh_token":"YlxCAnSyvtq1HcKqE3D3o-P_lT90wxdRQ6jfWbwQoKQaeFUZr51gQQQawSfpUUH4yf9kW51v7ENH2o4pDot7yIeN2tljVpKU6zuolj6gFKq0uDA6KkDDz54cDzfx1aw4","token_type":"Bearer","expires_in":"299"}% lengleng@MacBook-Pro ~/Downloads/auth-server-demo password ±
lengleng@MacBook-Pro ~/Downloads/auth-server-demo password ± curl --location --request POST 'http://localhost:3000/oauth2/token'
> --header 'Authorization: Basic cGlnOnBpZw=='
> --header 'Content-Type: application/x-www-form-urlencoded'
> --data-urlencode 'grant_type=refresh_token'
> --data-urlencode 'refresh_token=YlxCAnSyvtq1HcKqE3D3o-P_lT90wxdRQ6jfWbwQoKQaeFUZr51gQQQawSfpUUH4yf9kW51v7ENH2o4pDot7yIeN2tljVpKU6zuolj6gFKq0uDA6KkDDz54cDzfx1aw4'
>
{"access_token":"eyJraWQiOiI2YmU4Yz;,"refresh_token":"YlxCAnSyvtq1HcKqE3D3o-P_lT90wxdRQ6jfWbwQoKQaeFUZr51gQQQawSfpUUH4yf9kW51v7ENH2o4pDot7yIeN2tljVpKU6zuolj6gFKq0uDA6KkDDz54cDzfx1aw4","token_type":"Bearer","expires_in":"299"}%
撤销令牌
- 通过 access_token
curl --location --request POST 'http://localhost:3000/oauth2/revoke'
--header 'Authorization: Basic cGlnOnBpZw=='
--header 'Content-Type: application/x-www-form-urlencoded'
--data-urlencode 'token=eyJraWQiOiI0NmM3Zjk0OS01NmZmLTRlMjg;
--data-urlencode 'token_type_hint=access_token'
- 通过 refresh_token
curl --location --request POST 'http://localhost:3000/oauth2/revoke'
--header 'Authorization: Basic cGlnOnBpZw=='
--header 'Content-Type: application/x-www-form-urlencoded'
--data-urlencode 'token=ku4R4n7YD1f584KXj4k_3GP9o-HbdY-PDIIh-twPVJTmvHa5mLIoifaNhbBvFNBbse6_wAMcRoOWuVs9qeBWpxQ5zIFrF1A4g1Q7LhVAfH1vo9Uc7WL3SP3u82j0XU5x'
--data-urlencode 'token_type_hint=refresh_token'
项目源码地址
1.《【fist of bean】Spring Authorization Server》援引自互联网,旨在传递更多网络信息知识,仅代表作者本人观点,与本网站无关,侵删请联系页脚下方联系方式。
2.《【fist of bean】Spring Authorization Server》仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证。
3.文章转载时请保留本站内容来源地址,https://www.cxvn.com/gl/djyxgl/189247.html