【发布时间】:2014-07-25 19:34:16
【问题描述】:
我已经搜索了一个很好的例子,但找不到一个。我想从 SOAP 标头中获取用户名和密码,并在使用我们现有的服务方法进行身份验证后设置 spring 安全上下文。我已经实现了 Wss4jSecurityInterceptor 并验证了标头元素。我需要在回调或其他机制中做的是创建一个 uthetication 上下文,以便稍后在我们的端点中访问它。
但是,我不认为回调是正确的地方,因为我不断收到密码,没有密码错误。我是 Spring 安全和集成的新手。
配置:
<bean id="SOAPSecurityInterceptor" class="com.ps.snt.ws.interceptor.SOAPSecurityInterceptor">
<property name="validationActions" value="UsernameToken"/>
<property name="validationCallbackHandler" ref="callbackHandler"/>
</bean>
<bean id="callbackHandler" class="com.ps.snt.ws.interceptor.SOAPSecurityValidationCallbackHandler">
</bean>
回调:
public class SOAPSecurityValidationCallbackHandler extends SimplePasswordValidationCallbackHandler {
@Override
protected void handleUsernameToken(WSPasswordCallback callback) throws IOException, UnsupportedCallbackException {
System.out.println("In security callback " + callback.getPassword());
boolean valid = true;
String token = callback.getIdentifier();
String password = callback.getPassword();
Integer zoneID = null;
String username = null;
StringBuffer errorMessages = new StringBuffer();
if(StringUtils.isEmpty(token)) {
errorMessages.append("Username token cannot be empty");
valid = false;
} else {
Pattern pattern = Pattern.compile("^[\\w]+\\d\\d\\d\\d\\d");
Matcher matcher = pattern.matcher(token);
if(!matcher.matches()) {
valid = false;
errorMessages.append("Username token must be in the format 'user@zone'.");
}
else {
String[] parts = token.split("@");
username = parts[0];
zoneID = Integer.parseInt(parts[1]);
}
}
if(StringUtils.isEmpty(password)) {
errorMessages.append("Password cannot be empty.");
valid = false;
}
if(valid && username != null && zoneID != null) {
LoginService loginService = new LoginService();
LoginContextDO loginContextDO = loginService.getAuthenticatedLoginContext(username, password, zoneID);
AbstractAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(username, password);
authentication.setDetails(loginContextDO);
authentication.setAuthenticated(true);
SecurityContextHolder.getContext().setAuthentication(authentication);
} else {
System.out.println("Authetnication failed!");
}
}
}
我的要求很简单: - 验证 SOAP 标头(有效) - 检索用户名和密码 - 调用我们的旧服务来创建我们的登录上下文 - 设置 spring 安全上下文(以 logincontext 作为详细信息),以便稍后在端点中使用
我可以使用什么机制来验证soap标头并从该标头设置安全上下文?
【问题讨论】: