I have a method that looks like this, and is used by the client in the manner shown in the 2nd code snippet. I am using Spring beans in my app, so I am wondering if Spring has their own way of doing this? I can inject the username/password easily enough, but I am wondering if there is a Spring-centric approach.
The client is accessing a password protected URL that is hosting a web service on a private network
Code:import java.net.Authenticator; public class MyAuthenticator extends Authenticator { protected PasswordAuthentication getPasswordAuthentication() { String promptString = getRequestingPrompt(); String hostname = getRequestingHost(); InetAddress ipaddr = getRequestingSite(); int port = getRequestingPort(); String username = "myusername"; String password = "mypassword"; return new PasswordAuthentication(username, password.toCharArray()); } }
Client code:
Code:Authenticator.setDefault(new MyAuthenticator());


Reply With Quote
