【Feign扩展】Feign支持BasicAuth验证

文章介绍了如何在微服务接口中增强安全性,特别是对重要接口使用BasicAuth鉴权。通过在FeignClient中添加自定义配置,创建BasicAuthRequestInterceptor,将账号密码配置在application.yaml中,并在请求头中添加Authorization。这样,每次请求都会自动附带鉴权信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、背景

一些比较重要的微服务,我们暴露的接口可能会希望安全性更高一些,此时,我们会给这些接口增加一些鉴权,如比较简单且方便的鉴权方式Basic Auth鉴权,此时,针对这些有Basic Auth鉴权的接口,我们该如何写Feign,其实是通过覆盖Feign的默认配置来支持鉴权。

二、 步骤

FeignClient的属性configuration增加自定义配置Configuration.class

@FeignClient(name = "mytest-server", configuration = Configuration.class)
public interface Client {
    //..
}

Configuration如下(特别注意,别加@Configuration注解,避免扫包扫到导致给全局的Feign都增加了BasicAuthRequestInterceptor拦截器):

/**
 * basic验证配置
 */
@EnableConfigurationProperties({Properties.class})
public class Configuration {
    @Autowired
    private Properties properties;
    public Configuration() {
    }
    @Bean
    public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
        return new BasicAuthRequestInterceptor(properties.getUsername(), properties.getPassword());
    }
}

其中Properties.java

/**
 * basic验证配置
 */
@ConfigurationProperties(prefix = "mytest")
@Data
public class Properties {
    private String username;
    private String password;
}

application.yaml增加basic auth账号密码配置如下:

mytest:
    username: test
    password: 123456

三、源码解析

核心就是请求头上添加Authorization=Basic xxx

public class BasicAuthRequestInterceptor implements RequestInterceptor {

  private final String headerValue;

  /**
   * Creates an interceptor that authenticates all requests with the specified username and password
   * encoded using ISO-8859-1.
   *
   * @param username the username to use for authentication
   * @param password the password to use for authentication
   */
  public BasicAuthRequestInterceptor(String username, String password) {
    this(username, password, ISO_8859_1);
  }

  /**
   * Creates an interceptor that authenticates all requests with the specified username and password
   * encoded using the specified charset.
   *
   * @param username the username to use for authentication
   * @param password the password to use for authentication
   * @param charset the charset to use when encoding the credentials
   */
  public BasicAuthRequestInterceptor(String username, String password, Charset charset) {
    checkNotNull(username, "username");
    checkNotNull(password, "password");
    this.headerValue = "Basic " + base64Encode((username + ":" + password).getBytes(charset));
  }

  /*
   * This uses a Sun internal method; if we ever encounter a case where this method is not
   * available, the appropriate response would be to pull the necessary portions of Guava's
   * BaseEncoding class into Util.
   */
  private static String base64Encode(byte[] bytes) {
    return Base64.encode(bytes);
  }

  @Override
  public void apply(RequestTemplate template) {
    template.header("Authorization", headerValue);
  }
}

在这里插入图片描述

Edge浏览器无法打开扩展商店时,可以尝试使用"移花接木"的方法来解决。这种方法的原理是,由于Edge浏览器和Google Chrome浏览器的内核相同,可以在Edge上下载扩展程序,然后将下载的扩展程序添加到Chrome中。具体步骤如下: 1. 打开Edge浏览器,进入扩展程序商店。 2. 在商店中找到并下载想要的扩展程序。 3. 打开Chrome浏览器,点击右上角的菜单按钮,选择“更多工具”。 4. 在下拉菜单中选择“扩展程序”。 5. 在扩展程序页面的右上角开启“开发者模式”。 6. 在页面左上角点击“加载已解压的扩展程序”。 7. 在弹出的对话框中选择之前在Edge上下载的扩展程序文件夹,点击“选择文件夹”。 8. 扩展程序将被添加到Chrome浏览器中,可以在浏览器的右上角看到扩展程序图标。 9. 现在可以正常使用扩展程序了。 使用这种方法,可以在Edge浏览器上下载扩展程序并将其添加到Chrome浏览器中,从而解决Edge浏览器无法打开扩展商店的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [chrome无法打开应用商店添加扩展程序的解决方案](https://blog.csdn.net/qq_43550820/article/details/120175053)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [edge浏览器无法打开任何界面包括设置、扩展页面](https://blog.csdn.net/yo_le/article/details/129583886)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值