日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

idea中使用osgi_OSGi环境中的Servlet基本身份验证

發布時間:2023/12/3 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 idea中使用osgi_OSGi环境中的Servlet基本身份验证 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

idea中使用osgi

您首先需要獲得對OSGI HTTP Service的引用。 您可以通過聲明性服務來做到這一點。 這篇文章將集中在獲得對HTTP服務的引用之后的步驟。 注意:此職位的完整課程位于此處
通過OSGI HTTP Service注冊Servlet時,它為您提供了提供HTTPContext實現的選項。
httpService.registerServlet(alias, new MyServlet(), initParams, null);

當我們實現HTTPContext接口時,我們可以實現三種方法。 在為ermmm請求之前,將調用這三(3)個handleSecurity中的一部分……檢查安全性。

public class BasicAuthSecuredContext implements HttpContext{@Overridepublic boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException {return false;}@Overridepublic URL getResource(String s) {return null; }@Overridepublic String getMimeType(String s) {return null;} }

因此,在實現此功能時,我從OSGI HTTPContext文檔和HTTP Authentication spec中借用了很多內容。 如果您有興趣學習很多東西,深入研究細節等內容,則必須閱讀它們。或者您可以閱讀本文的其余部分。

首先,除非使用https,否則基本認證非常重要。 如果不存在,我們會讓用戶知道它是禁區。 讓我們繼續做。

if (!request.getScheme().equals("https")) {response.sendError(HttpServletResponse.SC_FORBIDDEN);return false; }

接下來,讓我們檢查Authorization標頭。 如果那不在那里,我們會讓他們知道,他們需要那種東西才能在那里。 或者我們只是說他們是未經授權的。 現在開始吧。

if (request.getHeader("Authorization") == null) {response.sendError(HttpServletResponse.SC_UNAUTHORIZED);return false; }

好的,兩項測試通過了。 現在,我們做一些實際的工作。 讓我們提取標頭以對其進行解碼,然后執行“不太正確”的身份驗證。

protected boolean authenticated(HttpServletRequest request) {String authzHeader = request.getHeader("Authorization");String usernameAndPassword = new String(Base64.decodeBase64(authzHeader.substring(6).getBytes()));int userNameIndex = usernameAndPassword.indexOf(":");String username = usernameAndPassword.substring(0, userNameIndex);String password = usernameAndPassword.substring(userNameIndex + 1);// Now, do the authentication against in the way you want, ex: ldap, db stored uname/pw// Here I will do lame hard coded credential check. HIGHLY NOT RECOMMENDED! return ((username.equals("username") && password.equals("password"));}

讓我們將此方法集成到handleSecurity方法中。 請注意,當安全性失敗時,如何將有意義的錯誤消息設置為響應(第14行)。 這樣可以防止用戶猜測,并且他們知道出了什么問題。 Ermm,至少,如果他們知道HTTP錯誤代碼,他們將確切知道出了什么問題。

@Overridepublic boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException {if (!request.getScheme().equals("https")) {response.sendError(HttpServletResponse.SC_FORBIDDEN);return false;}if (request.getHeader("Authorization") == null) {response.sendError(HttpServletResponse.SC_UNAUTHORIZED);return false;}if (authenticated(request)) {return true;} else {response.sendError(HttpServletResponse.SC_UNAUTHORIZED);return false;}}

而已。 現在,在注冊servlet時傳遞該對象,

httpService.registerServlet(alias, new MyServlet(), initParams, new BasicAuthSecuredContext());

…并看到OSGI Servlet中基本身份驗證的強大功能!

參考: 在我們的JCG合作伙伴 Mackie Mathew的dev_religion博客上, 在OSGI環境中為Servlet實現Servlet的基本身份驗證 。


翻譯自: https://www.javacodegeeks.com/2012/06/servlet-basic-auth-in-osgi-environment.html

idea中使用osgi

總結

以上是生活随笔為你收集整理的idea中使用osgi_OSGi环境中的Servlet基本身份验证的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。