将JWT与Spring Security OAuth2结合使用

将JWT与Spring Security OAuth2结合使用

概述

OAuth 2.0 是行业标准的授权协议。OAuth 2.0 专注于客户端开发人员的简单性,同时为 Web 应用程序、桌面应用程序、移动电话和客厅设备提供特定的授权流程。

OAuth 授权服务器负责对用户进行身份验证并发布包含用户数据和适当访问策略的访问令牌。

下面我们将使用Spring Authorization Server构建一个简单的授权服务器。

OAuth2授权服务器实现

让我们从OAuth2授权服务器配置实现开始。

maven依赖
 <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
   <version>2.6.7</version>
</dependency>

<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-oauth2-authorization-server</artifactId>
  <version>0.3.1</version>
</dependency>
配置

首先让我们通过application.yml配置数据库连接信息.

spring:
  application:
    name: auth-server
  datasource:
    druid:
      db-type: mysql
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/integrated_oauth?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
      username: <<username>> # 修改用户名
      password: <<password>> # 修改密码

之后我们创建一个AuthorizationServerConfig配置类,在此类中我们将创建OAuth2授权服务器所需特定的Bean。第一个将是客户端服务存储库,我们使用RegisteredClient构建器类型创建一个客户端,并将它持久化到数据库中。

 @Bean
    public RegisteredClientRepository registeredClientRepository(JdbcTemplate jdbcTemplate) {
        RegisteredClient registeredClient = RegisteredClient.withId(UUID.randomUUID().toString())
                .clientId("relive-client")
                .clientSecret("{noop}relive-client")
                .clientAuthenticationMethods(s -> {
                    s.add(ClientAuthenticationMethod.CLIENT_SECRET_POST);
                    s.add(ClientAuthenticationMethod.CLIENT_SECRET_BASIC);
                })
                .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
                .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
                .authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
                .authorizationGrantType(AuthorizationGrantType.PASSWORD)
                .redirectUri("http://127.0.0.1:8070/login/oauth2/code/messaging-client-authorization-code")
                .scope("message.read")
                .clientSettings(ClientSettings.builder()
                        .requireAuthorizationConsent(true)
                        .requireProofKey(false)
                        .build())
                .tokenSettings(TokenSettings.builder()
                        .idTokenSignatureAlgorithm(SignatureAlgorithm.RS256)
                        .accessTokenTimeToLive(Duration.ofSeconds(30 * 60))
                        .refreshTokenTimeToLive(Duration.ofSeconds(60 * 60))
                        .reuseRefreshTokens(true)
                        .build())
                .build();

        JdbcRegisteredClientRepository registeredClientRepository = new JdbcRegisteredClientRepository(jdbcTemplate);
        registeredClientRepository.save(registeredClient);

        return registeredClientRepository;
    }

我们配置的属性是:

  • id–Registeredclient唯一标识

  • clentId–客户端标识符

  • clientSecret–客户端秘密

  • clientAuthenticationMethods–客户端可能使用的身份验证方法。支持的值为client_secret_basicclient_secret_postprivate_key_jwtclient_secret_jwtnone

  • authorizationGrantTypes–客户端可以使用的授权类型。支持的值为authorization_codeclient_credentialsrefresh_token

  • redirectUris–客户端已注册重定向 URI

  • scopes–允许客户端请求的范围。

  • clientSettings–客户端的自定义设置

  • tokenSettings–发布给客户端的 OAuth2 令牌的自定义设置


接下来让我们配置存储新授权和查询现有授权的中心组件OAuth2AuthorizationService。

   @Bean
    public OAuth2AuthorizationService authorizationService(JdbcTemplate jdbcTemplate, RegisteredClientRepository registeredClientRepository) {
        return new JdbcOAuth2AuthorizationService(jdbcTemplate, registeredClientRepository);
    }

针对OAuth2 授权请求的授权“同意”,Spring提供了OAuth2AuthorizationConsentService存储新授权同意和查询现有授权同意的组件。

   @Bean
    public OAuth2AuthorizationConsentService authorizationConsentService(JdbcTemplate jdbcTemplate, RegisteredClientRepository registeredClientRepository) {
        return new JdbcOAuth2AuthorizationConsentService(jdbcTemplate, registeredClientRepository);
    }

