Spring Cloud微服务安全实战_4-5_搭建OAuth2资源服务器
上一篇搭建了一个OAuth2认证服务器,可以生成token,这篇来改造下之前的订单微服务,使其能够认这个token令牌。
本篇针对订单服务要做三件事:
1,要让他知道自己是资源服务器,他知道这件事后,才会在前边加一个过滤器去验令牌(配置@EnableResourceServer 配置类)
2,要让他知道自己是什么资源服务器(配置资源服务器ID)
3,配置去哪里验令牌,怎么验令牌,要带什么信息去验 (配置@EnableWebSecurity 配置TokenServices,配置AuthenticationManager)
搭建资源服务器


++++++++++++++++++资源服务器现有的各个类++++++++++++++++++++++++++++
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.nb.security</groupId>
<artifactId>nb-order-api</artifactId>
<version>0.0.1-SNAPSHOT</version> <properties>
<java.version>1.8</java.version>
</properties> <dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependencies> </dependencyManagement> <dependencies> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!--OAuth2-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency> <!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency> </dependencies> <build>
<plugins> <!--指定JDK编译版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 打包跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin> <plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
application.yml:
server:
port: 9060
OrderInfo.java :
package com.nb.security.order; import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor; @Data
@NoArgsConstructor
@AllArgsConstructor
public class OrderInfo { private Long productId; }
OrderController:
package com.nb.security.order; import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate; @Slf4j
@RestController
@RequestMapping("/orders")
public class OrderController { private RestTemplate restTemplate = new RestTemplate(); @PostMapping
public OrderInfo create(@RequestBody OrderInfo info){
//查询价格
// PriceInfo price = restTemplate.getForObject("http://localhost:9080/prices/"+info.getProductId(),PriceInfo.class);
// log.info("price is "+price.getPrice());
return info;
} @GetMapping
public OrderInfo getInfo(@PathVariable Long id){
log.info("getInfo: id is "+id);
return new OrderInfo(id);
} }
启动类:
package com.nb.security; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class NbOrderApiApplication { public static void main(String[] args) {
SpringApplication.run(NbOrderApiApplication.class, args);
} }
PriceInfo.java (暂时先不用):
package com.nb.security.order; import lombok.Data; import java.math.BigDecimal; @Data
public class PriceInfo { private Long id; private BigDecimal price;
}
++++++++++++++++++资源服务器现有的各个类结束++++++++++++++++++++++++++++
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
Spring Cloud微服务安全实战_4-5_搭建OAuth2资源服务器的更多相关文章
- Spring cloud微服务安全实战-7-3prometheus环境搭建
Prmetheus 主要用来做来Metrics的监控和报警,这张图是官方的架构图. 这是他的核心 它的作用是根据我们的配置去完成数据的采集.服务的发现,以及数据的存储. 这是服务的发现,通过Servi ...
- Spring cloud微服务安全实战_汇总
Spring cloud微服务安全实战 https://coding.imooc.com/class/chapter/379.html#Anchor Spring Cloud微服务安全实战-1-1 课 ...
- 《Spring Cloud微服务 入门 实战与进阶》
很少在周末发文,还是由于昨晚刚收到实体书,还是耐不住性子马上发文了. 一年前,耗时半年多的时间,写出了我的第一本书<Spring Cloud微服务-全栈技术与案例解析>. 时至今日,一年的 ...
- Spring Cloud微服务安全实战_00_前言
一.前言: 一直以来对服务安全都很感兴趣,所以就学习.这是学习immoc的 jojo老师的 <Spring Cloud微服务安全实战课程>的笔记,讲的很好. 课程简介: 二.最终形成的架 ...
- Spring cloud微服务安全实战 最新完整教程
课程资料获取链接:点击这里 采用流行的微服务架构开发,应用程序访问安全将会面临更多更复杂的挑战,尤其是开发者最关心的三大问题:认证授权.可用性.可视化.本课程从简单的API安全入手,过渡到复杂的微服务 ...
- Spring Cloud微服务安全实战_4-3_订单微服务&价格微服务
实现一个场景: 订单微服务: POM: <?xml version="1.0" encoding="UTF-8"?> <project xml ...
- Spring Cloud微服务安全实战_4-4_OAuth2协议与微服务安全
接上篇文章,在这个流程中,PostMan可以代表客户端应用,订单服务是资源服务器,唯一缺少的是 认证服务器 ,下面来搭建认证服务器 项目结构: Pom.xml : DependencyManager ...
- Spring cloud微服务安全实战-6-8sentinel限流实战
阿里2018年开源的. 简单来说就是干三件事,最终的结果就是保证你的服务可用,不会崩掉.保证服务高可用. 流控 先从最简单的场景来入手. 1.引用一个依赖, 2,声明一个资源. 3.声明一个规则 注意 ...
- Spring cloud微服务安全实战-6-4权限控制改造
授权,权限的控制 令牌里的scope包含fly就有权限访问.根据Oauth的scope来做权限控制, 要让@PreAuthorize生效,就要在启动类里面写一个注解. 里面有一个属性叫做,就是在方法的 ...
随机推荐
- vue v-html 富文本解析 空格,换行,图片大小问题
1.保留空格,换行属性 //保留换行空格问题 white-space: pre-wrap; 2.超出部分,强制换行,一般用于数字 //富文本换行 word-wrap: break-word; tabl ...
- 【前端开发环境】前端使用GIT管理代码仓库需要掌握的几个必备技巧和知识点总结
1. Git的三种状态 已提交 committed 已暂存 staged 已修改 modified 2. Git的三个区域 Git仓库 是 Git 用来保存项目的元数据和对象数据库的地方. 这是 Gi ...
- Preface_英语
这是一本游戏指南.没错,你没有 看错,这就是一本游戏指南.当然,这 本指南针对的只是名为"英文"的游戏. 把英文和电子游戏比较一下,我们 会发现,这两者有惊人的相似之处. 第一,它 ...
- Entity Framework 6 中如何获取 EntityTypeConfiguration 的 Edm 信息?(二)
接着上一篇 直接贴代码了: using System; using System.Collections.Generic; using System.Data.Entity; using System ...
- Redis set集合
Set操作.Set集合就是不允许重复的列表 (无序集合) sadd(name,values) # name对应的集合中添加元素 scard(name) # 获取name对应的集合中元素个数 sdiff ...
- f(n-1) + f(n-2)的编译器处理
https://gcc.godbolt.org int addx(int a){ return a + 2; } int gooo(){ return addx(3) + addx(4) + ad ...
- Filebeat和pipleline processor-不部署logstash,实现对数据的处理
利用ingest node所提供的Pipeline帮我们对数据进行处理. 在Elasticsearch中的配置文件elasticsearch.yml文件中配置:node.ingest: true in ...
- 一个JAVA应用启动缓慢问题排查 --来自jdk securerandom 的问候
开发某个项目过程中,就需求,搭建了一套测试环境.很快完成! 后来代码中加入了许多新功能,会涉及到反复重启,然后就发现了启动特别慢.这给原本功能就不多的应用增添了许多的负担. 我决定改变这一切!找到启动 ...
- 【RT-Thread笔记】IO设备模型及GPIO设备
RTT内核对象--设备 RT-Thread有多种内核对象,其中设备device就是其中一种. 内核继承关系图如下: 设备继承关系图如下: device对象对应的结构体如下: 其中,设备类型type有如 ...
- 你不知道的Go unsafe.Pointer uintptr原理和玩法
unsafe.Pointer 这个类型比较重要,它是实现定位和读写的内存的基础,Go runtime大量使用它.官方文档对该类型有四个重要描述: (1)任何类型的指针都可以被转化为Pointer (2 ...