簡體   English   中英

在發布實例CQ5 / AEM上為Servlet實施身份驗證

[英]Implement Authentication for servlet on publish instance CQ5/AEM

我有一個方案,並且對實施的任何建議都會有很大幫助。 我在publish上創建了一個servlet,它將有來自許多其他第三方應用程序的POST請求。 該Servlet僅將傳入的發布數據存儲在JCR中。 我已經成功創建了該servlet,但是現在的要求是確保該servlet的安全,以便只允許使用特定用戶名和密碼訪問該servlet的應用程序。

我該怎么做?

我會去的方式:

要求那些第三方應用程序向您發送用戶名和密碼,以便您可以在servlet上對其進行驗證,然后決定是允許還是拒絕該請求。

來自Servlet調用(第三方應用程序)

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // ... 

        request.setAttribute("username", "a_valid_user");
        request.setAttribute("password", "a_valid_password");
        request.getRequestDispatcher("yourApp/YourServlet").forward(req, resp);
}

在您的servlet上:

String username = request.getParameter("username");
String password = request.getParameter("password");

if("a_valid_user".equals(username) && "a_valid_password".equals(password) {
     // validate and go further
} else {
     // do not process the request
}

上面的示例是有效的,以防萬一您可以一邊驗證它們。

如果此樣本不能回答您的問題,請提供有關這些第三方應用程序以及您要驗證它們的方式的更多信息。

您可以考慮使用Google客戶庫。 我將其用於AEM發布實例中的用戶身份驗證。 第三方服務器通過身份驗證后,您可以使用單獨的AEM服務帳戶來處理POST處理。

這是我寫的一篇關於將這些庫集成到AEM中的文章。 OSGI中的Google Client API

這樣,您應該可以設置第三方服務帳戶的身份驗證...,如此處https://developers.google.com/identity/protocols/OAuth2ServiceAccount所述

我實際上尚未在AEM中完成服務器到服務器的身份驗證,但應該可以。 但是在一個單獨的項目(非AEM)中,我使用了Google客戶端庫來驗證服務帳戶。

我建議使用兩步過程:

  • 步驟1:驗證並生成令牌,您還可以使用第三方服務生成令牌。
  • 第2步:使用此令牌調用您的Servlet,該Servlet將首先驗證令牌,然后再使用發布數據。

感謝大家的回復。 最后,我在cq中實現了以下代碼進行身份驗證:

  final String authorization = request.getHeader("Authorization"); if (authorization != null && authorization.startsWith("Basic")) { StringTokenizer st = new StringTokenizer(authorization); if (st.hasMoreTokens()) { String basic = st.nextToken(); if (basic.equalsIgnoreCase("Basic")) { String decodedStr = Base64.decode(st.nextToken()); LOGGER.info("Credentials: " + decodedStr); int p = decodedStr.indexOf(":"); if (p != -1) { String login = decodedStr.substring(0, p).trim(); String password = decodedStr.substring(p + 1).trim(); Credentials credentials = new SimpleCredentials(login, password.toCharArray()); adminSession = repository.login(credentials); if (null != adminSession) { // means authenticated and do your stuff here } } } } } 

同樣在調用發布servlet的web服務代碼中,以下是關於我如何在auth標頭中提供憑據的代碼:

  String authStr = usrname+":"+password; // encode data on your side using BASE64 byte[] bytesEncoded = Base64.encodeBase64(authStr.getBytes()); String authEncoded = new String(bytesEncoded); connection.setRequestProperty("Authorization", "Basic "+authEncoded); connection.setDoOutput(true); connection.setRequestMethod("POST"); OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); writer.write("jsondata={sample:jsoncontent}"); writer.close(); 

暫無
暫無

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

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