簡體   English   中英

java中的Restful Web應用程序(安全性)

[英]Restful Web application (security) in java

我們正在開發兩個Web應用程序應用程序A:在glassfish服務器上運行的Restful Web服務。 應用程序B:在Tomcat服務器上運行的Dynamics Web應用程序。

我只通過我的應用程序B訪問應用程序A,我不希望任何其他應用程序訪問我的應用程序A. 為此,我計划在相應的服務器中安裝客戶端 - 服務器證書,以便我的應用程序A只能由我的應用程序B訪問,我想阻止其他應用程序訪問我的應用程序A.你能告訴我如何在相應的服務器中安裝客戶端 - 服務器證書????

如果有人有更好的替代方案來獲得這個,請解釋我。

如果可能,請用示例解釋。

謝謝

您在尋找身份驗證和授權嗎? 如果是,你可以使用彈簧安全。 如果您只查找具有某些用戶標識和密碼的身份驗證(根級別訪問),請使用基本身份驗證。 它將檢查用戶是否有效並且可以訪問網址。

代碼和更多信息檢查網址。

如何實現基本身份驗證?

如何使用基本身份驗證來使用RESTful Web服務?

當您使用Java時,我認為您正在使用Spring Framework,如果是這樣,您可以使用Spring Session在應用程序之間共享身份驗證

  1. 我有一些使用純java進行簡單和基本身份驗證的示例代碼。 它可能適合您的需要。
  2. 使用郵遞員設置和取消設置基本身份驗證以對其進行測試。
  3. 好處:簡單。 除了javax.ws.rs之外不需要任何其他東西。*
  4. 只有當你對應用程序B(這和我的項目是一樣的)100%控制時,這是一個內部應用程序(不是公共訪問的網頁)

碼:

//  to inject request 
@Context
private HttpServletRequest request; 

@GET
@Path("/testAuth")
@Produces(MediaType.APPLICATION_JSON)
public Response testAuth() {
    // TODO 
    // this is only a template for doing authentication in the near future
    String returnString = "";

    //check if authenticated
    String authorization = request.getHeader("Authorization");
    if (authorization == null || authorization.toUpperCase().startsWith("BASIC ") == false) {
        //no authenticated
        returnString =  "{\"testAuth\", \"need authentication\"}"; 
        return Response.status(401).entity(returnString).build();
    } else{

        String credentials = authorization.substring("Basic".length()).trim();
        byte[] decoded = DatatypeConverter.parseBase64Binary(credentials);
        String decodedString = new String(decoded);
        String[] actualCredentials = decodedString.split(":");
        String ID = actualCredentials[0];
        String Password = actualCredentials[1];
        String Result = userAuthenticate(ID, Password);

        returnString =  "{\"testAuth\", \"" + 
            " (" + Result + ") \"}";
        return Response.status(200).entity(returnString).build();
    }   

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM