Seal^_^ 2025-12-04 15:39 采纳率: 91.2%
浏览 6
已结题

如何通过Spring Security OAuth2和JWT实现微服务之间的安全调用? 资源服务器如何验证访问令牌的合法性?

如何通过Spring Security OAuth2和JWT实现微服务之间的安全调用? 资源服务器如何验证访问令牌的合法性?

  • 写回答

2条回答 默认 最新

  • 紫雾凌寒 新星创作者: 人工智能技术领域 2025-12-04 15:47
    关注

    让 “紫雾凌寒” 来帮你回答,本回答结合 DeepSeek

    如果还有问题,请评论或点击头像私信,如果有帮助到您, 请关注、采纳、点赞

    1. 关键点分析:
      - 要实现微服务之间通过Spring Security OAuth2和JWT进行安全调用,需要解决多个方面的问题。包括配置授权服务器来颁发JWT令牌,配置资源服务器来验证这些令牌。
      - 资源服务器验证访问令牌合法性涉及到正确配置Spring Security以识别和验证JWT中的信息。
    2. 解决方案:
      - 配置授权服务器(Authorization Server)
      • 引入Spring Security OAuth2相关依赖。例如在pom.xml中:
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-oauth2</artifactId>
    </dependency>
    
     - 创建授权服务器配置类。例如:
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.authentication.AuthenticationManager;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
    import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
    import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
    import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
    import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
    import org.springframework.security.oauth2.provider.token.TokenStore;
    import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
    import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
    
    import java.util.Arrays;
    
    @Configuration
    @EnableAuthorizationServer
    public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
    
        @Override
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
            clients.inMemory()
                   .withClient("clientId")
                   .secret("clientSecret")
                   .authorizedGrantTypes("authorization_code", "password", "refresh_token")
                   .scopes("read", "write")
                   .autoApprove(true);
        }
    
        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
            TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
            tokenEnhancerChain.setTokenEnhancers(Arrays.asList(jwtAccessTokenConverter(), new CustomTokenEnhancer()));
    
            endpoints.tokenStore(tokenStore())
                   .tokenEnhancer(tokenEnhancerChain)
                   .authenticationManager(authenticationManager());
        }
    
        @Bean
        public TokenStore tokenStore() {
            return new JwtTokenStore(jwtAccessTokenConverter());
        }
    
        @Bean
        public JwtAccessTokenConverter jwtAccessTokenConverter() {
            JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
            converter.setSigningKey("123");
            return converter;
        }
    
        @Override
        public void configure(HttpSecurity http) throws Exception {
            http.csrf().disable();
        }
    }
    
    • 配置资源服务器(Resource Server)
      • 引入相关依赖:
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-oauth2</artifactId>
    </dependency>
    
     - 创建资源服务器配置类:
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
    import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
    import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
    import org.springframework.security.oauth2.provider.token.TokenStore;
    
    @Configuration
    @EnableResourceServer
    public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
    
        @Override
        public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
            resources.tokenStore(tokenStore());
        }
    
        @Override
        public void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
                   .antMatchers("/public/**").permitAll()
                   .anyRequest().authenticated();
        }
    
        @Bean
        public TokenStore tokenStore() {
            return new JwtTokenStore(jwtAccessTokenConverter());
        }
    
    
        @Bean
        public JwtAccessTokenConverter jwtAccessTokenConverter() {
            JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
            converter.setSigningKey("123");
            return converter;
        }
    }
    
    • 微服务之间调用示例
      • 在调用微服务的代码中,例如使用Feign客户端:
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    
    @FeignClient(name = "resource-service", url = "http://localhost:8080", configuration = OAuth2FeignRequestInterceptor.class)
    public interface ResourceServiceClient {
    
        @GetMapping("/protected")
        String getProtectedResource();
    }
    
     - 这里`OAuth2FeignRequestInterceptor`用于在Feign调用时添加访问令牌。
    
    1. 多种解决方案优缺点:
      - 基于Spring Security OAuth2和JWT的方案
      • 优点
      • 安全性高,JWT包含了用户信息和权限信息,在传输过程中不易被篡改。
      • 分布式系统中,微服务之间的调用可以独立验证令牌,无需依赖授权服务器的实时验证。
      • 可扩展性好,可以方便地添加新的微服务和调整权限策略。
      • 缺点
      • JWT一旦签发,在有效期内无法撤销,除非使用黑名单机制。
      • 由于JWT包含用户信息和权限信息,体积相对较大,会增加网络传输开销。
    2. 总结:
      通过Spring Security OAuth2和JWT实现微服务之间安全调用,首先要配置好授权服务器来颁发JWT令牌,再配置资源服务器来验证这些令牌。在微服务之间调用时,通过合适的方式(如Feign客户端并配置拦截器)传递令牌。这种方式能有效保障微服务之间调用的安全性,并且具有较好的可扩展性,但也需要注意JWT本身的一些特性带来的优缺点。

    希望以上解答对您有所帮助。如果您有任何疑问,欢迎在评论区提出。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月3日
  • 已采纳回答 3月26日
  • 创建了问题 12月4日