=doc #18968 Document auth options for Java DSL
This commit is contained in:
parent
696cfed51f
commit
af2bc368a2
6 changed files with 247 additions and 1 deletions
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
package docs.http.javadsl.server;
|
||||
|
||||
import akka.http.javadsl.model.HttpRequest;
|
||||
import akka.http.javadsl.model.headers.Host;
|
||||
import akka.http.javadsl.server.Handler1;
|
||||
import akka.http.javadsl.server.RequestContext;
|
||||
import akka.http.javadsl.server.Route;
|
||||
import akka.http.javadsl.server.RouteResult;
|
||||
import akka.http.javadsl.server.values.BasicCredentials;
|
||||
import akka.http.javadsl.server.values.HttpBasicAuthenticator;
|
||||
import akka.http.javadsl.testkit.JUnitRouteTest;
|
||||
import akka.http.scaladsl.model.headers.Authorization;
|
||||
import org.junit.Test;
|
||||
import scala.Option;
|
||||
import scala.concurrent.Future;
|
||||
|
||||
public class HttpBasicAuthenticatorExample extends JUnitRouteTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void testBasicAuthenticator() {
|
||||
//#basic-authenticator-java
|
||||
final HttpBasicAuthenticator<String> authentication = new HttpBasicAuthenticator<String>("My realm") {
|
||||
|
||||
private final String hardcodedPassword = "correcthorsebatterystaple";
|
||||
|
||||
public Future<Option<String>> authenticate(BasicCredentials credentials) {
|
||||
// this is where your actual authentication logic would go
|
||||
if (credentials.available() && // no anonymous access
|
||||
credentials.verify(hardcodedPassword)) {
|
||||
return authenticateAs(credentials.identifier());
|
||||
} else {
|
||||
return refuseAccess();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
final Route route =
|
||||
authentication.route(
|
||||
handleWith1(
|
||||
authentication,
|
||||
new Handler1<String>() {
|
||||
public RouteResult apply(RequestContext ctx, String user) {
|
||||
return ctx.complete("Hello " + user + "!");
|
||||
}
|
||||
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
// tests:
|
||||
final HttpRequest okRequest =
|
||||
HttpRequest
|
||||
.GET("http://akka.io/")
|
||||
.addHeader(Host.create("akka.io"))
|
||||
.addHeader(Authorization.basic("randal", "correcthorsebatterystaple"));
|
||||
testRoute(route).run(okRequest).assertEntity("Hello randal!");
|
||||
|
||||
final HttpRequest badRequest =
|
||||
HttpRequest
|
||||
.GET("http://akka.io/")
|
||||
.addHeader(Host.create("akka.io"))
|
||||
.addHeader(Authorization.basic("randal", "123abc"));
|
||||
testRoute(route).run(badRequest).assertStatusCode(401);
|
||||
|
||||
//#basic-authenticator-java
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
|
||||
*/
|
||||
package docs.http.javadsl.server;
|
||||
|
||||
import akka.http.javadsl.model.HttpRequest;
|
||||
import akka.http.javadsl.model.headers.Host;
|
||||
import akka.http.javadsl.model.headers.OAuth2BearerToken;
|
||||
import akka.http.javadsl.server.Handler1;
|
||||
import akka.http.javadsl.server.RequestContext;
|
||||
import akka.http.javadsl.server.Route;
|
||||
import akka.http.javadsl.server.RouteResult;
|
||||
import akka.http.javadsl.server.values.BasicCredentials;
|
||||
import akka.http.javadsl.server.values.HttpBasicAuthenticator;
|
||||
import akka.http.javadsl.server.values.OAuth2Authenticator;
|
||||
import akka.http.javadsl.server.values.OAuth2Credentials;
|
||||
import akka.http.javadsl.testkit.JUnitRouteTest;
|
||||
import akka.http.scaladsl.model.headers.Authorization;
|
||||
import org.junit.Test;
|
||||
import scala.Option;
|
||||
import scala.concurrent.Future;
|
||||
|
||||
public class OAuth2AuthenticatorExample extends JUnitRouteTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void testOAuth2Authenticator() {
|
||||
//#oauth2-authenticator-java
|
||||
final OAuth2Authenticator<String> authentication = new OAuth2Authenticator<String>("My realm") {
|
||||
|
||||
private final String hardcodedToken = "token";
|
||||
|
||||
@Override
|
||||
public Future<Option<String>> authenticate(OAuth2Credentials credentials) {
|
||||
// this is where your actual authentication logic would go, looking up the user
|
||||
// based on the token or something in that direction
|
||||
if (credentials.available() && // no anonymous access
|
||||
credentials.verify(hardcodedToken)) {
|
||||
// not a secret + identity pair, so this is actually the token
|
||||
return authenticateAs(credentials.identifier());
|
||||
} else {
|
||||
return refuseAccess();
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
final Route route =
|
||||
authentication.route(
|
||||
handleWith1(
|
||||
authentication,
|
||||
new Handler1<String>() {
|
||||
public RouteResult apply(RequestContext ctx, String token) {
|
||||
return ctx.complete("The secret token is: " + token);
|
||||
}
|
||||
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
// tests:
|
||||
final HttpRequest okRequest =
|
||||
HttpRequest
|
||||
.GET("http://akka.io/")
|
||||
.addHeader(Host.create("akka.io"))
|
||||
.addHeader(Authorization.oauth2("token"));
|
||||
testRoute(route).run(okRequest).assertEntity("The secret token is: token");
|
||||
|
||||
final HttpRequest badRequest =
|
||||
HttpRequest
|
||||
.GET("http://akka.io/")
|
||||
.addHeader(Host.create("akka.io"))
|
||||
.addHeader(Authorization.oauth2("wrong"));
|
||||
testRoute(route).run(badRequest).assertStatusCode(401);
|
||||
|
||||
//#oauth2-authenticator-java
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue