2012-07-02 3 views
1

Authenticator 클래스가있는 웹 서비스 클라이언트가 있습니다. 인증자는 사용자 이름/암호가 필요합니다. Spring을 사용하여 자격 증명을 주입하는 방법에 대한 도움말을 찾고 있습니다.자체에 생성자가있는 빈 와이어 연결

인증 자 또는 인증자를 인스턴스화하는 클라이언트에 사용자/패스를 주입해야합니까?

나는 봄에 처음 왔기 때문에 어떤 구체적인 예도 인정 될 것이다. 자격 증명,

@Controller 
    public class WSClient { 
     @Autowired 
     MyAuthenticator myAuthenticator; 
    } 
} 

인증 :이

는 두 가지 구성 요소가 어떻게 생겼는지 있습니다

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

답변

1

사용 @ValueAuthentication 콩에서 사용자 이름/암호를 설정

@Component 
public class MyAuthenticator extends Authenticator { 
    @Value("${credentials.username}") 
    private final String userName; 
    @Value("${credentials.password}") 
    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()); 
    } 
} 

및 XML 파일

<util:properties id="credentials" location="classpath:credentials.properties"/> 

추가하고 클래스 경로

credentials.properties를 넣어