I have a web service client that has an Authenticator class. The Authenticator requires a username/password. Looking for help on how to inject the credentials using Spring.
Should I inject the user/pass into the Authenticator or into the client that is instantiating the Authenticator.
Any concrete examples would be appreciated, as I am new to Spring.
These are what the two components look like:
@Controller
public class WSClient {
@Autowired
MyAuthenticator myAuthenticator;
}
}
The Authenticator, with the credentials:
public class MyAuthenticator extends Authenticator {
private final String userName;
private final String passWord;
public MyAuthenticator(String userName, String passWord) {
this.userName = userName;
this.passWord = passWord;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.userName, this.passWord.toCharArray());
}
}
Use
@Valueto set username/password inAuthenticationbeanand in XML file
add
and put
credentials.propertiesin classpath