springcloud 基于security 进行接口安全认证

1  spring cloud 开发一定要导入的两个 maven 依赖,要不会报乱七八糟的错误
<dependency> <!-- 进行SpringCloud依赖包的导入处理 -->
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Finchley.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>
<dependency> <!-- SpringCloud离不开SpringBoot,所以必须要配置此依赖包 -->
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.1.2.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

接口安全认证步骤如下:

首先 添加 security maven 依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

application.properties 或者 yml 中添加配置

spring.security.user.name=root
spring.security.user.password=root
spring.security.user.roles=USER

验证 security 是否生效  访问添加了 security 随便一个接口 

 

http://localhost:8081/getMenu  访问  弹出 页面 如下

 

输入 配置的账号密码可以看到访问接口的返回数据

 

服务器消费者端变动,服务器消费者需要请求的时候添加账号密码验证信息

@Bean
public HttpHeaders getHeaders() { // 要进行一个Http头信息配置
    HttpHeaders headers = new HttpHeaders(); // 定义一个HTTP的头信息
    String auth = "root:root"; // 认证的原始信息
    byte[] encodedAuth = Base64.getEncoder()
            .encode(auth.getBytes(Charset.forName("US-ASCII"))); // 进行一个加密的处理
    String authHeader = "Basic " + new String(encodedAuth);
    headers.set("Authorization", authHeader);
    return headers;
}

restTemplate 请求带上 请求验证头信息即可

 

 

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Gateway 和 Spring Security 可以相互整合,以提供安全的 API 网关解决方案。下面是一些步骤来实现这个整合: 1. 首先,在你的 Spring Cloud Gateway 项目中添加 Spring Security 的依赖。你可以在 `build.gradle` 或 `pom.xml` 文件中添加以下依赖: ```groovy implementation 'org.springframework.boot:spring-boot-starter-security' ``` 2. 创建一个安全配置类来配置 Spring Security。你可以创建一个类,并使用 `@EnableWebSecurity` 注解标记它,然后扩展 `WebSecurityConfigurerAdapter` 类。在这个类中,你可以配置认证和授权规则。 ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/api/public/**").permitAll() // 允许公共访问的接口 .anyRequest().authenticated() // 其他接口需要认证 .and() .httpBasic(); // 使用 HTTP Basic 认证 } } ``` 3. 在 Gateway 中配置 Spring Security。在你的 Gateway 配置文件(如 `application.yml`)中,添加以下配置: ```yaml spring: security: user: name: user # 设置默认用户名 password: password # 设置默认密码 ``` 4. 进一步配置 Gateway 的路由和鉴权规则。你可以使用 Gateway 的路由配置来定义每个路由的鉴权规则。例如,你可以为需要认证的路由添加 `filters`,进行基于角色的访问控制: ```yaml spring: cloud: gateway: routes: - id: my_route uri: http://example.com predicates: - Path=/api/private/** filters: - name: Security args: roles: ROLE_ADMIN ``` 在上面的例子中,只有具有 `ROLE_ADMIN` 角色的用户才能访问 `/api/private/**` 路径。 以上是整合 Spring Cloud Gateway 和 Spring Security 的基本步骤。你可以根据自己的需求进一步定制和配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值