14.3、Spring Authorization Server
分类: Spring Cloud Security
Spring Authorization Server
Spring Authorization Server 是 OAuth2 授权服务器实现。本节将学习 Spring Authorization Server。
本节将学习:Authorization Server 简介、项目搭建、客户端注册,以及授权端点配置。
Authorization Server 简介
定义
Spring Authorization Server 是 Spring 官方提供的 OAuth2 授权服务器实现,支持 OAuth2 和 OpenID Connect。
核心功能
核心功能:
- OAuth2 授权服务器
- 客户端注册
- 令牌管理
- 用户认证
- 授权端点
项目搭建
依赖添加
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-authorization-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
配置文件
spring: security: oauth2: authorization-server: issuer-url: http://localhost:9000
启动类
@SpringBootApplication public class AuthorizationServerApplication { public static void main(String[] args) { SpringApplication.run(AuthorizationServerApplication.class, args); } }
客户端注册
内存注册
@Configuration public class AuthorizationServerConfig { @Bean public RegisteredClientRepository registeredClientRepository() { RegisteredClient registeredClient = RegisteredClient .withId(UUID.randomUUID().toString()) .clientId("client-id") .clientSecret("{noop}client-secret") .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC) .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE) .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN) .redirectUri("http://localhost:8080/login/oauth2/code/client-id") .scope("read") .scope("write") .build(); return new InMemoryRegisteredClientRepository(registeredClient); } }
数据库注册
@Bean public RegisteredClientRepository registeredClientRepository(JdbcTemplate jdbcTemplate) { return new JdbcRegisteredClientRepository(jdbcTemplate); }
授权端点配置
安全配置
@Configuration @EnableWebSecurity public class SecurityConfig { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http .authorizeHttpRequests(authorize -> authorize .requestMatchers("/oauth2/**").permitAll() .anyRequest().authenticated() ) .oauth2ResourceServer(oauth2 -> oauth2 .jwt(jwt -> jwt.decoder(jwtDecoder())) ); return http.build(); } }
授权服务器配置
@Configuration @EnableAuthorizationServer public class AuthorizationServerConfig { @Bean public AuthorizationServerSettings authorizationServerSettings() { return AuthorizationServerSettings.builder() .issuer("http://localhost:9000") .build(); } @Bean public JWKSource<SecurityContext> jwkSource() { RSAKey rsaKey = generateRsa(); JWKSet jwkSet = new JWKSet(rsaKey); return (jwkSelector, securityContext) -> jwkSelector.select(jwkSet); } }
用户详情服务
@Bean public UserDetailsService userDetailsService() { UserDetails user = User.withDefaultPasswordEncoder() .username("user") .password("password") .roles("USER") .build(); return new InMemoryUserDetailsManager(user); }
官方资源
- Spring Authorization Server 官方文档:https://spring.io/projects/spring-authorization-server
- Spring Security OAuth2:https://spring.io/guides/tutorials/spring-boot-oauth2/
本节小结
在本节中,我们学习了:
第一个是 Authorization Server 简介。 Spring Authorization Server 是 OAuth2 授权服务器。
第二个是项目搭建。 如何搭建授权服务器项目。
第三个是客户端注册。 如何注册 OAuth2 客户端。
第四个是授权端点配置。 如何配置授权端点。
这就是 Spring Authorization Server。搭建好授权服务器后,就可以提供 OAuth2 授权服务了。
在下一节,我们将学习 JWT 令牌管理。