前言
-
Spring Authorization Server 是 Spring 团队最新开发适配 OAuth 协议的授权服务器项目,旨在替代原有的 Spring Security OAuth
-
经过半年的开发和孵化,目前已经发布了 0.1.0 版本,初步支持授权码、客户端、刷新、注销等 OAuth 协议
-
本文环境基于 Spring Boot 2.4.2 && authorization-server 0.1.0
Server 搭建
1. maven 依赖
1<!--oauth2 server--> 2<dependency> 3 <groupId>org.springframework.security.experimental</groupId> 4 <artifactId>spring-security-oauth2-authorization-server</artifactId> 5 <version>0.1.0</version> 6</dependency> 7<!--security dependency--> 8<dependency> 9 <groupId>org.springframework.boot</groupId> 10 <artifactId>spring-boot-starter-security</artifactId> 11</dependency>
2. 初始化配置
-
由于官方还未提供对应的 Spring Boot Starter 自动化配置,需要自己配置相关的 @Bean
-
本配置基于 Spring Boot 2.4.2 请知悉
@Configuration @EnableWebSecurity @Import(OAuth2AuthorizationServerConfiguration.class) public class AuthServerConfiguration {
1// 定义 spring security 拦击链规则 2@Bean 3SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception { 4 http 5 .authorizeRequests(authorizeRequests -> 6 authorizeRequests.anyRequest().authenticated() 7 ) 8 .formLogin(withDefaults()); 9 return http.build(); 10}// 创建默认登录用户 lengleng / 123456 @Bean public UserDetailsService userDetailsService() { UserDetails userDetails = User.builder() .username("lengleng") .password("{noop}123456") .authorities("ROLE_USER") .build(); return new InMemoryUserDetailsManager(userDetails); }
// 创建默认的bean 登录客户端,基于 授权码、 刷新令牌的能力 @Bean public RegisteredClientRepository registeredClientRepository() { RegisteredClient client = RegisteredClient.withId("pig") .clientId("pig") .clientSecret("pig") .clientAuthenticationMethod(ClientAuthenticationMethod.BASIC) .authorizationGrantTypes(authorizationGrantTypes -> { authorizationGrantTypes.add(AuthorizationGrantType.AUTHORIZATION_CODE); authorizationGrantTypes.add(AuthorizationGrantType.REFRESH_TOKEN); }) .redirectUri("https://pig4cloud.com") .build(); return new InMemoryRegisteredClientRepository(client); }
// 指定token 生成的加解密密钥 @Bean @SneakyThrows public JWKSource<SecurityContext> jwkSource() { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(2048); KeyPair keyPair = keyPairGenerator.generateKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
1 // @formatter:off 2 RSAKey rsaKey= new RSAKey.Builder(publicKey) 3 .privateKey(privateKey) 4 .keyID(UUID.randomUUID().toString()) 5 .build(); 6 JWKSet jwkSet = new JWKSet(rsaKey); 7 return (jwkSelector, securityContext) -> jwkSelector.select(jwkSet); 8}}
测试
授权码认证
curl --location --request GET 'http://localhost:3000/oauth2/authorize?client_id=pig&client_secret=pig&response_type=code&redirect_uri=https://pig4cloud.com'
获取令牌
1curl --location --request POST 'http://localhost:3000/oauth2/token' \ 2--header 'Authorization: Basic cGlnOnBpZw==' \ 3--header 'Content-Type: application/x-www-form-urlencoded' \ 4--data-urlencode 'grant_type=authorization_code' \ 5--data-urlencode 'code={code}' \ 6--data-urlencode 'redirect_uri=https://pig4cloud.com'
刷新令牌
1curl --location --request POST 'http://localhost:3000/oauth2/token' \ 2--header 'Authorization: Basic cGlnOnBpZw==' \ 3--header 'Content-Type: application/x-www-form-urlencoded' \ 4--data-urlencode 'grant_type=refresh_token' \ 5--data-urlencode 'refresh_token={refresh_token}' \
撤销令牌
-
通过 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={access_token}'
--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={refresh_token}'
--data-urlencode 'token_type_hint=refresh_token'
内容扩展 | Token 个性化
-
RegisteredClient 支持个性化 token 设置的入参
RegisteredClient..tokenSettings()
-
默认配置如下, 包括令牌有效期,刷新令牌控制等
1protected static Map<String, Object> defaultSettings() { 2 Map<String, Object> settings = new HashMap<>(); 3 settings.put(ACCESS_TOKEN_TIME_TO_LIVE, Duration.ofMinutes(5)); 4 settings.put(REUSE_REFRESH_TOKENS, true); 5 settings.put(REFRESH_TOKEN_TIME_TO_LIVE, Duration.ofMinutes(60)); 6 return settings; 7}
总结
-
由于官方暂时未完善相关的文档,所有的端点入参等需要参考 The OAuth 2.0 Authorization Framework