SpringSecurity分布式整合之资源服务器搭建和测试
资源服务
说明
资源服务可以有很多个,这里只拿产品服务为例,记住,资源服务中只能通过公钥验证认证。不能签发token!
创建产品服务并导入jar包
根据实际业务导包即可,咱们就暂时和认证服务一样了。
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springboot_security_jwt_rsa_parent</artifactId><groupId>com.leon</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>leon_source_product</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><dependency><groupId>com.leon</groupId><artifactId>leon_common</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.0</version></dependency></dependencies>
</project>
编写产品服务配置文件
切记这里只能有公钥地址!
server:port: 9002
spring:datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql:///security_authorityusername: rootpassword: root
mybatis:type-aliases-package: com.leon.domainconfiguration:map-underscore-to-camel-case: true
logging:level:com.itheima: debug
rsa:key:pubKeyFile: D:\auth_key\id_key_rsa.pub
编写读取公钥的配置类
@ConfigurationProperties("rsa.key")
public class RsaKeyProperties {private String pubKeyFile;private PublicKey publicKey;@PostConstructpublic void createRsaKey() throws Exception {publicKey = RsaUtils.getPublicKey(pubKeyFile);}public String getPubKeyFile() {return pubKeyFile;}public void setPubKeyFile(String pubKeyFile) {this.pubKeyFile = pubKeyFile;}public PublicKey getPublicKey() {return publicKey;}public void setPublicKey(PublicKey publicKey) {this.publicKey = publicKey;}
}
编写启动类
@SpringBootApplication
@MapperScan("com.leon.mapper")
@EnableConfigurationProperties(RsaKeyProperties.class)
public class AuthSourceApplication {public static void main(String[] args) {SpringApplication.run(AuthSourceApplication.class, args);}
}
复制认证服务中SpringSecurity配置类做修改,去掉“增加自定义认证过滤器”即可!
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled=true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredprivate RsaKeyProperties prop;//SpringSecurity配置信息public void configure(HttpSecurity http) throws Exception {http.csrf().disable().authorizeRequests().antMatchers("/product").hasAnyRole("USER").anyRequest().authenticated().and().addFilter(new JwtVerifyFilter(super.authenticationManager(), prop)).sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);}
}
编写产品处理器
@RestController
@RequestMapping("/product")
public class ProductController {@GetMappingpublic String findAll(){return "产品测试成功!";}
}
在产品处理器上添加访问需要ADMIN角色
@RestController
@RequestMapping("/product")
public class ProductController {@Secured("ROLE_ADMIN")@GetMappingpublic String findAll(){return "产品测试成功!";}
}
SpringSecurity分布式整合之资源服务器搭建和测试相关推荐
- SpringSecurity分布式整合之认证模块搭建
认证服务 创建认证服务工程并导入jar包 <?xml version="1.0" encoding="UTF-8"?> <project xm ...
- SpringSecurity分布式整合之认证服务配置文件编写和测试
编写SpringSecurity配置类 @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(securedEnabled=tru ...
- MQTT服务器搭建和测试步骤及遇见的问题
MQTT服务器搭建和测试步骤及遇见的问题 前言:MQTT服务器有好多种,查看:Servers/Brokers Apache-Apollo:一个代理服务器,在ActiveMQ基础上发展而来,可以支持ST ...
- 阿里云MQTT服务器搭建与测试(全图文,非常详细)
阿里云MQTT服务器搭建与测试 一. MQTT概念 二. 阿里云MQTT服务器搭建 1 阿里云平台注册及认证 2 添加平台 2 创建产品与设备 获取MQTT连接相关信息 三.MQTT.fx测试 1 M ...
- SpringSecurity(二十)---OAuth2:实现资源服务器(上)资源服务器搭建以及直接调用授权服务器模式
一. 前言 本章将讨论如何使用Spring Security实现一个资源服务器,资源服务器是管理用户资源的组件.另外,学习本章有个前提,需要先把前面搭建授权服务器的相关文章先给阅读,否则可能后面出现的 ...
- 我的世界整合包 云服务器搭建方法(ECS)
最近因为放假了,想和朋友开黑玩MC. 但是由于真的受不了某侠的联机平台(再加上自己学了点东西),想自己搭建一个服务器 注:这篇文章的操作过程是由其他人的教程加上我的个人的过程整合而成的 搭建前准备: ...
- SpringSecurity分布式整合之分布式认证流程说明
分布式认证概念说明 分布式认证,即我们常说的单点登录,简称SSO,指的是在多应用系统的项目中,用户只需要登录一次,就可以访 问所有互相信任的应用系统. 分布式认证流程图 首先,我们要明确,在分布式项目 ...
- Windows nginx静态资源服务器搭建
第一次搭建本地静态资源服务器nginx,记录以供后续学习 实现页面如下 1. 了解nginx: Nginx是一个开源的Web服务器,同时Nginx也提供了反向代理和负载均衡的功能.为了实现将Windo ...
- Nginx静态资源服务器搭建
1 安装nginx(docker安装nginx见此篇) 2 配置nginx的server代码块 # 这是静态资源服务器的配置文件 server {listen 80;server_name 域名;ro ...
最新文章
- ArchLinux上安装TIM
- 前端架构之移动端混合架构(hybrid)
- Python+numpy实现矩阵QR分解
- Latex除法a/b \frac{a}{b}
- Swift基础语法学习-4.Bool类型
- ReflectionException: There is no setter for property named ‘createTime‘ in ‘class XXX‘
- ALVA Systems发布AR新品 倪光南院士致辞
- python 绕过 反爬
- iPhone 14 系列维修价曝光,修不起!
- Intel寄存器名称解释及用途,%eax%ebx等都是什么意思
- C++沙海拾遗(三)
- Sklearn_LearningCurve
- C++RTTI运算符
- PP模块--MRP专题一:MRP基本逻辑
- jython mysql_jython 访问数据库的方法
- 【无标题】智慧工厂数字孪生建设方案
- 组策略 计算机 用户账户控制,Windows 10 (用户帐户控制组策略和注册表) - Microsoft 365 Security | Microsoft Docs...
- 前端复杂表格一键导出看这篇就够了(附源码)
- 【node.js+html】无聊在家写一个在线客服聊天系统
- Java源码分析 AbstractList的迭代器实现(Itr和ListItr)