2013-08-06 88 views
15

我使用JBoss ASJAX-RS來創建REST端點。JAX-RS:如何保護REST端點?

比方說我的課看起來像

@Path("/users") 
public class UserResource { 


    @GET 
    public Response getAccount() { 
    return "hello"; 
    } 
} 

現在getAccount沒有在此刻

通緝認證
- 我想補充的認證,這樣,當代碼擊中getAccount用戶已認證
- 我希望身份驗證由註釋而非XML配置驅動,如果可能的話
- 我想對數據庫做比較,看用戶是否有效

問題
- 我從來沒有做過,所以我不知道如何實現它
- 我身邊有很多用Google搜索發現澤西例子

UPDATE
- 我想送認證證書與每個請求,並創建任何會話

請指導我一個簡單的工作示例,我會嘗試從此延伸

+0

你要要求身份驗證憑證會傳入每個請求中,還是要允許創建會話? – thatidiotguy

+0

我剛剛更新了我的問題,我想每個請求發送憑據,而不是創造 – daydreamer

+0

我已經實現瞭解決方案 請參閱我的答案在http://stackoverflow.com/questions/18345600/how-任何會議to-intercept-rest-endpoint-to-all-all-headers?answertab =最老的#tab-top – daydreamer

回答

10

我用下面的代碼解決了這個問題。一旦我做

我已經通過修改攔截我已經解決了這個

令牌機制將被更新,下面是代碼

註釋

@Inherited 
@InterceptorBinding 
@Target({ ElementType.TYPE, ElementType.METHOD }) 
@Retention(RetentionPolicy.RUNTIME) 
public @interface SecurityChecked { 

} 

資源類

public class SecureResource { 

    @GET 
    @SecurityChecked 
    public Response getUser() { 
     return Response.ok("authenticated successfully!").build(); 
    } 
} 

攔截器類

@Interceptor 
@Provider 
@ServerInterceptor 
@SecurityChecked 
public class SecurityCheckInterceptor implements PreProcessInterceptor, AcceptedByMethod { 
    private static final Logger LOGGER = LoggerFactory.getLogger(SecurityCheckInterceptor.class); 

    @Nullable 
    @Override 
    public ServerResponse preProcess(final HttpRequest request, final ResourceMethod method) throws Failure, WebApplicationException { 
     final List<String> authToken = request.getHttpHeaders().getRequestHeader("X-AUTH"); 

     if (authToken == null || !isValidToken(authToken.get(0))) { 
      final ServerResponse serverResponse = new ServerResponse(); 
      serverResponse.setStatus(Response.Status.UNAUTHORIZED.getStatusCode()); 
      return serverResponse; 
     } 

     return null; 
    } 

    private static boolean isValidToken(@Nonnull final String authToken) { 
     LOGGER.info("validating token: " + authToken); 
     return true; 
    } 

    @SuppressWarnings("rawtypes") 
    @Override 
    public boolean accept(final Class declaring, final Method method) { 
     // return declaring.isAnnotationPresent(SecurityChecked.class); // if annotation on class 
     return method.isAnnotationPresent(SecurityChecked.class); 
    } 
} 

,然後我通過在JBoss中部署的資源類和發行以下的命令行命令運行我的集成測試

curl --header 'X-AUTH: 1a629d035831feadOOO4uFReLyEW8aTmrCS' http://localhost:8080/market-1.0-SNAPSHOT/rest/login 
curl --header 'InvalidHeader: InvalidHeaderValue' http://localhost:8080/market-1.0-SNAPSHOT/rest/login 
16

您需要的是JAX RS端點前面的無狀態Spring安全配置。 我已經解決了,你正在試圖解決具體問題,但我沒有我自己的代碼共享..

這裏是一個項目,做你所要求的確切的事情,有些聰明的人做了這一切你;)

https://github.com/philipsorst/angular-rest-springsecurity

是什麼魔法?

  1. 你有這確實驗證一個未受保護的URL,並設置用戶角色以及..
  2. 然後返回某種令牌,把它放在一些地方在緩存中,這將在每個後續調用所預期..
  3. 對其他受保護資源發出新的請求後,您將檢查令牌是否存在於緩存/會話存儲中(您需要一些機制來跟蹤有效令牌)
  4. 如果令牌被重新發送並且有效,在Spring Security中執行程序化登錄,以確保您可以使用Spring提供的所有安全功能,(註釋,JSTL標籤等。 )!
  5. 一旦通過令牌驗證,您將在您的控制器(又名JAX RS資源)中獲得已登錄的用戶詳細信息以進一步處理安全問題。
  6. 如果令牌無效或不存在,它將被故障困住(401)

請參考以下鏈接瞭解如何配置無狀態Spring Security。, https://github.com/philipsorst/angular-rest-springsecurity/blob/master/src/main/resources/context.xml

查看用戶是如何驗證用於第一時間,並且被生成的令牌.. https://github.com/philipsorst/angular-rest-springsecurity/blob/master/src/main/java/net/dontdrinkandroot/example/angularrestspringsecurity/rest/resources/UserResource.java

這裏是被令牌 檢查之後在每次請求進行編程登錄的類.. https://github.com/philipsorst/angular-rest-springsecurity/blob/master/src/main/java/net/dontdrinkandroot/example/angularrestspringsecurity/rest/AuthenticationTokenProcessingFilter.java

+0

真的幫了我 – daydreamer

+0

這對我也有幫助。由於我需要以不同的方式檢查一些細節,因此我最終選擇了「只是另一個演示」。儘管基礎知識非常相似(不能否認靈感來自這裏),但我放棄了UI和JPA/DB,並更多地關注Spring Security。結果是[here](https://github.com/virgo47/restful-spring-security),它的評論是基於Gradle的,包含基於curl的test.sh。然後我寫了一篇關於這個話題的[博客文章](http://virgo47.wordpress.com/2014/07/27/restful-spring-security-with-authentication-token/)以及一些序列圖。應該有幫助。 – virgo47

+1

嘿拉克什,我已經檢查了github上的angular-rest-springsecurity項目,我有一個問題,雖然所有與安全相關的問題都在圖片中進行了授權,但是當我刷新瀏覽器時,一旦用戶被「驗證」控制似乎被重定向到登錄頁面,即使當我只是登錄,然後單擊刷新....你有這個想法..? – Amit