| 本站(springdoc.cn)中的内容来源于 spring.io ,原始版权归属于 spring.io。由 springboot.io - Spring Boot中文社区 进行翻译,整理。可供个人学习、研究,未经许可,不得进行任何转载、商用或与之相关的行为。 商标声明:Spring 是 Pivotal Software, Inc. 在美国以及其他国家的商标。 |
如果你刚刚开始使用Spring Authorization Server,下面的章节将引导你创建你的第一个应用程序。
安装 Spring Authorization Server
Spring Authorization Server可以用在你已经使用 Spring Security 的任何地方。
开始使用Spring授权服务器的最简单方法是创建一个基于 Spring Boot 的应用程序。你可以使用 start.spring.io 来生成一个基本项目,或者使用 默认的 Authorization Server 示例 作为指导。然后将Spring Authorization Server 作为一个依赖加入,如下面的例子。
| 官方的 start.spring.io 在国外,从国内访问经常不稳定。推荐使用由 Spring Boot中文社区 提供的镜像服务 start.springboot.io 。 |
Maven
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-authorization-server</artifactId>
<version>1.0.1-SNAPSHOT</version>
</dependency>
Gradle
implementation "org.springframework.security:spring-security-oauth2-authorization-server:1.0.1-SNAPSHOT"
| 关于用Maven或Gradle使用Spring Boot的更多信息,见 安装Spring Boot。 |
开发你的第一个应用程序
要开始工作,你需要在Spring @Configuration 中定义为 @Bean 的最低限度的所需组件。这些组件可以定义如下。
| 要跳过设置并运行一个工作实例,请看 默认 authorization server 示例。 |
@Configuration
public class SecurityConfig {
@Bean (1)
@Order(1)
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http)
throws Exception {
OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http);
http.getConfigurer(OAuth2AuthorizationServerConfigurer.class)
.oidc(Customizer.withDefaults()); // Enable OpenID Connect 1.0
http
// Redirect to the login page when not authenticated from the
// authorization endpoint
.exceptionHandling((exceptions) -> exceptions
.authenticationEntryPoint(
new LoginUrlAuthenticationEntryPoint("/login"))
)
// Accept access tokens for User Info and/or Client Registration
.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);
return http.build();
}
@Bean (2)
@Order(2)
public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http)
throws Exception {
http
.authorizeHttpRequests((authorize) -> authorize
.anyRequest().authenticated()
)
// Form login handles the redirect to the login page from the
// authorization server filter chain
.formLogin(Customizer.withDefaults());
return http.build();
}
@Bean (3)
public UserDetailsService userDetailsService() {
UserDetails userDetails = User.withDefaultPasswordEncoder()
.username("user")
.password("password")
.roles("USER")
.build();
return new InMemoryUserDetailsManager(userDetails);
}
@Bean (4)
public RegisteredClientRepository registeredClientRepository() {
RegisteredClient registeredClient = RegisteredClient.withId(UUID.randomUUID().toString())
.clientId("messaging-client")
.clientSecret("{noop}secret")
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
.authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
.authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
.redirectUri("http://127.0.0.1:8080/login/oauth2/code/messaging-client-oidc")
.redirectUri("http://127.0.0.1:8080/authorized")
.scope(OidcScopes.OPENID)
.scope(OidcScopes.PROFILE)
.scope("message.read")
.scope("message.write")
.clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
.build();
return new InMemoryRegisteredClientRepository(registeredClient);
}
@Bean (5)
public JWKSource<SecurityContext> jwkSource() {
KeyPair keyPair = generateRsaKey();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
RSAKey rsaKey = new RSAKey.Builder(publicKey)
.privateKey(privateKey)
.keyID(UUID.randomUUID().toString())
.build();
JWKSet jwkSet = new JWKSet(rsaKey);
return new ImmutableJWKSet<>(jwkSet);
}
private static KeyPair generateRsaKey() { (6)
KeyPair keyPair;
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
keyPair = keyPairGenerator.generateKeyPair();
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
return keyPair;
}
@Bean (7)
public JwtDecoder jwtDecoder(JWKSource<SecurityContext> jwkSource) {
return OAuth2AuthorizationServerConfiguration.jwtDecoder(jwkSource);
}
@Bean (8)
public AuthorizationServerSettings authorizationServerSettings() {
return AuthorizationServerSettings.builder().build();
}
}
这是一个快速入门的最小配置。要了解每个组件的用途,请看下面的描述。
| 1 | 一个用于 协议端点 的 Spring Security 过滤器链。 |
| 2 | 一个用于 认证 的 Spring Security 过滤器链。 |
| 3 | UserDetailsService 的一个实例,用于检索要认证的用户。 |
| 4 | RegisteredClientRepository 的一个实例,用于管理客户端。 |
| 5 | com.nimbusds.jose.jwk.source.JWKSource 的一个实例,用于签署访问令牌(access token)。 |
| 6 | java.security.KeyPair 的一个实例,其 key 在启动时生成,用于创建上述 JWKSource。 |
| 7 | JwtDecoder 的一个实例,用于解码签名访问令牌(access token)。 |
| 8 | AuthorizationServerSettings 的一个实例,用于配置Spring授权服务器。 |