微服务通过feign调用/oauth/token方法

OAuth2获取token原生方法

服务内部通过feign调用/oauth/token方法

  1. 创建FeignClient接口类
    1. import com.goldnet.core.launch.constant.AppConstant;
      import org.springframework.cloud.openfeign.FeignClient;
      import org.springframework.util.MultiValueMap;
      import org.springframework.web.bind.annotation.RequestBody;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestMethod;
      
      /**
       * @Author 
       * @Date 2020/12/9 11:50
       * @Version 1.0
       */
      @FeignClient(
      	value = AppConstant.APPLICATION_AUTH_NAME,
      	fallback = AuthFeignClientFallback.class
      )
      public interface AuthFeignClient {
      	@RequestMapping(method = RequestMethod.POST, value = "/oauth/token")
      	Object postAccessToken(@RequestParam MultiValueMap<String, String> parameters, @RequestHeader MultiValueMap<String, String> headers);
      }
      
      import org.springframework.stereotype.Component;
      import org.springframework.util.MultiValueMap;
      
      /**
       * @Author 
       * @Date 2020/12/9 14:45
       * @Version 1.0
       */
      @Component
      public class AuthFeignClientFallback implements AuthFeignClient {
      	@Override
      	public Object postAccessToken(MultiValueMap<String, String> map) {
      		return "获取token失败";
      	}
      }

      参数解释:

      1. AppConstant.APPLICATION_AUTH_NAME  服务提供者的服务名
      2. "aHg6aHhfc2VjcmV0" 是client_id:client_secret 通过base64编码获得----项目要求,视情况而定
      3. Tenant-Id 租户id----项目要求,视情况而定
  2. 调用方法
    1. HttpServletRequest request = WebUtil.getRequest();
      String tenantId = request.getHeader("Tenant-Id");
      String authorization = request.getHeader("Authorization");
      MultiValueMap<String,String> headers = new LinkedMultiValueMap<>();
      headers.add("Content-Type","multipart/form-data");
      headers.add("Authorization",authorization);
      headers.add("Tenant-Id",tenantId);
      //body
      MultiValueMap<String,String> body = new LinkedMultiValueMap<>();
      body.add("username","11");
      body.add("password","11");
      body.add("grant_type","password");
      //调用auth服务
      log.info("[接口名称:调用auth][请求数据body:{}][请求数据headers:{}]",body,headers);
      Object token = oAuthFeignClient.postAccessToken(body, headers);

### Feign 客户端传递 Token 的实现 为了使 Feign 客户端能够在请求头中携带 `Authorization` 字段来传输 token,在 Spring Cloud Security OAuth2 环境下,可以通过自定义拦截器的方式完成这一操作。具体来说,创建实现了 `RequestInterceptor` 接口的类用于修改即将发出的 HTTP 请求。 当构建基于 Feign 的 RESTful API 调用时,可以利用此类向目标服务发送带有认证信息(即 Bearer Token)的请求[^1]: ```java import feign.RequestInterceptor; import feign.RequestTemplate; import org.springframework.security.oauth2.provider.OAuth2Authentication; import org.springframework.security.core.context.SecurityContextHolder; public class FeignClientRequestInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate template) { // 获取当前上下文中的安全认证对象 OAuth2Authentication authentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication(); if (authentication != null && authentication.getOAuth2Request() != null){ String tokenValue = ((org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) authentication.getOAuth2Request().getAccessToken()).getValue(); // 将获取到的Token加入HTTP头部作为授权凭证 template.header("Authorization", "Bearer " + tokenValue); } } } ``` 上述代码片段展示了如何通过访问 Spring Security 上下文中保存的安全令牌,并将其附加到每一个由该 Feign 客户端发起的服务间通信请求上。这使得下游微服务能够验证这些请求的身份合法性并据此提供相应资源访问权限[^2]。 此外,为了让此拦截器生效,还需要确保其被正确注册到了应用程序配置文件当中,通常是在启动类或特定配置类里添加相应的 Bean 注册逻辑。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值