接下来让我们创建一个bean,配置OAuth2授权服务其他默认配置,并以用来对与未认证的授权请求,将该请求重定向到登录页面。

   @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
        OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http);
        return http.exceptionHandling(exceptions -> exceptions.
                authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login"))).build();
    }

每个授权服务器都需要用于令牌的签名密钥,让我们生成一个RSA密钥:

final class KeyGeneratorUtils {

    private KeyGeneratorUtils() {
    }

    static KeyPair generateRsaKey() {
        KeyPair keyPair;
        try {
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(2048);
            keyPair = keyPairGenerator.generateKeyPair();
        } catch (Exception ex) {
            throw new IllegalStateException(ex);
        }
        return keyPair;
    }
}
public final class Jwks {

	private Jwks() {
	}

	public static RSAKey generateRsa() {
		KeyPair keyPair = KeyGeneratorUtils.generateRsaKey();
		RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
		RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
		return new RSAKey.Builder(publicKey)
				.privateKey(privateKey)
				.keyID(UUID.randomUUID().toString())
				.build();
	}
}
 @Bean
    public JWKSource<SecurityContext> jwkSource() {
        RSAKey rsaKey = Jwks.generateRsa();
        JWKSet jwkSet = new JWKSet(rsaKey);
        return (jwkSelector, securityContext) -> jwkSelector.select(jwkSet);
    }

处理完令牌的签名密钥,授权服务器还需要一个颁发者URL,我们可以通过ProviderSettings创建:

    @Bean
    public ProviderSettings providerSettings() {
        return ProviderSettings.builder()
                .issuer("http://127.0.0.1:8080")
                .build();
    }

最后我们将启用Spring Security安全配置类,以保护我们的服务。

@EnableWebSecurity
@Configuration
public class DefaultSecurityConfig {


    @Bean
    SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
        http
                .authorizeRequests(authorizeRequests ->
                        authorizeRequests.anyRequest().authenticated()
                )
                .formLogin(withDefaults())
        return http.build();
    }
  
  //...
}

这里authorizeRequests.anyRequest().authenticated()让所有请求都需要认证,并提供基于Form表单的身份认证。


我们还需要定义测试使用的用户信息,以下创建一个基于内存的用户信息存储库。

    @Bean
    UserDetailsService users() {
        UserDetails user = User.withDefaultPasswordEncoder()
                .username("admin")
                .password("password")
                .roles("USER")
                .build();
        return new InMemoryUserDetailsManager(user);
    }

资源服务器实现

现在我们将创建一个资源服务器,服务中的API接口只允许通过OAuth2授权服务器身份验证的请求。

maven依赖
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <version>2.6.7</version>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
  <version>2.6.7</version>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
  <version>2.6.7</version>
</dependency>
配置

首先让我们通过application.yml配置服务端口。

server:
  port: 8090

接下来,进行OAuth2安全配置,我们需要使用之前授权服务器在ProviderSettings中设置的issuerUri。

spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: http://127.0.0.1:8080

资源服务器将使用此Uri进一步进行自我配置,发现授权服务器的公钥,并传入用于验证JWT的JwtDecoder。此过程的结果是授权服务器必须启动并接收请求才能使资源服务器成功启动。


如果资源服务器必须能够独立于授权服务器启动,那么可以提供jwk-set-uri。这将是我们进一步在OAuth2安全配置中添加属性:

spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: http://127.0.0.1:8080
          jwk-set-uri: http://127.0.0.1:8080/oauth2/jwks

现在我们可以设置Spring Security安全配置,对服务资源的每个请求都应该被授权并具有适当的权限:

@EnableWebSecurity
public class ResourceServerConfig {

    @Bean
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.requestMatchers()
                .antMatchers("/resource/test/**")
                .and()
                .authorizeRequests()
                .mvcMatchers("/resource/test/**")
                .access("hasAuthority('SCOPE_message.read')")
                .and()
                .oauth2ResourceServer()
                .jwt();
        return http.build();
    }
}

