spring启动应用程序代理身份验证407

jbose2ul  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(422)

嗨,我在我的spring boot项目中遇到了proxy 407身份验证错误。我尝试了两种情况proxyhost和proxyport值正常工作,但用户名和密码无效?有人会遇到这种情况吗?在我的本地计算机上,当我提供端口和主机,但不提供用户名密码时,它会使我的本地用户正常工作,但我给用户名和密码的格式不对,看它的效果?我没有效果。另外,在服务器上我得到了407身份验证代理例外。如何在 Spring 启动传递代理用户名和密码?谢谢
我添加了jvm参数,但是proxyuser和proxypassword无效

-Dhttps.proxyHost=something -Dhttps.proxyPort=5555-Dhttps.proxyUser=xxx -Dhttps.proxyPassword=yyy

我还向MyRestTemplateBuilder添加了一些代理代码,但没有效果。

RestTemplate restTemplate=new RestTemplateBuilder()
            .build();
        int proxyPortNum =5555;
        String proxyHost="something";

         CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(proxyHost, proxyPortNum), new UsernamePasswordCredentials("myname", "333"));

         HttpClientBuilder clientBuilder = HttpClientBuilder.create();
        clientBuilder.useSystemProperties();
        clientBuilder.setProxy(new HttpHost(proxyHost, proxyPortNum));
        clientBuilder.setDefaultCredentialsProvider(credsProvider);
        clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());
         CloseableHttpClient client = clientBuilder.build();
 HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
    factory.setHttpClient(client);

    restTemplate.setRequestFactory(factory);
yv5phkfx

yv5phkfx1#

你应该试试this:-----

private RestTemplate createRestTemplate() throws Exception {
        final String username = "myname";
        final String password = "333";
        final String proxyUrl = "something";
        final int port = 5555;

        HttpHost myProxy = new HttpHost(proxyUrl, port);
       HttpClientBuilder clientBuilder = HttpClients.custom();
List<Header> headers = new ArrayList<>();
BasicHeader authHeader = new BasicHeader("Authorization",base64authUserPassword());
headers.add(authHeader);
RequestDefaultHeaders reqHeader = new RequestDefaultHeaders(headers);
clientBuilder.addInterceptorLast(reqHeader);
        clientBuilder.setProxy(myProxy).disableCookieManagement();

        HttpClient httpClient = clientBuilder.build();
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setHttpClient(httpClient);

        return new RestTemplate(factory);
    }

public String base64authUserPassword() {
byte[] baseencode = Base64.getEncoder().encode((username + ":" + password).getBytes());
return "Basic " + new String(baseencode);
}

希望它能解决你的问题。

相关问题