2015-06-25 55 views
0

我正在使用澤西島(java)框架。我使用容器請求過濾器基於cookie進行了身份驗證。現在我必須做授權。那麼,我該如何繼續?請快速指導。澤西框架授權

回答

1

澤西島有@RolesAllowed("role")註釋以方便驗證檢查。使用:

@Context 
HttpServletRequest httpRequest;` 

,並在登錄方法把身份進入會議喜歡這裏:

HttpSession session = httpRequest.getSession(true); 
session.setAttribute(key, val); 

過濾器

final String name = session.getAttribute(key); 

... 

SecurityContext securityContext = new SecurityContext() { 

    public boolean isUserInRole(String roleName) { 
     return roleName.equals("role"); 
    } 

... 

    public Principal getUserPrincipal() { 

     ... 

     return new Principal() { 
      public String getName() { 
       return name; 
      } 
     }; 

     ... 

    } 

... 

}; 

requestContext.setSecurityContext(securityContext); 

這是它的短。這是相當普遍的做法。如果你想我可以在GitHub上分享ref impl。

+0

謝謝。請分享.. –

+0

Ref impl is [here](https://github.com/fedon/spring-jersey-auth) – Dmytro

+0

對不起,這是錯誤的項目。現在我推了適當的一個。 – Dmytro