1

JavaのRESTful Webサービスでトークンベースの認証を実装しようとしています。RESTトークンベースの認証が機能しない

これまでのところ、私はNameBindingが

@NameBinding 
@Retention(RetentionPolicy.SOURCE) 
@Target({ElementType.TYPE, ElementType.METHOD}) 
public @interface Secured { } 

2を確保して作成された)次のこと 1を行っているが)

@Secured 
@Provider 
@Priority(Priorities.AUTHENTICATION) 
public class AuthenticationFilter implements ContainerRequestFilter { 

    @Override 
    public void filter(ContainerRequestContext requestContext) throws IOException { 

     // Get the HTTP Authorization header from the request 
     String authorizationHeader = requestContext.getHeaderString(HttpHeaders.AUTHORIZATION); 

     // Check if the HTTP Authorization header is present and formatted correctly 
     if (authorizationHeader == null || !authorizationHeader.startsWith("Bearer")) { 
      throw new NotAuthorizedException("Authorization header must be provided"); 
     } 

     // Extract the token from the HTTP Authorization header 
     String token = authorizationHeader.substring("Bearer".length()).trim(); 

     try { 

      // Validate the token 
      validateToken(token); 

     } catch (Exception e) { 
      requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED).build()); 
     } 
    } 

    private void validateToken(String token) throws Exception { 
     // Check if it was issued by the server and if it's not expired 
     // Throw an Exception if the token is invalid 
    } 

3認証フィルタを作成した)今私は私の上に固定注釈を入れしようとしていた場合何らかの形でそれが動作していないし、正しいjsonが返されます。

@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 from = null; 
Date to = null; 
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
if(!fromDate.equals("")) 
{ 
    from = formatter.parse(fromDate); 
} 

if(!toDate.equals("")) 
{ 
    to = formatter.parse(toDate); 
} 

ArrayList<Document> invoices = (ArrayList<Document>) CustomerBiz.getInvoices(documentumConfigUtil,DocumentType.TAX_INVOICE,account,from,to); 
return invoices; 
} 

私が間違っているところを教えてください。

注:Java Webサービスを作成するためにApache CXFとSpringを使用しました。

答えて

0

私はこの問題を解決しました。実は問題は、私は問題

<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> 
を修正するために以下の行を使用し、私のbeans.xmlの

にあった

関連する問題