io.vertx.ext.web.handler.CookieHandler Java Examples

The following examples show how to use io.vertx.ext.web.handler.CookieHandler. 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: EdgeDispatcher.java    From scaffold with Apache License 2.0 5 votes vote down vote up
public void init(Router router) {
  ///捕获 {ServiceComb微服务Name}/{服务路径&参数} 的URL
  String regex = "/([^\\\\/]+)/(.*)";
  router.routeWithRegex(regex).handler(CookieHandler.create());
  router.routeWithRegex(regex).handler(createBodyHandler());
  router.routeWithRegex(regex).failureHandler(this::onFailure).handler(this::onRequest);
}
 
Example #2
Source File: VertxNubes.java    From nubes with Apache License 2.0 5 votes vote down vote up
/**
 * @param vertx the vertx instance
 */
public VertxNubes(Vertx vertx, JsonObject json) {
  this.vertx = vertx;
  config = Config.fromJsonObject(json, vertx);
  deploymentIds = new ArrayList<>();
  registry = new ParameterAdapterRegistry();
  marshallers = new HashMap<>();
  config.setMarshallers(marshallers);
  config.createAnnotInjectors(registry);

  // marshalling
  TemplateEngineManager templManager = new TemplateEngineManager(config);
  registerAnnotationProcessor(View.class, new ViewProcessorFactory(templManager));
  registerAnnotationProcessor(File.class, new FileProcessorFactory());
  registerMarshaller("text/plain", new PlainTextMarshaller());
  registerMarshaller("application/json", new JacksonPayloadMarshaller());
  String domainPackage = config.getDomainPackage();
  if (domainPackage != null) {
    try {
      Reflections reflections = new Reflections(domainPackage, new SubTypesScanner(false));
      registerMarshaller("application/xml", new JAXBPayloadMarshaller(reflections.getSubTypesOf(Object.class)));
    } catch (JAXBException je) {
      throw new VertxException(je);
    }
  }
  failureHandler = new DefaultErrorHandler(config, templManager, marshallers);

  // default processors/handlers
  CookieHandler cookieHandler = CookieHandler.create();
  registerAnnotationHandler(Cookies.class, cookieHandler);
  registerAnnotationHandler(CookieValue.class, cookieHandler);
  registerAnnotationHandler(Throttled.class, RateLimitationHandler.create(config));
  registerTypeProcessor(PaginationContext.class, new PaginationProcessor());
  registerTypeProcessor(Payload.class, new PayloadTypeProcessor(marshallers));
  registerAnnotationProcessor(Redirect.class, new ClientRedirectProcessorFactory());
  registerAnnotationProcessor(ContentType.class, new ContentTypeProcessorFactory());
  registerAnnotationProcessor(Logout.class, new LogoutProcessor());
}
 
Example #3
Source File: VxApiApplication.java    From VX-API-Gateway with MIT License 4 votes vote down vote up
/**
 * 创建http服务器
 * 
 * @param createHttp
 */
