Created
December 25, 2018 13:33
-
-
Save alswl/57d446c87cbe52e1d2548c8a2d5e4962 to your computer and use it in GitHub Desktop.
spring basic auth for HTTP bean configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation=" | |
http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"> | |
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"> | |
<constructor-arg ref="httpClientFactory" /> | |
<property name="messageConverters"> | |
<list> | |
<bean class="org.springframework.http.converter.json.GsonHttpMessageConverter" /> | |
</list> | |
</property> | |
</bean> | |
<bean id="httpClientFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory"> | |
<constructor-arg ref="httpClient" /> | |
</bean> | |
<bean id="httpClient" class="org.apache.http.impl.client.DefaultHttpClient"> | |
<property name="credentialsProvider" ref="credentialProvider"/> | |
</bean> | |
<!-- Credentials provider needed by apache default http client --> | |
<bean id="credentialProvider" class="org.apache.http.impl.client.BasicCredentialsProvider" /> | |
<!-- Used to invoke a method in BasicCredentialsProvider. This has to be done this way as BasicCredentialsProvider does not take | |
provider and credentials in constructor or setter method. It has to set by invoking setCredentials() method and passing two arguments --> | |
<bean id="methodInvoke" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> | |
<property name="targetObject" ref="credentialProvider"/> | |
<property name="targetMethod" value="setCredentials"> </property> | |
<property name="arguments" > | |
<list> | |
<ref bean="authScope" /> | |
<ref bean="credentials" /> | |
</list> | |
</property> | |
</bean> | |
<!-- Authorization scope for accessing restful service. Since we want this template to be used for everything, we are setting up it with defaults --> | |
<bean id="authScope" class="org.apache.http.auth.AuthScope"> | |
<constructor-arg name="host"><null /></constructor-arg> | |
<constructor-arg><value>-1</value> </constructor-arg> | |
<constructor-arg><null /></constructor-arg> | |
<constructor-arg><null /></constructor-arg> | |
</bean> | |
<!-- Username and Password Credentials to access restful service --> | |
<bean id="credentials" class="org.apache.http.auth.UsernamePasswordCredentials"> | |
<constructor-arg name="userName"><value>john.doe</value></constructor-arg> | |
<constructor-arg name="password"><value>johnspassword</value></constructor-arg> | |
</bean> | |
</beans> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment