java - REST Token based Authentication not working -


i trying implement token based authentication in java restful web service.

so far have done following things 1) created namebinding secured

@namebinding @retention(retentionpolicy.source) @target({elementtype.type, elementtype.method}) public @interface secured { } 

2) created authentication filter

@secured @provider @priority(priorities.authentication) public class authenticationfilter implements containerrequestfilter {      @override     public void filter(containerrequestcontext requestcontext) throws ioexception {          // http authorization header request         string authorizationheader = requestcontext.getheaderstring(httpheaders.authorization);          // check if http authorization header present , formatted correctly          if (authorizationheader == null || !authorizationheader.startswith("bearer")) {             throw new notauthorizedexception("authorization header must provided");         }          // extract token http authorization header         string token = authorizationheader.substring("bearer".length()).trim();          try {              // validate token             validatetoken(token);          } catch (exception e) {             requestcontext.abortwith(response.status(response.status.unauthorized).build());         }     }      private void validatetoken(string token) throws exception {         // check if issued server , if it's not expired         // throw exception if token invalid     } 

3) when trying put secured annotation on service method somehow not working , correct json returned.

@get @secured @path("{custid}/invoices") @produces({"application/json"}) @consumes({"application/x-www-form-urlencoded"})  public list<document> getcustomerinvoices(         @pathparam("custid") string account,         @defaultvalue("") @queryparam("fromdate") string fromdate,         @defaultvalue("") @queryparam("todate") string todate) throws exception{ date = null; date = null; simpledateformat formatter = new simpledateformat("yyyy-mm-dd"); if(!fromdate.equals("")) {     = formatter.parse(fromdate); }  if(!todate.equals("")) {     = formatter.parse(todate); }  arraylist<document> invoices = (arraylist<document>) customerbiz.getinvoices(documentumconfigutil,documenttype.tax_invoice,account,from,to); return  invoices; } 

please suggest me doing wrong.

note: have used apache cxf , spring create java web service.

i have solved issue. problem in beans.xml

i used following lines fix problem

<jaxrs:server id="customerresource" address="/customers">         <jaxrs:servicebeans>             <ref bean="customerresource" />         </jaxrs:servicebeans>         <jaxrs:providers>             <ref bean='jsonprovider' />             <ref bean='authenticationfilter' />         </jaxrs:providers>      </jaxrs:server> 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -