Springboot Feign调用oauth2获取token报错[401] Unauthorized

在搭建Springboot项目后台管理系统时,后台登录接口通过feign请求auth2获取token报错:

feign.FeignException$Unauthorized: [401] during [POST] to [http://authorization-server/oauth/token?grant_type=password&username=admin&password=102131456&login_type=admin_type&Authorization=Basic%20Y29pbi1hcGk6Y29pbi1zZWNyZXQ%3D] [OAuth2FeignClient#getToken(String,String,String,String,String)]: [{"timestamp":"2024-07-23T10:44:53.456+0000","status":401,"error":"Unauthorized","message":"Unauthorized","path":"/oauth/token"}]

主要原因是feign调用接口的Authorization把请求设置RequestParam导致报这个错,修改成RequestHeader即可,代码如下:

package org.tfq.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.tfq.model.JwtToken;

/**
 * @author: fqtang
 * @date: 2024/07/23/10:16
 * @description: 调用授权服务器获取token
 */
@FeignClient(value = "authorization-server")
public interface OAuth2FeignClient {

	/**
	 * 登录接口
	 * @param grant_type 授权类型
	 * @param username   账号
	 * @param password   密码
	 * @param login_type 登录类型
	 * @param basic_auth token的认证账号和密码,第三方客户端加密数据,注意要用请求头
	 * @return 登录结果(包含菜单和权限)
	 */
	@PostMapping("/oauth/token")
	ResponseEntity<JwtToken> getToken(
		@RequestParam("grant_type") String grant_type,
		@RequestParam("username") String username,
		@RequestParam("password") String password,
		@RequestParam("login_type") String login_type,
		@RequestHeader("Authorization") String basic_auth);

}

若有其他问题请留言。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: springboot feign调用例子可以这样:1. 创建一个FeignClient接口,定义要调用的远程接口:@FeignClient("springboot-service") public interface SpringbootService { @RequestMapping("/hello") String hello(); }2. 在需要调用的地方注入FeignClient接口:@Autowired private SpringbootService springbootService;3. 调用远程接口:String result = springbootService.hello(); ### 回答2: Spring Boot Feign 是一种用于简化 RESTful 服务调用的声明式 HTTP 客户端。下面是一个简单的基于 Spring Boot Feign调用示例。 首先,需要在项目的 pom.xml 文件中添加FeignSpring Cloud 相关的依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 然后,在主启动类上添加 `@EnableFeignClients` 注解以启用 Feign 客户端: ```java @SpringBootApplication @EnableFeignClients public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ``` 接下来,创建一个 Feign 客户端接口,并使用 `@FeignClient` 注解指定要调用的远程服务: ```java @FeignClient(name = "remote-service") public interface RemoteServiceClient { @GetMapping("/api/user/{id}") UserDTO getUserById(@PathVariable Long id); } ``` 在上述示例中,我们创建了一个名为 `RemoteServiceClient` 的 Feign 客户端接口,并指定其调用的远程服务名称为 "remote-service"。接口中的 `getUserById` 方法用于调用 "remote-service" 服务的 "/api/user/{id}" 接口,并接收一个路径变量 id,返回类型为 UserDTO。 最后,在需要调用远程服务的地方注入 `RemoteServiceClient` 客户端接口,并使用它进行调用: ```java @RestController public class UserController { @Autowired private RemoteServiceClient remoteServiceClient; @GetMapping("/user/{id}") public UserDTO getUserById(@PathVariable Long id) { return remoteServiceClient.getUserById(id); } } ``` 在上述示例中,我们在 UserController 中注入了 `RemoteServiceClient` 客户端接口,并使用它调用远程服务的 getUserById 方法,返回结果作为该接口的返回值。 通过以上步骤,就可以完成一个简单的 Spring Boot Feign 调用示例。注意,还需要配置远程服务的地址和端口等相关配置。 ### 回答3: Spring Boot Feign是一个基于HTTP请求的声明式Web Service客户端。它使用注解方式声明和配置对其他服务的调用,并且提供了一些便捷的功能,使得与其他服务的通信更加简单高效。下面是一个关于使用Spring Boot Feign调用例子。 首先,我们需要在项目的pom.xml文件中添加Feign的依赖。 ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.2.3.RELEASE</version> </dependency> ``` 然后,在启动类上添加@EnableFeignClients注解以启动Feign功能。 ```java @SpringBootApplication @EnableFeignClients public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 接下来,我们创建一个Feign接口,并使用@FeignClient注解指定要调用的服务的名称和URL。 ```java @FeignClient(name = "example-service", url = "http://localhost:8080") public interface ExampleFeignClient { @GetMapping("/example") String getExample(); } ``` 在上面的例子中,我们声明了一个名为ExampleFeignClient的Feign接口,它会调用一个名为example-service的服务,并且该服务的URL是http://localhost:8080。 最后,我们可以在其他组件中使用ExampleFeignClient接口来进行调用。 ```java @RestController public class ExampleController { private final ExampleFeignClient exampleFeignClient; public ExampleController(ExampleFeignClient exampleFeignClient) { this.exampleFeignClient = exampleFeignClient; } @GetMapping("/example-feign") public String invokeExampleService() { return exampleFeignClient.getExample(); } } ``` 上面的例子中,我们在ExampleController中注入了ExampleFeignClient接口,在调用invokeExampleService方法时,实际上是调用了ExampleFeignClient接口中的getExample方法。 这就是一个简单的Spring Boot Feign调用例子。使用Feign能够简化微服务之间的通信,提高开发效率。希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值