当前位置:   article > 正文

SpringCloud整合OAuth2资源服务器搭建笔记_springcloud 搭建oauth2

springcloud 搭建oauth2
  1. 添加依赖
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-oauth2</artifactId>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
  2. 资源服务器的配置说明
    2.1 配置资源服务器ID
    2.2 配置资源服务器如何验令牌
    
    • 1
    • 2
  3. 资源服务器配置
    @Configuration
    @EnableResourceServer
    public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {
        @Override
        public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
            resources.resourceId("order-server") ;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  4. 资源服务器验证令牌
    @Configuration
    @EnableWebSecurity
    public class Oauth2WebSecurityConfig extends WebSecurityConfigurerAdapter {
        // 资源服务器的令牌服务// 如何验证令牌
        @Bean
        public ResourceServerTokenServices tokenServices(){
            RemoteTokenServices tokenServices = new RemoteTokenServices() ;
            tokenServices.setClientId("orderService");
            tokenServices.setClientSecret("123456");
            tokenServices.setCheckTokenEndpointUrl("http://localhost:9090/oauth/check_token");
            return tokenServices ;
        }
        // 认证跟用户相关信息就要配置AuthenticationManager
        @Bean
        @Override
        public AuthenticationManager authenticationManagerBean() throws Exception {
            OAuth2AuthenticationManager authenticationManager = new OAuth2AuthenticationManager() ;
            authenticationManager.setTokenServices(tokenServices());
            return authenticationManager;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  5. 验证资源服务器功能
    访问服务是否正常: http://localhost:9080/orders
    header参数 --> Authorization: bearer token
    
    • 1
    • 2
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号