SPRING CLOUD微服务安全实战_4-5_搭建OAUTH2资源服务器

上一篇搭建了一个OAuth2认证服务器,可以生成token,这篇来改造下之前的订单微服务,使其能够认这个token令牌。

本篇针对订单服务要做三件事:

1,要让他知道自己是资源服务器,他知道这件事后,才会在前边加一个过滤器去验令牌(配置@EnableResourceServer 配置类)

2,要让他知道自己是什么资源服务器(配置资源服务器ID)

3,配置去哪里验令牌,怎么验令牌,要带什么信息去验 (配置@EnableWebSecurity 配置TokenServices,配置AuthenticationManager)

搭建资源服务器

++++++++++++++++++资源服务器现有的各个类++++++++++++++++++++++++++++

 pom.xml:

 View Code

application.yml:

 View Code

OrderInfo.java :

 View Code

OrderController:

 View Code

启动类:

 View Code

PriceInfo.java (暂时先不用):

 View Code

++++++++++++++++++资源服务器现有的各个类结束++++++++++++++++++++++++++++

1,在资源服务器pom哩加上 oauth2的依赖:

        <!--OAuth2-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-oauth2</artifactId>
        </dependency>

2,新建资源服务器配置类,继承   ResourceServerConfigurerAdapter

复制代码

package com.nb.security.resource.server;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
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;

/**
 * 资源服务器
 * 配置了@EnableResourceServer ,所有发往nb-order-api的请求,都会去请求头里找token,找不到不让你过
 */
@Configuration
@EnableResourceServer//告诉nb-order-api,你就是资源服务器
public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        //配置资源服务器的id,“现在我就是资源服务器order-server!!!”
        resources.resourceId("order-server");
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        /**
         * 进入nb-order-api的所有请求,哪些要拦截,哪些要放过,在这里配置
         */
        http.authorizeRequests()
                .antMatchers("/hello")
                .permitAll() //放过/haha不拦截
                .anyRequest().authenticated();//其余所有请求都拦截
    }
}

复制代码

ResourceServerConfigurerAdapter 有两个方法:

 开篇说的1、2、3中的1和2已经完成了,下面做第3件事,怎么验令牌:

新建配置类:

复制代码

package com.nb.security.resource.server;

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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationManager;
import org.springframework.security.oauth2.provider.token.RemoteTokenServices;
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;

/**
 * 怎么验发往本服务的请求头的令牌
 * 1,自定义tokenServices ,说明去哪里去验token
 * 2,重写authenticationManagerBean()方法,将AuthenticationManager暴露为一个Bean
 *    要认证跟用户相关的信息,一般用 AuthenticationManager
 *
 * 这样配置了后,所有发往nb-order-api的请求,
 * 需要验token的时候就会发请求去http://localhost:9090/oauth/check_token验token,获取到token对应的用户信息
 */
@Configuration
@EnableWebSecurity
public class OAuth2WebSecurityConfig extends WebSecurityConfigurerAdapter{


    /**
     * 通过这个Bean,去远程调用认证服务器,验token
     * @return
     */
    @Bean
    public ResourceServerTokenServices tokenServices(){
        RemoteTokenServices tokenServices = new RemoteTokenServices();
        tokenServices.setClientId("orderService");//在认证服务器配置的,订单服务的clientId
        tokenServices.setClientSecret("123456");//在认证服务器配置的,订单服务的ClientSecret
        tokenServices.setCheckTokenEndpointUrl("http://localhost:9090/oauth/check_token");
        return tokenServices;
    }


    /**
     * 要认证跟用户相关的信息,一般用 AuthenticationManager
     * 覆盖这个方法,可以将AuthenticationManager暴露为一个Bean
     *
     * @return
     * @throws Exception
     */
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        OAuth2AuthenticationManager authenticationManager = new OAuth2AuthenticationManager();
        authenticationManager.setTokenServices(tokenServices());//设置为自定义的TokenServices,去校验令牌
        return authenticationManager;
    }
}

复制代码

 认证服务器关于订单服务的配置:

 启动认证服务器,

启动订单 资源服务器,

访问认证服务器获取token  : localhost:9090/oauth/token   

 

 拿着 token 去资源服务器创建订单,注意,选择bearer类型的token,生成的请求头是这样的(注:Authorization的value值,postman生成的是以Bearer开头的,但是我看谷歌浏览器restclient插件生成的是bearer的B是小写):

 

 在资源服务器里,可以通过该注解获取用户名:

错误的情况: 

如果把资源服务器配置的resourceId改成了order-server222,请求创建订单,会受到如下的错误

 如果资源服务器检验token的cilentID获取clientSecret写错了,后台会报错:

 ++++++++++++++++++++++分割线++++++++++++++++++++++

小结:

接上篇的认证服务器,本篇实现了资源服务器,以及与认证服务器的交互,怎么去验令牌。

遗留疑问:

认证服务器里面,配置资源服务器secret的时候,用passwordEncoder对123456进行了加密,而在资源服务器里,clientSecret确实明文的123456,这两者不需要一致么?

认证服务器里对资源服务器ClientId、 ClientSecret配置:

资源服务器验令牌携带的ClientId、 ClientSecret:

代码github :https://github.com/lhy1234/springcloud-security/tree/chapt-4-5-resource-server

  

欢迎关注个人公众号一起交流学习:

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值