admin管理员组文章数量:1391995
I am using SOAP to call API, and I can't use RestTemplate
so I found the way use ProxySelector
.
But when my proxy is set with auth (username/password) I can't use SOAP with ProxySelector
although I also set Authenticator
in ProxySelector
.
I get the message:
HTTP transport error: java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 407 Proxy Authentication Required
I'm sure my username and password for my proxy is correct. This is the code:
@Component
@Slf4j
public class CustomProxySelector extends ProxySelector {
private ProxySelector def;
private final List<Proxy> proxies = new ArrayList<>();
@Value("${proxy-host:#{null}}")
private String proxyHost;
@Value("${proxy-port:#{null}}")
private Integer proxyPort;
@Value("${proxy-username:#{null}}")
private String username;
@Value("${proxy-password:#{null}}")
private String password;
@Value("${api}")
private String api;
@Value("${proxy.enable:false}")
private boolean proxy;
@PostConstruct
public void init() {
this.def = ProxySelector.getDefault();
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, (null == proxyPort) ? 80 : proxyPort));
this.proxies.add(proxy);
ProxySelector.setDefault(this);
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password.toCharArray());
}
});
}
@Override
public List<Proxy> select(URI uri) {
if (StringUtilspare(uri.toString(), this.api) == 0 && Boolean.TRUE.equals(this.enableProxy)) {
log.info(logString, uri, this.proxies);
return this.proxies;
}
log.info(logString, uri, def.select(uri));
return def.select(uri);
}
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
log.info("Failed to connect to a proxy ({}) when connecting to {}", sa, uri.getHost());
if (sa == null || ioe == null) {
throw new IllegalArgumentException("Arguments can't be null.");
}
def.connectFailed(uri, sa, ioe);
}
}
本文标签: javaSet Authentication for Proxy with ProxySelectorStack Overflow
版权声明:本文标题:java - Set Authentication for Proxy with ProxySelector - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744591390a2614511.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论