【问题标题】:What is the replacement for deprecated BasicAuthorizationInterceptor?已弃用的 BasicAuthorizationInterceptor 的替代品是什么?
【发布时间】:2022-03-18 02:30:57
【问题描述】:

我正在使用新的 BasicAuthorizationInterceptor 在 oauth2.0 中进行基本身份验证。我找不到已弃用的 BasicAuthorizationInterceptor 的替代品。请帮帮我

【问题讨论】:

标签: java spring oauth-2.0 spring-framework-beans


【解决方案1】:

使用BasicAuthenticationInterceptor 对我有用。

【讨论】:

    【解决方案2】:

    来自BasicAuthorizationInterceptor 文档:

    已弃用,从 5.1.1 开始,支持重用 HttpHeaders.setBasicAuth(java.lang.String, java.lang.String) 的 BasicAuthenticationInterceptor,改为共享其默认字符集 ISO-8859-1此处使用的 UTF-8 格式

    【讨论】:

      【解决方案3】:

      使用BasicAuthenticationInterceptor同时也在寻找ClientHttpRequestInterceptor的接口

      【讨论】:

        【解决方案4】:

        这就是解决我的问题的方法:

        • 支持BasicAuthenticationInterceptor,它重用HttpHeaders.setBasicAuth(java.lang.String, java.lang.String),共享其默认字符集ISO-8859-1,而不是这里使用的UTF-8 1

        【讨论】:

        【解决方案5】:

        我回答得有点晚了,但上面的大多数答案都说可以使用 BasicAuthenticationInterceptor 代替现在已弃用的 BasicAuthorizationInterceptor。但是如果你看到两者的实现,你会发现在 BasicAuthenticationInterceptor 中重写了拦截方法,如下所示:

          public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
                HttpHeaders headers = request.getHeaders();
                if (!headers.containsKey("Authorization")) { // adds only if no Authorization header is absent
                    headers.setBasicAuth(this.encodedCredentials);
                }
        
                return execution.execute(request, body);
            }
        

        BasicAuthorizationInterceptor 在被覆盖的拦截方法中有以下内容:

            public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
                String token = Base64Utils.encodeToString((this.username + ":" + this.password).getBytes(StandardCharsets.UTF_8));
                request.getHeaders().add("Authorization", "Basic " + token); // no condition check
                return execution.execute(request, body);
            }
        

        作为一个快速总结,BasicAuthenticationInterceptor 检查是否存在 Authorzation 标头,并且仅在不存在但 BasicAuthorizationInterceptor 没有该检查时添加。

        所以基本的解决方案是创建一个自定义拦截器,如果你想添加多个授权头,你可以创建它引用 BasicAuthorizationInterceptor 类。

        更多信息here

        【讨论】:

          猜你喜欢
          • 2016-05-25
          • 2019-04-05
          • 1970-01-01
          • 1970-01-01
          • 2017-12-17
          • 2021-05-26
          • 2021-12-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多