use of io.vertx.ext.web.sstore.SessionStore in project vertx-web by vert-x3.
the class WebExamples method example33.
public void example33(Vertx vertx) {
Router router = Router.router(vertx);
// We need a cookie handler first
router.route().handler(CookieHandler.create());
// Create a clustered session store using defaults
SessionStore store = ClusteredSessionStore.create(vertx);
SessionHandler sessionHandler = SessionHandler.create(store);
// Make sure all requests are routed through the session handler too
router.route().handler(sessionHandler);
// Now your application handlers
router.route("/somepath/blah/").handler(routingContext -> {
Session session = routingContext.session();
session.put("foo", "bar");
// etc
});
}
use of io.vertx.ext.web.sstore.SessionStore in project VX-API-Gateway by EliMirren.
the class VxApiApplication method createHttpsServer.
/**
* 创建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);
httpsRouter.route().handler(BodyHandler.create().setUploadsDirectory("../temp/file-uploads").setBodyLimit(appOption.getContentLength()));
// 跨域处理
if (corsOptions != null) {
CorsHandler corsHandler = CorsHandler.create(corsOptions.getAllowedOrigin());
corsHandler.allowedHeaders(corsOptions.getAllowedHeaders()).allowCredentials(corsOptions.isAllowCredentials()).exposedHeaders(corsOptions.getExposedHeaders()).allowedMethods(corsOptions.getAllowedMethods()).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 -> {
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()));
}
});
}
use of io.vertx.ext.web.sstore.SessionStore in project vertx-openshift-it by cescoffier.
the class WebSessionVerticle method setupRouter.
private Router setupRouter() {
Router router = Router.router(vertx);
router.put().handler(BodyHandler.create());
router.route().handler(CookieHandler.create());
SessionStore store = ClusteredSessionStore.create(vertx);
SessionHandler sessionHandler = SessionHandler.create(store);
router.route().handler(sessionHandler);
router.put("/web-session/:key").handler(rc -> {
String key = rc.pathParam("key");
String value = rc.getBodyAsString();
rc.session().put(key, value);
sendResponse(rc, Future.succeededFuture("OK"));
});
router.get("/web-session/:key").handler(rc -> {
String key = rc.pathParam("key");
String value = rc.session().get(key);
sendResponse(rc, Future.succeededFuture(value));
});
router.get("/health").handler(rc -> {
rc.response().putHeader(HttpHeaders.CONTENT_TYPE, "text/plain").end("OK");
});
return router;
}
use of io.vertx.ext.web.sstore.SessionStore in project vertx-web by vert-x3.
the class WebExamples method example32.
public void example32() {
// a clustered Vert.x
Vertx.clusteredVertx(new VertxOptions().setClustered(true), res -> {
Vertx vertx = res.result();
// Create a clustered session store using defaults
SessionStore store1 = ClusteredSessionStore.create(vertx);
// Create a clustered session store specifying the distributed map name to use
// This might be useful if you have more than one application in the cluster
// and want to use different maps for different applications
SessionStore store2 = ClusteredSessionStore.create(vertx, "myclusteredapp3.sessionmap");
});
}
use of io.vertx.ext.web.sstore.SessionStore in project vertx-web by vert-x3.
the class EventbusBridgeTest method testSendRequiresAuthorityHasAuthority.
@Test
public void testSendRequiresAuthorityHasAuthority() throws Exception {
sockJSHandler.bridge(defaultOptions.addInboundPermitted(new PermittedOptions().setAddress(addr).setRequiredAuthority("bang_sticks")));
router.clear();
router.route().handler(CookieHandler.create());
SessionStore store = LocalSessionStore.create(vertx);
router.route().handler(SessionHandler.create(store));
JsonObject authConfig = new JsonObject().put("properties_path", "classpath:login/loginusers.properties");
AuthProvider authProvider = ShiroAuth.create(vertx, ShiroAuthRealmType.PROPERTIES, authConfig);
addLoginHandler(router, authProvider);
router.route("/eventbus/*").handler(sockJSHandler);
testSend("foo");
}
Aggregations