最后,我们将创建一个 REST 控制器,它将返回jwt的claims信息。

@RestController
public class ResourceServerTestController {

    @GetMapping("/resource/test")
    public Map<String, Object> getArticles(@AuthenticationPrincipal Jwt jwt) {
        return jwt.getClaims();
    }
}

OAuth2客户端

现在我们要创建一个客户端,它首先向授权服务器请求授权获取访问令牌,在访问资源服务器对应资源。

maven依赖
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <version>2.6.7</version>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
  <version>2.6.7</version>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-oauth2-client</artifactId>
  <version>2.6.7</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webflux</artifactId>
  <version>5.3.9</version>
</dependency>
配置

首先我们将要在application.yml中配置客户端的访问端口8070。

server:
  port: 8070

接下来我们将定义OAuth2客户端的配置属性:

spring:
  security:
    oauth2:
      client:
        registration:
          messaging-client-authorization-code:
            provider: client-provider
            client-id: relive-client
            client-secret: relive-client
            authorization-grant-type: authorization_code
            redirect-uri: "http://127.0.0.1:8070/login/oauth2/code/{registrationId}"
            scope: message.read
            client-name: messaging-client-authorization-code
        provider:
          client-provider:
            authorization-uri: http://127.0.0.1:8080/oauth2/authorize
            token-uri: http://127.0.0.1:8080/oauth2/token

现在让我们创建一个WebClient实例用于向资源服务器执行HTTP请求:

  @Bean
    WebClient webClient(OAuth2AuthorizedClientManager authorizedClientManager) {
        ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2Client = new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
        return WebClient.builder()
                .apply(oauth2Client.oauth2Configuration())
                .build();
    }

WebClient添加了一个OAuth2授权过滤器,需要OAuth2AuthorizedClientManager作为依赖项,在此处只配置了授权码和刷新token,如有需要可以添加其他模式:

@Bean
    OAuth2AuthorizedClientManager authorizedClientManager(ClientRegistrationRepository clientRegistrationRepository,
                                                          OAuth2AuthorizedClientRepository authorizedClientRepository) {

        //可以扩展其他模式
        OAuth2AuthorizedClientProvider authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder
                .builder()
                .authorizationCode()
                .refreshToken()
                .build();
        DefaultOAuth2AuthorizedClientManager authorizedClientManager = new DefaultOAuth2AuthorizedClientManager(clientRegistrationRepository, authorizedClientRepository);
        authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);

        return authorizedClientManager;
    }

最后,我们将配置Spring Security安全配置:

 @Bean
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                .authorizeRequests(authorizeRequests ->
                        //便于测试,将权限开放
                        authorizeRequests.anyRequest().permitAll()
                )
                .oauth2Client(withDefaults());
        return http.build();
    }

这里我们将客户端API权限全部放开,但是在实际情况下,客户端服务是需要认证的,OAuth2协议本身是授权协议,并不关心认证的具体表现形式,你也可以向授权服务器一样添加简单的表单认证

访问资源列表

最后,我们创建一个控制器,我们将使用之前配置的WebClient向我们资源服务器发起HTTP请求:

@RestController
public class ClientTestController {
    @Autowired
    private WebClient webClient;

    @GetMapping(value = "/client/test")
    public Map<String, Object> getArticles(@RegisteredOAuth2AuthorizedClient("messaging-client-authorization-code") OAuth2AuthorizedClient authorizedClient) {
        return this.webClient
                .get()
                .uri("http://127.0.0.1:8090/resource/test")
                .attributes(oauth2AuthorizedClient(authorizedClient))
                .retrieve()
                .bodyToMono(Map.class)
                .block();
    }
}

在上面示例中,我们使用@RegisteredOAuth2AuthorizedClient注解绑定OAuth2AuthorizedClient,并以触发OAuth2授权码模式流程获取访问令牌。

结论