public void createHttpServer(Handler<AsyncResult<Void>> createHttp) {
	this.httpRouter = Router.router(vertx);
	httpRouter.route().handler(this::filterBlackIP);
	httpRouter.route().handler(CookieHandler.create());
	SessionStore sessionStore = null;
	if (vertx.isClustered()) {
		sessionStore = ClusteredSessionStore.create(vertx);
	} else {
		sessionStore = LocalSessionStore.create(vertx);
	}
	SessionHandler sessionHandler = SessionHandler.create(sessionStore);
	sessionHandler.setSessionCookieName(appOption.getSessionCookieName());
	sessionHandler.setSessionTimeout(appOption.getSessionTimeOut());
	httpRouter.route().handler(sessionHandler);
	// 跨域处理
	if (corsOptions != null) {
		CorsHandler corsHandler = CorsHandler.create(corsOptions.getAllowedOrigin());
		if (corsOptions.getAllowedHeaders() != null) {
			corsHandler.allowedHeaders(corsOptions.getAllowedHeaders());
		}
		corsHandler.allowCredentials(corsOptions.isAllowCredentials());
		if (corsOptions.getExposedHeaders() != null) {
			corsHandler.exposedHeaders(corsOptions.getExposedHeaders());
		}
		if (corsOptions.getAllowedMethods() != null) {
			corsHandler.allowedMethods(corsOptions.getAllowedMethods());
		}
		corsHandler.maxAgeSeconds(corsOptions.getMaxAgeSeconds());
		httpRouter.route().handler(corsHandler);
	}
	// 如果在linux系统开启epoll
	if (vertx.isNativeTransportEnabled()) {
		serverOptions.setTcpFastOpen(true).setTcpCork(true).setTcpQuickAck(true).setReusePort(true);
	}
	// 404页面
	httpRouter.route().order(999999).handler(rct -> {
		if (LOG.isDebugEnabled()) {
			LOG.debug("用户: " + rct.request().remoteAddress().host() + "请求的了不存的路径: " + rct.request().method() + ":" + rct.request().path());
		}
		HttpServerResponse response = rct.response();
		if (appOption.getNotFoundContentType() != null) {
			response.putHeader("Content-Type", appOption.getNotFoundContentType());
		}
		response.end(appOption.getNotFoundResult());
	});
	// 创建http服务器
	vertx.createHttpServer(serverOptions).requestHandler(httpRouter::accept).listen(serverOptions.getHttpPort(), res -> {
		if (res.succeeded()) {
			System.out.println(
					MessageFormat.format("{0} Running on port {1} by HTTP", appOption.getAppName(), Integer.toString(serverOptions.getHttpPort())));
			createHttp.handle(Future.succeededFuture());
		} else {
			System.out.println("create HTTP Server failed : " + res.cause());
			createHttp.handle(Future.failedFuture(res.cause()));
		}
	});
}
 
Example #4
Source File: VxApiApplication.java    From VX-API-Gateway with MIT License 4 votes vote down vote up
/**
 * 创建https服务器
 * 
 * @param createHttp
 */
