MessageContext mc = context.getMessageContext();
Then one of:
AuthorizationPolicy policy =
(AuthorizationPolicy)mc.get(AuthorizationPolicy.class.getName());
or if you want to use JAX-WS standard calls:
mc.getPrincipal()
to get the User principal from the HTTP stack and pull the username and
password from there.
Dan
On Wednesday 25 August 2010 6:24:56 pm kettch wrote:
> I've got an Axis webservice that I've been charged with moving to CXF.
>
> The one thing that is confounding me is how to get the authentication info.
>
> For example, this is the existing snippet in the Axis version:
>
> MessageContext mc = MessageContext.getCurrentContext();
> try {
> authenticator.authenticate(mc.getUsername(),mc.getPassword());
> ...
> operation=mc.getOperation().getName();
> } catch(Exception e){
> logger.warn("User authentication error - " + mc.getUsername());
> throw new AxisFault(new QName(""),"User Authentication Error",
> "getUser",new Element[]{});
> }
>
> Now as far as I've been able to find online, the "new" version should look
> like:
>
> @Resource private WebServiceContext context; // In the class definition
> ....
>
> MessageContext mc = context.getMessageContext();
> AuthorizationPolicy policy =
> (AuthorizationPolicy)mc.get(AuthorizationPolicy.class);
> try {
> authenticator.authenticate(policy.getUsername(),policy.getPassword());
> ...
> operation = ((QName)mc.get(Message.WSDL_OPERATION)).toString();
> } catch(Exception e){
> logger.warn("User authentication error - " + mc.getUsername());
> throw new AxisFault(new QName(""),"User Authentication Error",
> "getUser",new Element[]{});
> }
>
>
> But I am getting an exception thrown when getting the AuthorizationPolicy
> like this:
>
> AuthorizationPolicy policy =
> (AuthorizationPolicy)mc.get(AuthorizationPolicy.class);
>
> saying that "java.lang.Class cannot be cast to java.lang.String".
>
> All of the examples I've seen online have the exact same code...what am I
> doing wrong? I'm new to CXF so I'm sure it's something stupid. I'm using
> CXF 2.2.10.
--
Daniel Kulp
[email protected]
http://dankulp.com/blog