Java Code Examples for io.vertx.ext.web.Session#get()

The following examples show how to use io.vertx.ext.web.Session#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AdminLoginHandler.java    From joyqueue with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(final RoutingContext context) {
    HttpServerRequest request = context.request();
    Session session = context.session();
    if (session == null) {
        context.fail(new HttpStatusException(HTTP_INTERNAL_ERROR, "No session - did you forget to include a SessionHandler?"));
        return;
    }
    String remoteIP = getRemoteIP(request);
    context.put(REMOTE_IP, remoteIP);
    User user = session.get(userSessionKey);
    if (user == null) {
        user = userService.findByCode(DEFAULT_LOGIN_USER_CODE);
    }
    //存放用户上下文信息
    context.put(USER_KEY, user);
    context.next();
}
 
Example 2
Source File: CSRFHandlerImpl.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
private String getTokenFromSession(RoutingContext ctx) {
  Session session = ctx.session();
  if (session == null) {
    return null;
  }
  // get the token from the session
  String sessionToken = session.get(headerName);
  if (sessionToken != null) {
    // attempt to parse the value
    int idx = sessionToken.indexOf('/');
    if (idx != -1 && session.id() != null && session.id().equals(sessionToken.substring(0, idx))) {
      return sessionToken.substring(idx + 1);
    }
  }
  // fail
  return null;
}
 
Example 3
Source File: ClusteredSessionHandlerTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
private void checkSession(Session session) {
  assertEquals(123456L, (long) session.get("somelong"));
  assertEquals(1234, (int) session.get("someint"));
  assertEquals((short) 123, (short) session.get("someshort"));
  assertEquals((byte) 12, (byte) session.get("somebyte"));
  assertEquals(123.456d, (double) session.get("somedouble"), 0);
  assertEquals(123.456f, (float) session.get("somefloat"), 0);
  assertEquals('X', (char) session.get("somechar"));
  assertTrue(session.get("somebooleantrue"));
  assertFalse(session.get("somebooleanfalse"));
  assertEquals("wibble", session.get("somestring"));
  assertTrue(TestUtils.byteArraysEqual(bytes, session.get("somebytes")));
  assertEquals(buffer, session.get("somebuffer"));
  JsonObject json = session.get("someclusterserializable");
  assertNotNull(json);
  assertEquals("bar", json.getString("foo"));
}
 
Example 4
Source File: VxApiAuthSessionTokenImpl.java    From VX-API-Gateway with MIT License 5 votes vote down vote up
@Override
public void handle(RoutingContext event) {
	Session session = event.session();
	if (session == null) {
		if (!event.response().ended()) {
			event.response().putHeader(HttpHeaderConstant.SERVER, VxApiGatewayAttribute.FULL_NAME)
					.putHeader(HttpHeaderConstant.CONTENT_TYPE, authFailContentType.val()).end(authFailResult);
		}
	} else {
		// session中的token
		String apiToken = session.get(apiTokenName) == null ? null : session.get(apiTokenName).toString();
		// 用户request中的token
		String userTokoen = null;
		if (userTokenScope == ParamPositionEnum.HEADER) {
			userTokoen = event.request().getHeader(userTokenName);
		} else {
			userTokoen = event.request().getParam(userTokenName);
		}
		// 检验请求是否正确如果正确放行反则不通过
		if (!StrUtil.isNullOrEmpty(apiToken) && apiToken.equals(userTokoen)) {
			event.next();
		} else {
			if (!event.response().ended()) {
				event.response().putHeader(HttpHeaderConstant.SERVER, VxApiGatewayAttribute.FULL_NAME)
						.putHeader(HttpHeaderConstant.CONTENT_TYPE, authFailContentType.val()).end(authFailResult);
			}
		}
	}
}