3

For my own hosted JAX-WS (Soap) web services, I can add handlers by defining a handler-chain file and adding them there, for instance:

@WebService(serviceName = "My_Service", portName = "My_Port_Soap12", targetNamespace = "urn:mynamespace")
@HandlerChain(file = "handler-chain.xml")
public class MyService { ... }

In my handler-chain.xml:

<?xml version="1.0" encoding="UTF-8"?>
<handler-chains xmlns="http://java.sun.com/xml/ns/javaee">
   <handler-chain>
     <handler>
      <handler-class>MyHandler</handler-class>
     </handler>
   </handler-chain>
</handler-chains>

Is there any way I can do the same for a JAX-RS Rest service?

I need some custom authentication logic which I need to add to my services.

Davio
  • 4,609
  • 2
  • 31
  • 58
  • 1
    Do you mean like an interceptor/filter? I'm not familiar with JAX-WS and term "handler" in this context? As far as authentication goes, you can write a [`ContainerRequestFilter`](https://javaee-spec.java.net/nonav/javadocs/javax/ws/rs/container/ContainerRequestFilter.html) with JAX-RS 2. See [here](http://stackoverflow.com/a/28271760/2587435) – Paul Samsotha Feb 12 '15 at 09:36
  • Yes, in my handler, I can get a context and stuff like that, so I can look at some headers and do some preprocessing. – Davio Feb 12 '15 at 11:19
  • If you are using JAX-RS 2, then you can use the `ContainerRequestFilter` like I mentioned. You can get all that you mentioned from the `ContainerRequestContext` passed to the filter. A lower version, then the filters are implementation specific. Or you can just write a regular servlet filter – Paul Samsotha Feb 12 '15 at 11:21
  • That's a good start, is there any way to redirect the user to some sort of login page (or ask for Basic auth credentials)? – Davio Feb 12 '15 at 13:51
  • Your servlet container already has support for this. There's a link to the Java EE tutorial, in the link I provided above. That will show you how to set up Basic Auth in the web.xml. You will need to look into the documentation of your server for how to configure the store that holds the user/credentials. The link also has a link to a complete example not using the servlet feature, but writing your own filter to handle Basic Auth – Paul Samsotha Feb 12 '15 at 13:57
  • I could not find `AuthenticationException`, nor its mapper. – Davio Feb 12 '15 at 14:19
  • https://github.com/jersey/jersey/tree/master/examples/https-clientserver-grizzly/src/main/java/org/glassfish/jersey/examples/httpsclientservergrizzly – Paul Samsotha Feb 12 '15 at 14:28

0 Answers0