回答
如果你想它很簡單,你可以嘗試JDK 1.6 com.sun.net.httpserver.HttpServer
,它也有基本身份驗證mechanizm:
HttpServer server = HttpServer.create(new InetSocketAddress(8888), 0);
HttpContext cc = server.createContext("/test", new MyHandler());
cc.setAuthenticator(new BasicAuthenticator("test") {
@Override
public boolean checkCredentials(String user, String pwd) {
return user.equals("test") && pwd.equals("test");
}
});
server.setExecutor(null); // creates a default executor
server.start();
如果你希望它是簡單的,比如Apache,然後嘗試碼頭http://www.eclipse.org/jetty/它是一個真正的Web服務器它易於使用,並且可以嵌入到Java獨立應用程序中。
的技術,那你可能需要的,被稱爲Spring 3 security
。它經常被用於java EE
應用程序的框架。嘗試谷歌它。我相信你會發現很多有用的信息和教程。
由於Java 1.6
,有一個內置HTTP server
。 這是一個Example
package com.example;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class Test {
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/test", new MyHandler());
server.setExecutor(null); // creates a default executor
server.start();
}
static class MyHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
String response = "This is the response";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}
執行它,去http://localhost:8000/test
,你會看到響應
非常感謝您的快速回復,但我如何添加到您的代碼驗證?我必須使用下面提到的Spring 3安全性嗎? – user1554427 2013-05-03 09:12:48
沒有必要,那是你的邏輯! – 2013-05-03 09:16:31
你可以看看NanoHTTPPD的源代碼。這是一個用Java編寫的非常簡單的Web服務器。
如果您想在glassfish服務器中創建JEE6應用程序,您可以嘗試Realm。 看看http://docs.oracle.com/javaee/6/tutorial/doc/bnbxj.html
- 1. Python網絡/服務器身份驗證
- 2. 網絡代理服務器中的XACML身份驗證
- 3. 樹脂網絡服務器上的用戶身份驗證
- 4. WSO2身份服務器身份驗證
- 5. 身份驗證服務器
- 6. 在NodeJS的TCP服務器上實現簡單身份驗證
- 7. 基本身份驗證簡單HTTP服務器
- 8. box.com api多線程網絡服務的OAuth身份驗證
- 9. 網絡服務身份驗證的IIS 6錯誤
- 10. Liferay的JSON網絡服務身份驗證
- 11. Windows身份驗證和網絡服務帳戶作爲的db_owner
- 12. 使用Windows身份驗證的簡單身份驗證代理
- 13. 驗證服務器的身份
- 14. 跨服務器的IIS身份驗證
- 15. 服務器的Google API身份驗證
- 16. Google Apps的身份驗證服務器
- 17. 簡單REST身份驗證
- 18. 簡單身份驗證
- 19. HTTP簡單身份驗證與POST表單身份驗證
- 20. 對簡單的PHP RESTful API服務進行身份驗證
- 21. Novell網絡上的Windows身份驗證
- 22. asmx網絡服務:客戶端身份驗證
- 23. 獨立身份驗證wcf網絡服務
- 24. 從Kerberos身份驗證機器到NTLM服務器的驗證
- 25. DDD身份驗證服務
- 26. 身份驗證服務
- 27. Web服務身份驗證
- 28. Angular2身份驗證服務
- 29. 身份驗證和服務
- 30. WCF身份驗證服務
*「源代碼或任何有用的教程將非常有幫助。」*顯示工作量並詢問(特定)問題也會有所幫助。就目前來看,這不是一個真正的問題。 – 2013-05-03 09:05:51