本文整理匯總了Java中com.sun.net.httpserver.HttpContext.setAuthenticator方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpContext.setAuthenticator方法的具體用法?Java HttpContext.setAuthenticator怎麽用?Java HttpContext.setAuthenticator使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.sun.net.httpserver.HttpContext
的用法示例。
在下文中一共展示了HttpContext.setAuthenticator方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: start
import com.sun.net.httpserver.HttpContext; //導入方法依賴的package包/類
public void start() {
try {
server = HttpServer.create(new InetSocketAddress(8000), 0);
} catch (Exception e) {
Assert.fail("Exception thrown: " + e.getMessage());
}
server.createContext("/unprotected", new TestHttpHandler());
HttpContext protectedContext = server.createContext("/protected", new TestHttpHandler());
protectedContext.setAuthenticator(new BasicAuthenticator("get") {
@Override
public boolean checkCredentials(String user, String pwd) {
return user.equals("admin") && pwd.equals("admin");
}
});
server.setExecutor(null);
server.start();
}
示例2: afterPropertiesSet
import com.sun.net.httpserver.HttpContext; //導入方法依賴的package包/類
@Override
public void afterPropertiesSet() throws IOException {
InetSocketAddress address = (this.hostname != null ?
new InetSocketAddress(this.hostname, this.port) : new InetSocketAddress(this.port));
this.server = HttpServer.create(address, this.backlog);
if (this.executor != null) {
this.server.setExecutor(this.executor);
}
if (this.contexts != null) {
for (String key : this.contexts.keySet()) {
HttpContext httpContext = this.server.createContext(key, this.contexts.get(key));
if (this.filters != null) {
httpContext.getFilters().addAll(this.filters);
}
if (this.authenticator != null) {
httpContext.setAuthenticator(this.authenticator);
}
}
}
if (this.logger.isInfoEnabled()) {
this.logger.info("Starting HttpServer at address " + address);
}
this.server.start();
}
示例3: httpd
import com.sun.net.httpserver.HttpContext; //導入方法依賴的package包/類
/**
* Creates and starts an HTTP or proxy server that requires
* Negotiate authentication.
* @param scheme "Negotiate" or "Kerberos"
* @param principal the krb5 service principal the server runs with
* @return the server
*/
public static HttpServer httpd(String scheme, boolean proxy,
String principal, String ktab) throws Exception {
MyHttpHandler h = new MyHttpHandler();
HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
HttpContext hc = server.createContext("/", h);
hc.setAuthenticator(new MyServerAuthenticator(
proxy, scheme, principal, ktab));
server.start();
return server;
}
示例4: main
import com.sun.net.httpserver.HttpContext; //導入方法依賴的package包/類
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(HTTP_SERVER_PORT), 0);
HttpContext secureContext = server.createContext(DEMO_REST_BASIC_AUTH, new RestDemoHandler());
secureContext.setAuthenticator(new BasicAuthenticator("demo-auth") {
@Override
public boolean checkCredentials(String user, String pwd) {
return user.equals(USERNAME) && pwd.equals(PASSWORD);
}
});
server.createContext(DEMO_REST_NO_AUTH, new RestDemoHandler());
server.setExecutor(null);
System.out.println("[*] Waiting for messages.");
server.start();
}
示例5: RpcHandler
import com.sun.net.httpserver.HttpContext; //導入方法依賴的package包/類
/**
* Create the JSON-RPC request handler
*
* @param rpcPort
* RPC port
* @param rpcAllowIp
* List of allowed host addresses
* @param rpcUser
* RPC user
* @param rpcPassword
* RPC password
*/
public RpcHandler(int rpcPort, List<InetAddress> rpcAllowIp, String rpcUser, String rpcPassword) {
this.rpcPort = rpcPort;
this.rpcAllowIp = rpcAllowIp;
this.rpcUser = rpcUser;
this.rpcPassword = rpcPassword;
//
// Create the HTTP server using a single execution thread
//
try {
server = HttpServer.create(new InetSocketAddress(rpcPort), 10);
HttpContext context = server.createContext("/", this);
context.setAuthenticator(new RpcAuthenticator(LSystem.applicationName));
server.setExecutor(null);
server.start();
BTCLoader.info(String.format("RPC handler started on port %d", rpcPort));
} catch (IOException exc) {
BTCLoader.error("Unable to set up HTTP server", exc);
}
}
示例6: afterPropertiesSet
import com.sun.net.httpserver.HttpContext; //導入方法依賴的package包/類
public void afterPropertiesSet() throws IOException {
InetSocketAddress address = (this.hostname != null ?
new InetSocketAddress(this.hostname, this.port) : new InetSocketAddress(this.port));
this.server = HttpServer.create(address, this.backlog);
if (this.executor != null) {
this.server.setExecutor(this.executor);
}
if (this.contexts != null) {
for (String key : this.contexts.keySet()) {
HttpContext httpContext = this.server.createContext(key, this.contexts.get(key));
if (this.filters != null) {
httpContext.getFilters().addAll(this.filters);
}
if (this.authenticator != null) {
httpContext.setAuthenticator(this.authenticator);
}
}
}
if (this.logger.isInfoEnabled()) {
this.logger.info("Starting HttpServer at address " + address);
}
this.server.start();
}
示例7: afterPropertiesSet
import com.sun.net.httpserver.HttpContext; //導入方法依賴的package包/類
public void afterPropertiesSet() throws IOException {
InetSocketAddress address = this.hostname != null ? new InetSocketAddress(this.hostname, this.port) : new InetSocketAddress(this.port);
this.server = getInitializedServer(address);
if (server == null) {
LOG.warn("Unable to create server");
return;
}
if (this.executor != null) {
this.server.setExecutor(this.executor);
}
if (this.contexts != null) {
for (String key : this.contexts.keySet()) {
HttpContext httpContext = this.server.createContext(key, this.contexts.get(key));
if (this.filters != null) {
httpContext.getFilters().addAll(this.filters);
}
if (this.authenticator != null)
httpContext.setAuthenticator(this.authenticator);
}
}
if (LOG.isInfoEnabled()) {
LOG.info("Starting HttpServer at address " + address);
}
this.server.start();
}
示例8: createContext
import com.sun.net.httpserver.HttpContext; //導入方法依賴的package包/類
private void createContext(HttpServer server, String path, HttpHandler handler, Authenticator auth)
{
final HttpContext context = server.createContext(path, handler);
if( auth != null )
{
context.setAuthenticator(auth);
}
context.getFilters().addAll(filters);
}
示例9: buildHttpContext
import com.sun.net.httpserver.HttpContext; //導入方法依賴的package包/類
/**
* Build the HttpContext for the given endpoint.
* @param endpoint the JAX-WS Provider Endpoint object
* @param serviceName the given service name
* @return the fully populated HttpContext
*/
protected HttpContext buildHttpContext(Endpoint endpoint, String serviceName) {
String fullPath = calculateEndpointPath(endpoint, serviceName);
HttpContext httpContext = this.server.createContext(fullPath);
if (this.filters != null) {
httpContext.getFilters().addAll(this.filters);
}
if (this.authenticator != null) {
httpContext.setAuthenticator(this.authenticator);
}
return httpContext;
}
示例10: main
import com.sun.net.httpserver.HttpContext; //導入方法依賴的package包/類
public static void main (String[] args) throws Exception {
Handler handler = new Handler();
InetSocketAddress addr = new InetSocketAddress (0);
HttpServer server = HttpServer.create(addr, 0);
HttpContext ctx = server.createContext("/test", handler);
BasicAuthenticator a = new BasicAuthenticator("[email protected]") {
@Override
public boolean checkCredentials (String username, String pw) {
return "fred".equals(username) && pw.charAt(0) == 'x';
}
};
ctx.setAuthenticator(a);
ExecutorService executor = Executors.newCachedThreadPool();
server.setExecutor(executor);
server.start ();
java.net.Authenticator.setDefault(new MyAuthenticator());
System.out.print("Deadlock: " );
for (int i=0; i<2; i++) {
Runner t = new Runner(server, i);
t.start();
t.join();
}
server.stop(2);
executor.shutdown();
if (error) {
throw new RuntimeException("test failed error");
}
if (count != 2) {
throw new RuntimeException("test failed count = " + count);
}
System.out.println("OK");
}
示例11: main
import com.sun.net.httpserver.HttpContext; //導入方法依賴的package包/類
public static void main (String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
try {
Handler handler = new Handler();
HttpContext ctx = server.createContext("/test", handler);
BasicAuthenticator a = new BasicAuthenticator(REALM) {
public boolean checkCredentials (String username, String pw) {
return USERNAME.equals(username) && PASSWORD.equals(pw);
}
};
ctx.setAuthenticator(a);
server.start();
Authenticator.setDefault(new MyAuthenticator());
URL url = new URL("http://localhost:"+server.getAddress().getPort()+"/test/");
HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
InputStream is = urlc.getInputStream();
int c = 0;
while (is.read()!= -1) { c ++; }
if (c != 0) { throw new RuntimeException("Test failed c = " + c); }
if (error) { throw new RuntimeException("Test failed: error"); }
System.out.println ("OK");
} finally {
server.stop(0);
}
}
示例12: main
import com.sun.net.httpserver.HttpContext; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(0), 10);
ExecutorService e = Executors.newCachedThreadPool();
Handler h = new Handler();
HttpContext serverContext = server.createContext("/test", h);
int port = server.getAddress().getPort();
System.out.println("Server port = " + port);
ClientAuth ca = new ClientAuth();
ServerAuth sa = new ServerAuth("foo realm");
serverContext.setAuthenticator(sa);
server.setExecutor(e);
server.start();
HttpClient client = HttpClient.newBuilder()
.authenticator(ca)
.build();
try {
URI uri = new URI("http://127.0.0.1:" + Integer.toString(port) + "/test/foo");
HttpRequest req = HttpRequest.newBuilder(uri).GET().build();
HttpResponse resp = client.send(req, asString());
ok = resp.statusCode() == 200 && resp.body().equals(RESPONSE);
if (!ok || ca.count != 1)
throw new RuntimeException("Test failed");
// repeat same request, should succeed but no additional authenticator calls
resp = client.send(req, asString());
ok = resp.statusCode() == 200 && resp.body().equals(RESPONSE);
if (!ok || ca.count != 1)
throw new RuntimeException("Test failed");
// try a POST
req = HttpRequest.newBuilder(uri)
.POST(fromString(POST_BODY))
.build();
resp = client.send(req, asString());
ok = resp.statusCode() == 200;
if (!ok || ca.count != 1)
throw new RuntimeException("Test failed");
} finally {
server.stop(0);
e.shutdownNow();
}
System.out.println("OK");
}
示例13: createServer
import com.sun.net.httpserver.HttpContext; //導入方法依賴的package包/類
static HttpServer createServer(ExecutorService e, BasicAuthenticator sa) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(0), 10);
Handler h = new Handler();
HttpContext serverContext = server.createContext("/test", h);
serverContext.setAuthenticator(sa);
server.setExecutor(e);
server.start();
return server;
}
示例14: setContextAuthenticator
import com.sun.net.httpserver.HttpContext; //導入方法依賴的package包/類
static void setContextAuthenticator(HttpContext ctxt,
HttpTestAuthenticator auth) {
final String realm = auth.getRealm();
com.sun.net.httpserver.Authenticator authenticator =
new BasicAuthenticator(realm) {
@Override
public boolean checkCredentials(String username, String pwd) {
return auth.getUserName().equals(username)
&& new String(auth.getPassword(username)).equals(pwd);
}
};
ctxt.setAuthenticator(authenticator);
}
示例15: main
import com.sun.net.httpserver.HttpContext; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/info", new InfoHandler());
HttpContext hc1 = server.createContext("/get", new GetHandler());
hc1.setAuthenticator(new DigestAuthenticator("get") {
@Override
public String gethAuthToken(String username) {
return this.calculateAuthToken("admin", "password");
}
});
server.setExecutor(null); // creates a default executor
server.start();
System.out.println("The server is running");
}