如何读取 ws-security 中提供的用户或标识符

发布于 2024-08-10 18:23:56 字数 978 浏览 4 评论 0原文

我正在使用最新的 Apache CXF 来创建 Web 服务。我已经使用 org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor 配置了 ws-security。在我的 passwordCallbackClass 中,我可以通过调用 org.apache.ws.security.WSPasswordCallback 类的 getIdentifier() 方法来访问用户或标识符。

我还在整个设置中使用了 spring。

我想知道如何在代码中的其他位置访问标识符?我可以考虑在我的 passwordCallbackClass 中使用 ThreadLocal 吗?另一种方法是在我的所有服务方法参数中定义标识符属性,但这意味着客户端需要传递标识符两次,一次在安全块中,一次在服务调用中?


Edit

我的服务类如下所示,我需要读取 sayHi 方法中的标识符。

@WebService(endpointInterface = "com.webservice.HelloWorld")  
public class HelloWorldImpl implements HelloWorld {  
    public String sayHi(String text) {  
        return "Hello " + text;  
    }  
}

我的密码回调方法是我可以获取标识符的地方。

public void handle(Callback[] callbacks) throws IOException,UnsupportedCallbackExceptio {  
    WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];  
    pc.getIdentifier();  
}

I am using latest Apache CXF to create a webservice. I have configured ws-security using org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor. In my passwordCallbackClass I can access the user or identifier by calling getIdentifier() method of org.apache.ws.security.WSPasswordCallback class.

I am also using spring for the whole setup.

I would like to know how to access the identifier any where else in the code? I could think of using ThreadLocal in my passwordCallbackClass? Another way is define to an identifier property in all my service method parameters but that would mean client need to pass the identifier twice, one in the security block and again in the service call?


Edit

My service class looks like this and I need to read the Identifier in sayHi method.

@WebService(endpointInterface = "com.webservice.HelloWorld")  
public class HelloWorldImpl implements HelloWorld {  
    public String sayHi(String text) {  
        return "Hello " + text;  
    }  
}

My Password callback method is this where I can get the Identifier.

public void handle(Callback[] callbacks) throws IOException,UnsupportedCallbackExceptio {  
    WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];  
    pc.getIdentifier();  
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月光色 2024-08-17 18:23:56

我在密码回调方法中使用 ThreadLocal 来存储标识符。然后我在代码中的任何位置访问 ThreadLocal 以了解标识符。

I used ThreadLocal in my Password callback method to store the Identifier. I then access the ThreadLocal anywhere in the code to know the identifier.

四叶草在未来唯美盛开 2024-08-17 18:23:56

定义“代码中的任何其他位置”?你是在谈论其他拦截器吗?在你的服务器实现中?您使用的是 JAXWS 还是简单的前端?等等...

如果使用 jaxws 并且您希望在服务器实现中使用它,则 JAXWS WebServiceContext 有一个 getUserPrincipal() 方法,该方法只是 WSS4J 在验证用户后创建的主体。

几乎在其他任何地方(甚至在使用上下文的服务器实现中),您都可以执行以下操作:

message.get(SecurityContext.class).getUserPrincipal();

达到同样的结果。

Define "any where else in the code"? Are you talking about in other interceptors? In your server implementation? Are you using JAXWS or the simple frontend? Etc...

If using jaxws and you want it in your server impl, the JAXWS WebServiceContext has a getUserPrincipal() method that would just be a principal that WSS4J creates after you validate the user.

Pretty much anywhere else (or even in the server impl using the context), you can do something like:

message.get(SecurityContext.class).getUserPrincipal();

to achieve the same result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文