Spring OAuth2 Resource Server 配置

本文详细介绍了如何在SpringBoot应用中配置SpringOAuth2ResourceServer,包括使用JWT令牌验证和处理Opaque令牌,以及设置NimbusJwtDecoder和RemoteTokenServices。
摘要由CSDN通过智能技术生成

Spring OAuth2 Resource Server 是 Spring Security 中的一个模块,用于保护资源服务器上的API资源,确保只有持有合法访问令牌(access token)的客户端才能访问受保护的资源。以下是一个简化的Spring Boot应用中如何配置OAuth2 Resource Server的基本步骤和示例:

依赖引入

首先,在pom.xmlbuild.gradle中引入相应的依赖:

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

<!-- Gradle -->
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
}

配置资源服务器

使用JWT令牌验证

如果你的Authorization Server使用的是JWT令牌,则需要配置资源服务器来验证这种令牌:

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.oauth2.core.DelegatingOAuth2TokenValidator;
import org.springframework.security.oauth2.core.OAuth2Error;
import org.springframework.security.oauth2.jose.jws.JwsAlgorithms;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.jwt.JwtDecoder;
import org.springframework.security.oauth2.jwt.JwtValidators;
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;

import java.util.Arrays;

@Configuration
@EnableWebSecurity
public class ResourceServerConfig extends WebSecurityConfigurerAdapter {

    @Value("${spring.security.oauth2.resourceserver.jwt.issuer-uri}")
    private String issuerUri;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .oauth2ResourceServer()
                .jwt()
                    .decoder(jwtDecoder())
                    .jwtAuthenticationConverter(jwtAuthenticationConverter());
    }

    // 创建JWT解码器
    private JwtDecoder jwtDecoder() {
        NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withJwkSetUri(issuerUri).build();
        jwtDecoder.setJwtValidator(new DelegatingOAuth2TokenValidator<>(Arrays.asList(
            JwtValidators.createDefaultWithIssuer(issuerUri),
            new CustomJwtValidator() // 如果有自定义验证逻辑,可以添加自定义验证器
        )));
        return jwtDecoder;
    }

    // 自定义JWT转换器(如有必要)
    private Converter<Jwt, ? extends AbstractAuthenticationToken> jwtAuthenticationConverter() {
        JwtAuthenticationConverter converter = new JwtAuthenticationConverter();
        // 可能需要映射JWT声明到角色或其他属性
        OAuth2AuthenticatedPrincipal authoritiesExtractor = new AuthoritiesExtractor();
        converter.setJwtAuthoritiesConverter(authoritiesExtractor);
        return converter;
    }
    
    // 示例自定义验证器
    private static class CustomJwtValidator implements OAuth2TokenValidator<Jwt> {
        @Override
        public OAuth2TokenValidatorResult validate(Jwt jwt) {
            // 这里添加额外的JWT验证逻辑
            // ...
            return OAuth2TokenValidatorResult.success(); // 如果验证成功
        }
    }

    // 示例JWT声明转角色的提取器
    private static class AuthoritiesExtractor implements Converter<Jwt, Collection<GrantedAuthority>> {
        @Override
        public Collection<GrantedAuthority> convert(Jwt jwt) {
            // 从JWT声明中获取角色并转换成GrantedAuthority对象
            // ...
            return Arrays.asList(new SimpleGrantedAuthority("ROLE_USER")); // 示例
        }
    }
}
使用opaque令牌验证

如果使用的是opaque类型的访问令牌,则通常需要配置远程Token服务来验证:

// 配置opaque令牌验证
private RemoteTokenServices tokenService() {
    RemoteTokenServices tokenService = new RemoteTokenServices();
    tokenService.setCheckTokenEndpointUrl("http://auth-server/oauth/check_token");
    tokenService.setClientId("your-client-id");
    tokenService.setClientSecret("your-client-secret");
    return tokenService;
}

// 在configure(HttpSecurity)方法中添加配置
.and()
.oauth2ResourceServer()
.authenticationManager(new DefaultOAuth2AuthenticationManager(tokenService()));

注意事项

  • issuer-uri应该指向颁发JWT的Authorization Server的公共元数据URI,以便下载公钥进行验证。

  • 对于opaque令牌,配置RemoteTokenServices时需要替换为实际的检查令牌端点URL、客户端ID和客户端密钥。

  • 上述示例仅作演示用途,实际配置需根据具体的Authorization Server配置和需求调整。

  • JwtAuthenticationConverter用于将JWT中的声明转换为Spring Security可以识别的角色或其他权限信息。

务必按照您的OAuth2授权服务器的具体要求来适配上述配置。在现代应用中,尤其是采用OpenID Connect协议的场景下,使用JWT令牌并验证其签名及标准声明已经成为主流做法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

semicolon_helloword

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值