---
title: Javaプログラム内でHTTPプロキシの認証を突破するための設定
tags: []
categories: ["Programming", "Java", "java", "net", "Authenticator"]
date: 2013-10-28T00:29:13Z
updated: 2013-10-28T00:29:13Z
---

いつも忘れるのでメモ

    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));
        }
    }