本例主要演示在使用OAuth2进行两个服务安全通信,尤其是在复杂的互联网场景中,客户端服务和资源服务恰好由不同的平台提供,OAuth2非常善于获取用户的委托决策,很多方面他都比其他方案更简单,更安全。

与往常一样,本文中使用的源代码可在 GitHub 上获得。

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: Spring Security是一个基于Spring框架的安全框架,它提供了一系列的安全服务,包括身份认证、授权、攻击防护等。OAuth2是一种授权协议,它允许用户授权第三方应用访问他们的资源,而不需要将用户名和密码提供给第三方应用。JWT是一种轻量级的身份验证和授权机制,它使用JSON对象作为令牌,可以在客户端和服务器之间传递信息。 Spring Security OAuth2和JWT都是用于安全认证和授权的工具,但它们的实现方式不同。Spring Security OAuth2是基于OAuth2协议实现的,它提供了一系列的OAuth2相关的类和接口,可以方便地实现OAuth2的授权流程。而JWT则是一种轻量级的身份验证和授权机制,它使用JSON对象作为令牌,可以在客户端和服务器之间传递信息。 在实际应用中,可以根据具体的需求选择使用Spring Security OAuth2或JWT来实现安全认证和授权。如果需要实现OAuth2的授权流程,可以选择使用Spring Security OAuth2;如果只需要简单的身份验证和授权,可以选择使用JWT。 ### 回答2: Spring Security OAuth2 是一个基于 Spring SecurityOAuth2 插件,它支持客户端和服务器端的 OAuth2 协议,提供了丰富的授权和认证功能。Spring Security OAuth2 的主要作用是,用于处理 OAuth2 的认证和授权流程,在 OAuth2 的基础上实现了更加丰富的授权和认证策略,使得开发者可以更好地在其应用中使用 OAuth2,保证了应用的可靠性和安全性。 JWT(JSON Web Token)是一种特定的 JSON 格式,用于在发送方和接收方之间传输信息,并且可以可靠地验证该信息是否被篡改。JWT一般由三部分组成,分别为 header、payload 和 signature 三个部分。JWT 的主要作用是用于进行安全授权和身份认证,可以通过 JWT 验证和解析信息、用户认证等操作。在 Web 应用中,JWT 被广泛应用于前后端分离的开发模式中,以及 API 的安全验证。 相较于 Spring Security OAuth2,JWT 更加灵活和简单,因为它不需要进行额外的认证和授权流程。JWT 的认证和授权流程可一步完成,因此适用于分布式系统等场景下的认证和授权。另外,JWT 可以自定义 payload 的内容,可以传输更为复杂和丰富的信息,因此在一些特定的应用场景中,JWT 更具优势。 综上所述,Spring Security OAuth2 适用于需要更强大的授权和认证流程的场景,而 JWT 更适合简单的认证和授权场景,也更加灵活和方便。在实际场景中,可以根据应用的实际需求选择使用合适的安全解决方案。 ### 回答3: Spring Security是一个基于Spring框架的安全框架,可以为Web应用程序提供身份验证、授权、防护和其他安全的处理。OAuth2是一种为Web应用程序提供授权访问的开放标准,用于客户端访问受保护的资源。JWT是一种简单的基于Web标准的和可移植的身份验证和授权方案,用于无状态身份验证。 Spring Security OAuth2和JWTSpring Security框架中用于身份验证和授权的两种解决方案。Spring Security OAuth2基于OAuth2标准,通过token的方式进行身份验证和授权,而JWT则是一种常见的token。 在Spring Security OAuth2中,客户端通过OAuth2服务机构获取访问资源的token。而在JWT中,token是由服务端生成的,包含了用户的身份信息和其他相关信息。在使用Spring Security OAuth2时,需要通过OAuth2服务机构来获取token,而在使用JWT时,可以通过服务端直接生成token,具有更高的效率。 Spring Security OAuth2和JWT都能够为Web应用程序提供身份验证和授权功能,但Spring Security OAuth2较为复杂,需要先配置服务机构才能使用,而JWT则较为简单,可以直接在服务端生成token。因此,选择何种方案需要根据实际情况进行权衡。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值