本文整理汇总了Java中io.vertx.ext.auth.User类的典型用法代码示例。如果您正苦于以下问题:Java User类的具体用法?Java User怎么用?Java User使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
User类属于io.vertx.ext.auth包,在下文中一共展示了User类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: authenticate
import io.vertx.ext.auth.User; //导入依赖的package包/类
@Override
public void authenticate(JsonObject credentials, Handler<AsyncResult<User>> resultHandler) {
String token = credentials.getString("token");
httpClient.getAbs("https://graph.facebook.com:443/v2.4/me?access_token=" + token)
.handler(response ->
response.bodyHandler(buffer -> {
JsonObject json = new JsonObject(buffer.toString());
if (response.statusCode() != 200) {
String message = json.getJsonObject("error").getString("message");
resultHandler.handle(Future.failedFuture(message));
} else {
resultHandler.handle(Future.succeededFuture(
new MyUser(Long.parseLong(json.getString("id")),
json.getString("name"),
token)));
}
}))
.exceptionHandler(error ->
resultHandler.handle(Future.failedFuture(error.getMessage())))
.end();
}
示例2: getContextHandler
import io.vertx.ext.auth.User; //导入依赖的package包/类
private static Handler<RoutingContext> getContextHandler(ContextProvider instance) {
return context -> {
if (instance != null) {
Object provided = instance.provide(context.request());
if (provided instanceof User) {
context.setUser((User) provided);
}
if (provided instanceof Session) {
context.setSession((Session) provided);
}
if (provided != null) {
context.data().put(ContextProviderFactory.getContextKey(provided), provided);
}
}
context.next();
};
}
示例3: in
import io.vertx.ext.auth.User; //导入依赖的package包/类
public static void in(final IpcData data, final Envelop envelop) {
if (null != envelop) {
// User
final User user = envelop.user();
final JsonObject sendData = new JsonObject();
if (null != user) {
sendData.put("user", user.principal());
}
// Header
final MultiMap headers = envelop.headers();
if (null != headers) {
final JsonObject headerData = new JsonObject();
headers.forEach((entry) -> headerData.put(entry.getKey(), entry.getValue()));
sendData.put("header", headerData);
}
// Data
final Object content = envelop.data();
sendData.put("data", content);
sendData.put("config", data.getConfig());
// Data Prepared finished.
sendData.put("address", data.getAddress());
data.setData(sendData.toBuffer());
}
}
示例4: authorise
import io.vertx.ext.auth.User; //导入依赖的package包/类
@Override
protected void authorise(User user, RoutingContext context) {
// System.out.println("authorise =>"+user.principal().toString());
((GateUser)user).isAuthorised(authMgr.authProvider,context.request().uri(), res -> {
if (res.succeeded()) {
if (res.result()) {
context.next();
} else {
context.fail(HttpStatus.Unauthorized);
}
} else {
context.fail(res.cause());
}
});
}
示例5: fail
import io.vertx.ext.auth.User; //导入依赖的package包/类
private void fail(Future<JsonObject> completeFuture, AsyncResult<User> ar) {
String errorMessage = ar.cause().getMessage();
if (errorMessage != null) {
if (errorMessage.startsWith("Expired JWT")) {
completeFuture.fail(SystemException.wrap(DefaultErrorCode.EXPIRE_TOKEN, ar.cause())
.set("details", errorMessage));
} else if (errorMessage.startsWith("Invalid JWT")) {
completeFuture.fail(SystemException.wrap(DefaultErrorCode.INVALID_TOKEN, ar.cause())
.set("details", errorMessage));
} else {
completeFuture.fail(SystemException.wrap(DefaultErrorCode.PERMISSION_DENIED, ar.cause())
.set("details", ar.cause().getMessage()));
}
} else {
completeFuture.fail(SystemException.wrap(DefaultErrorCode.PERMISSION_DENIED, ar.cause())
.set("details", ar.cause().getMessage()));
}
}
示例6: extractAuthUserFromMessage
import io.vertx.ext.auth.User; //导入依赖的package包/类
public static User extractAuthUserFromMessage(Message<?> message) {
User user = null;
String serializedUser = message.headers().get(SwaggerRouter.AUTH_USER_HEADER_KEY);
if (serializedUser != null && !serializedUser.isEmpty()) {
Buffer buffer = Buffer.buffer(serializedUser);
UserHolder userHolder = new UserHolder();
userHolder.readFromBuffer(0, buffer);
user = userHolder.user;
if (user != null) {
String authProviderName = message.headers().get(SwaggerRouter.AUTH_PROVIDER_NAME_HEADER_KEY);
if (authProviderName != null) {
user.setAuthProvider(getAuthProviderFactory().getAuthProviderByName(authProviderName));
}
}
}
return user;
}
示例7: shouldDelegateIsUserInRole
import io.vertx.ext.auth.User; //导入依赖的package包/类
@Test
public void shouldDelegateIsUserInRole() {
User user = mock(User.class);
doAnswer(invocation -> {
String role = invocation.getArgumentAt(0, String.class);
Handler<AsyncResult<Boolean>> handler = invocation.getArgumentAt(1, Handler.class);
handler.handle(Future.succeededFuture("USER".equals(role)));
return user;
}).when(user).isAuthorised(isA(String.class), isA(Handler.class));
when(user.principal())
.thenReturn(new JsonObject().put("username", "marco"))
.thenReturn(new JsonObject());
when(routingContext.user()).thenReturn(null).thenReturn(user);
assertThat(vaadinRequest.isUserInRole("USER")).isFalse();
assertThat(vaadinRequest.isUserInRole("ADMIN")).isFalse();
assertThat(vaadinRequest.isUserInRole("USER")).isTrue();
//assertThat(vaadinRequest.getUserPrincipal().getName()).isNull();
}
示例8: testBasicAuthFailsWrongCredentials
import io.vertx.ext.auth.User; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Test
public final void testBasicAuthFailsWrongCredentials(final TestContext context) {
final Async async = context.async();
final String encodedUserPass = Base64.getEncoder()
.encodeToString("[email protected]_TENANT:password123".getBytes(StandardCharsets.UTF_8));
doAnswer(invocation -> {
Handler<AsyncResult<User>> resultHandler = invocation.getArgumentAt(1, Handler.class);
resultHandler.handle(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_UNAUTHORIZED, "bad credentials")));
return null;
}).when(credentialsAuthProvider).authenticate(any(JsonObject.class), any(Handler.class));
vertx.createHttpClient().put(httpAdapter.getInsecurePort(), HOST, "/somenonexistingroute")
.putHeader("content-type", HttpUtils.CONTENT_TYPE_JSON)
.putHeader(AUTHORIZATION_HEADER, "Basic " + encodedUserPass).handler(response -> {
context.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, response.statusCode());
response.bodyHandler(totalBuffer -> {
async.complete();
});
}).exceptionHandler(context::fail).end();
}
示例9: testBasicAuthSuccess
import io.vertx.ext.auth.User; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Test
public final void testBasicAuthSuccess(final TestContext context) throws Exception {
final Async async = context.async();
final String encodedUserPass = Base64.getEncoder()
.encodeToString("[email protected]_TENANT:password123".getBytes(StandardCharsets.UTF_8));
doAnswer(invocation -> {
Handler<AsyncResult<User>> resultHandler = invocation.getArgumentAt(1, Handler.class);
resultHandler.handle(Future.succeededFuture(new Device("DEFAULT_TENANT", "device_1")));
return null;
}).when(credentialsAuthProvider).authenticate(any(JsonObject.class), any(Handler.class));
vertx.createHttpClient().get(httpAdapter.getInsecurePort(), HOST, "/somenonexistingroute")
.putHeader("content-type", HttpUtils.CONTENT_TYPE_JSON)
.putHeader(AUTHORIZATION_HEADER, "Basic " + encodedUserPass).handler(response -> {
context.assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response.statusCode());
response.bodyHandler(totalBuffer -> {
async.complete();
});
}).exceptionHandler(context::fail).end();
}
示例10: authenticate
import io.vertx.ext.auth.User; //导入依赖的package包/类
@Override
public final void authenticate(final JsonObject authInfo, final Handler<AsyncResult<User>> resultHandler) {
DeviceCredentials credentials = getCredentials(Objects.requireNonNull(authInfo));
if (credentials == null) {
resultHandler.handle(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_UNAUTHORIZED, "malformed credentials")));
} else {
authenticate(credentials, s -> {
if (s.succeeded()) {
resultHandler.handle(Future.succeededFuture(s.result()));
} else {
resultHandler.handle(Future.failedFuture(s.cause()));
}
});
}
}
示例11: generateToken
import io.vertx.ext.auth.User; //导入依赖的package包/类
/**
* Authenticates the user and returns a JWToken if successful.
*
* @param username
* Username
* @param password
* Password
* @param resultHandler
* Handler to be invoked with the created JWToken
*/
public void generateToken(String username, String password, Handler<AsyncResult<String>> resultHandler) {
authenticate(username, password, rh -> {
if (rh.failed()) {
resultHandler.handle(Future.failedFuture(rh.cause()));
} else {
User user = rh.result().getUser();
String uuid = null;
if (user instanceof MeshAuthUser) {
uuid = ((MeshAuthUser) user).getUuid();
} else {
uuid = user.principal().getString("uuid");
}
JsonObject tokenData = new JsonObject().put(USERID_FIELD_NAME, uuid);
resultHandler.handle(Future.succeededFuture(jwtProvider.generateToken(tokenData,
new JWTOptions().setExpiresInSeconds(Mesh.mesh().getOptions().getAuthenticationOptions().getTokenExpirationTime()))));
}
});
}
示例12: authenticate
import io.vertx.ext.auth.User; //导入依赖的package包/类
@Override
public void authenticate(JsonObject authInfo, Handler<AsyncResult<User>> resultHandler) {
String password = environment.getProperty(USERS_PREFIX_KEY + authInfo.getString("username"));
if (password != null) {
// Get password from incoming HTTP request
String presentedPassword = authInfo.getString("password");
if (password.equals(presentedPassword)) {
resultHandler.handle(
Future.succeededFuture(new io.gravitee.management.services.http.auth.User()));
return ;
}
}
resultHandler.handle(Future.failedFuture("Unauthorized user"));
}
示例13: authenticate
import io.vertx.ext.auth.User; //导入依赖的package包/类
@Override
public void authenticate(JsonObject authInfo, Handler<AsyncResult<User>> resultHandler) {
String password = environment.getProperty(USERS_PREFIX_KEY + authInfo.getString("username"));
if (password != null) {
// Get password from incoming HTTP request
String presentedPassword = authInfo.getString("password");
if (password.equals(presentedPassword)) {
resultHandler.handle(
Future.succeededFuture(new io.gravitee.gateway.services.http.auth.User()));
return ;
}
}
resultHandler.handle(Future.failedFuture("Unauthorized user"));
}
示例14: handle
import io.vertx.ext.auth.User; //导入依赖的package包/类
@Override
public void handle(RoutingContext context) {
User user = context.user();
if (user != null) {
authorise(user, context);
return;
}
String apiToken;
try {
apiToken = parseApiToken(context.request());
} catch (BadRequestException bre) {
context.fail(bre);
return;
}
if (apiToken == null) {
context.fail(401);
return;
}
doAuth(context, apiToken);
}
示例15: writeToBuffer
import io.vertx.ext.auth.User; //导入依赖的package包/类
@Override
public void writeToBuffer(Buffer buffer) {
// try to get the user from the context otherwise fall back to any cached version
User user = context != null ? context.user() : this.user;
if (user != null && user instanceof ClusterSerializable) {
buffer.appendByte((byte)1);
String className = user.getClass().getCanonicalName();
if (className == null) {
throw new IllegalStateException("Cannot serialize " + user.getClass().getName());
}
byte[] bytes = className.getBytes(StandardCharsets.UTF_8);
buffer.appendInt(bytes.length);
buffer.appendBytes(bytes);
ClusterSerializable cs = (ClusterSerializable)user;
cs.writeToBuffer(buffer);
} else {
buffer.appendByte((byte)0);
}
}