Springboot:重定向和反向代理

我有一个SpringBoot和SpringMVC internal 应用程序(防火墙内部隐藏的内容)和DMZ中的公共身份验证(OAuth2)服务 .

我正在从内部区域的客户端访问登录页面 . 此页面有一个登录按钮 . 当我按下它时,我将客户端转发到auth服务器(在DMZ中),我只能通过代理访问 .

我尝试设置VM env变量:

-Dhttp.proxyHost=xx -Dhttp.proxyPort=yy -Dhttp.proxySet=true

并在 System.properties 中设置它们

System.setProperty("http.proxyHost", "http://xx");
System.setProperty("http.proxyPort", "xx");
System.setProperty("http.nonProxyHosts", "localhost|127.0.0.1");

但两者都没有效果 .

我还试图定义一个 SimpleClientHttpRequestFactory bean:

<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/util 
   http://www.springframework.org/schema/util/spring-util.xsd">

    <bean id="requestFactory"
        class="org.springframework.http.client.SimpleClientHttpRequestFactory">
        <property name="proxy">
            <bean id="proxy" class="java.net.Proxy">
                <constructor-arg>
                    <util:constant static-field="java.net.Proxy.Type.HTTP" />
                </constructor-arg>
                <constructor-arg>
                    <bean class="java.net.InetSocketAddress">
                        <constructor-arg value="xx" />
                        <constructor-arg value="yy" />
                    </bean>
                </constructor-arg>
            </bean>
        </property>
    </bean>

</beans>

没有任何成功 .

Question

如何配置Spring来代理我的重定向?

谢谢!

回答(1)

2 years ago

得到它如下工作(见 restTemplateProxy

private final String server = "xx";
private final int port = yy;
private final SimpleClientHttpRequestFactory clientHttpReq = new SimpleClientHttpRequestFactory();
private final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(server, port));
private RestTemplate restTemplateProxy  = null;

{
    clientHttpReq.setProxy(proxy);
    restTemplateProxy = new RestTemplate(clientHttpReq);
}

@RequestMapping(value = "getLightToken", method = RequestMethod.GET)
private String getLightToken(Model model) throws JsonProcessingException, IOException {

    /* Header */
    headers.clear();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

    /* Body */
    MultiValueMap<String, String> body = new LinkedMultiValueMap<String, String>();     
    body.add(CLIENT_ID.key, CLIENT_ID.val);
    body.add(CLIENT_SECRET.key, CLIENT_SECRET.val);
    body.add(GRANT_TYPE.key, GRANT_TYPE.val);
    body.add(SCOPE.key, SCOPE.val);

    /* Set the body and header of the request  */
    HttpEntity<?> request = new HttpEntity<>(body, headers);

    /* Request Authorisation */
    ResponseEntity<String> response = restTemplateProxy.exchange(BASE_URL + TOKEN_URI, HttpMethod.POST, request, String.class);

    return response.getBody();
}