public void createHttpsServer(Handler<AsyncResult<Void>> createHttps) {
	this.httpsRouter = Router.router(vertx);
	httpsRouter.route().handler(this::filterBlackIP);
	httpsRouter.route().handler(CookieHandler.create());
	SessionStore sessionStore = null;
	if (vertx.isClustered()) {
		sessionStore = ClusteredSessionStore.create(vertx);
	} else {
		sessionStore = LocalSessionStore.create(vertx);
	}
	SessionHandler sessionHandler = SessionHandler.create(sessionStore);
	sessionHandler.setSessionCookieName(appOption.getSessionCookieName());
	sessionHandler.setSessionTimeout(appOption.getSessionTimeOut());
	httpsRouter.route().handler(sessionHandler);
	// 跨域处理
	if (corsOptions != null) {
		CorsHandler corsHandler = CorsHandler.create(corsOptions.getAllowedOrigin());
		if (corsOptions.getAllowedHeaders() != null) {
			corsHandler.allowedHeaders(corsOptions.getAllowedHeaders());
		}
		corsHandler.allowCredentials(corsOptions.isAllowCredentials());
		if (corsOptions.getExposedHeaders() != null) {
			corsHandler.exposedHeaders(corsOptions.getExposedHeaders());
		}
		if (corsOptions.getAllowedMethods() != null) {
			corsHandler.allowedMethods(corsOptions.getAllowedMethods());
		}
		corsHandler.maxAgeSeconds(corsOptions.getMaxAgeSeconds());
		httpsRouter.route().handler(corsHandler);
	}
	// 创建https服务器
	serverOptions.setSsl(true);
	VxApiCertOptions certOptions = serverOptions.getCertOptions();
	if (certOptions.getCertType().equalsIgnoreCase("pem")) {
		serverOptions
				.setPemKeyCertOptions(new PemKeyCertOptions().setCertPath(certOptions.getCertPath()).setKeyPath(certOptions.getCertKey()));
	} else if (certOptions.getCertType().equalsIgnoreCase("pfx")) {
		serverOptions.setPfxKeyCertOptions(new PfxOptions().setPath(certOptions.getCertPath()).setPassword(certOptions.getCertKey()));
	} else {
		LOG.error("创建https服务器-->失败:无效的证书类型,只支持pem/pfx格式的证书");
		createHttps.handle(Future.failedFuture("创建https服务器-->失败:无效的证书类型,只支持pem/pfx格式的证书"));
		return;
	}
	Future<Boolean> createFuture = Future.future();
	vertx.fileSystem().exists(certOptions.getCertPath(), createFuture);
	createFuture.setHandler(check -> {
		if (check.succeeded()) {
			if (check.result()) {
				// 404页面
				httpsRouter.route().order(999999).handler(rct -> {
					if (LOG.isDebugEnabled()) {
						LOG.debug(
								"用户: " + rct.request().remoteAddress().host() + "请求的了不存的路径: " + rct.request().method() + ":" + rct.request().path());
					}
					HttpServerResponse response = rct.response();
					if (appOption.getNotFoundContentType() != null) {
						response.putHeader("Content-Type", appOption.getNotFoundContentType());
					}
					response.end(appOption.getNotFoundResult());
				});
				// 如果在linux系统开启epoll
				if (vertx.isNativeTransportEnabled()) {
					serverOptions.setTcpFastOpen(true).setTcpCork(true).setTcpQuickAck(true).setReusePort(true);
				}
				vertx.createHttpServer(serverOptions).requestHandler(httpsRouter::accept).listen(serverOptions.getHttpsPort(), res -> {
					if (res.succeeded()) {
						System.out.println(appOption.getAppName() + " Running on port " + serverOptions.getHttpsPort() + " by HTTPS");
						createHttps.handle(Future.succeededFuture());
					} else {
						System.out.println("create HTTPS Server failed : " + res.cause());
						createHttps.handle(Future.failedFuture(res.cause()));
					}
				});
			} else {
				LOG.error("执行创建https服务器-->失败:无效的证书或者错误的路径:如果证书存放在conf/cert中,路径可以从cert/开始,示例:cert/XXX.XXX");
				createHttps.handle(Future.failedFuture("无效的证书或者错误的路径"));
			}
		} else {
			LOG.error("执行创建https服务器-->失败:无效的证书或者错误的路径:如果证书存放在conf/cert中,路径可以从cert/开始,示例:cert/XXX.XXX", check.cause());
			createHttps.handle(Future.failedFuture(check.cause()));
		}
	});
}
 
Example #5
Source File: CloudIamAuthNexusProxyVerticle.java    From nexus-proxy with Apache License 2.0 4 votes vote down vote up
@Override
protected void preconfigureRouting(final Router router) {
    router.route().handler(CookieHandler.create());
    router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)).setSessionTimeout(SESSION_TTL));
}
 
Example #6
Source File: RestAPIVerticle.java    From vertx-blueprint-microservice with Apache License 2.0 4 votes vote down vote up
/**
 * Enable local session storage in requests.
 *
 * @param router router instance
 */
protected void enableLocalSession(Router router) {
  router.route().handler(CookieHandler.create());
  router.route().handler(SessionHandler.create(
    LocalSessionStore.create(vertx, "shopping.user.session")));
}
 
Example #7
Source File: RestAPIVerticle.java    From vertx-blueprint-microservice with Apache License 2.0 4 votes vote down vote up
/**
 * Enable clustered session storage in requests.
 *
 * @param router router instance
 */
protected void enableClusteredSession(Router router) {
  router.route().handler(CookieHandler.create());
  router.route().handler(SessionHandler.create(
    ClusteredSessionStore.create(vertx, "shopping.user.session")));
}
 
Example #8
Source File: RouterConfiguration.java    From vxms with Apache License 2.0 2 votes vote down vote up
/**
 * Define a cookie handler for your service, a cookie handler is always defined by default
 *
 * @param router {@link Router}
 */
default void cookieHandler(Router router) {
  router.route().handler(CookieHandler.create());
}