gpt4 book ai didi

java - 如何增强 CXF 拦截器方法的回调处理程序

转载 作者:行者123 更新时间:2023-12-01 09:58:21 26 4
gpt4 key购买 nike

我在 spring bean 配置文件中创建了一个WSS4JInInterceptor,如下所示

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">

<jaxws:endpoint id="book"
implementor="net.ma.soap.ws.endpoints.IBookEndPointImpl" address="/bookAuth">
<jaxws:inInterceptors>
<bean class="org.apache.cxf.binding.soap.saaj.SAAJInInterceptor"></bean>
<bean class="org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor">
<constructor-arg>
<map>
<entry key="action" value="UsernameToken" />
<entry key="passwordType" value="PasswordText" />
<entry key="passwordCallbackClass" value="net.ma.soap.ws.service.ServerPasswordCallback"></entry>
</map>
</constructor-arg>
</bean>
</jaxws:inInterceptors>
</jaxws:endpoint>
</beans>

ServerPasswordCallBack.java 如下所示

    package net.ma.soap.ws.service;

import java.io.IOException;
import java.util.ResourceBundle;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import org.apache.wss4j.common.ext.WSPasswordCallback;

public class ServerPasswordCallback implements CallbackHandler {

private static final String BUNDLE_LOCATION = "zuth";
private static final String PASSWORD_PROPERTY_NAME = "auth.manager.password";
private static String password;

static {
final ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_LOCATION);
password = bundle.getString(PASSWORD_PROPERTY_NAME);
}

@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
pc.setPassword(password);
}
}

通过密码验证,一切正常。

我想知道是否有其他方法可以增强 handle(Callback) 方法,使其更加复杂,以便它能够检查多个参数,例如,如果我可以让它检查访问 token ,这样会好得多。

密码属性在zuth_fr_FR.properties文件中定义如下

auth.manager.password=najah

最佳答案

如果您想对您的用户名和 token 进行一些自定义验证(例如使用 LDAP 或类似的东西验证目录服务),您可以编写自己的自定义 UsernameTokenValidator 覆盖 verifyPlaintextPassword(UsernameToken usernameToken) UsernameTokenValidator 并将其连接到您的 WSS4JInInterceptor,将以下内容添加到您的 bean 定义中

<property name="wssConfig">
<ref bean="usernameTokenWssConfig"/>
</property>

并将引用的类添加到您的代码库中:

@Component("usernameTokenWssConfig")
public class usernameTokenWssConfigWSSConfig {
public usernameTokenWssConfig() {
setValidator(WSSecurityEngine.USERNAME_TOKEN, new CustomUsernameTokenValidator());
setRequiredPasswordType(WSConstants.PASSWORD_TEXT);
}
}

关于java - 如何增强 CXF 拦截器方法的回调处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37009153/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com