Javaプログラム内でHTTPプロキシの認証を突破するための設定

いつも忘れるのでメモ

package sample;

import java.net.Authenticator;
import java.net.PasswordAuthentication;

import org.springframework.web.client.RestTemplate;

public class Main {

    static {
        System.setProperty("http.proxyHost", "proxy host");
        System.setProperty("http.proxyPort", "port");
        Authenticator.setDefault(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("domain\\username", "password"
                        .toCharArray());
            }
        });
    }

    public static void main(String[] args) {
        RestTemplate template = new RestTemplate();
        System.out.println(template.getForEntity("http://google.com",
                String.class));
    }
}