在Vert.x Web应用开发中,实现持久化登录体验需要结合用户认证体系和会话管理机制,通过合理的配置和代码实现,让用户在登录后的一段时间内无需重复验证身份,提升使用体验。

用户认证基础实现
Vert.x Web提供了AuthHandler来处理用户认证逻辑,我们可以结合内置的认证提供者完成基础的账号密码校验。首先需要引入相关的依赖,以Maven为例,添加以下依赖到pom.xml文件:
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>4.5.0</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-auth-properties</artifactId>
<version>4.5.0</version>
</dependency>
接下来创建认证提供者,这里使用基于属性文件的简单认证方式,实际开发中可替换为数据库认证等自定义实现:
import io.vertx.core.Vertx;
import io.vertx.ext.auth.properties.PropertyFileAuthentication;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BasicAuthHandler;
public class AuthExample {
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
Router router = Router.router(vertx);
// 创建基于属性文件的认证提供者,指定用户配置文件路径
PropertyFileAuthentication authProvider = PropertyFileAuthentication.create(vertx, "users.properties");
// 添加基础认证处理器,保护需要登录的路由
router.route("/protected/*").handler(BasicAuthHandler.create(authProvider));
// 受保护的接口
router.get("/protected/profile").handler(ctx -> {
ctx.response().end("这是受保护的用户资料页面");
});
vertx.createHttpServer().requestHandler(router).listen(8080);
}
}
会话管理配置
仅做认证还不够,需要结合会话管理来保持登录状态,避免每次请求都需要重新认证。Vert.x Web的会话管理需要依赖SessionHandler,同时需要选择会话存储方式,默认是本地内存存储,适合单实例场景。
首先添加会话相关的依赖:
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-session</artifactId>
<version>4.5.0</version>
</dependency>
配置会话处理器的代码示例如下:
import io.vertx.core.Vertx;
import io.vertx.ext.auth.properties.PropertyFileAuthentication;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BasicAuthHandler;
import io.vertx.ext.web.handler.SessionHandler;
import io.vertx.ext.web.sstore.LocalSessionStore;
public class SessionExample {
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
Router router = Router.router(vertx);
// 创建本地会话存储
LocalSessionStore sessionStore = LocalSessionStore.create(vertx);
// 创建会话处理器,设置会话过期时间为1小时(单位:毫秒)
SessionHandler sessionHandler = SessionHandler.create(sessionStore)
.setSessionTimeout(3600000);
// 全局应用会话处理器
router.route().handler(sessionHandler);
// 创建认证提供者
PropertyFileAuthentication authProvider = PropertyFileAuthentication.create(vertx, "users.properties");
// 认证处理器放在会话处理器之后
router.route("/protected/*").handler(BasicAuthHandler.create(authProvider));
// 登录接口,认证成功后将会话标记为已登录
router.post("/login").handler(ctx -> {
// 这里简化逻辑,实际中BasicAuthHandler会先校验,通过后设置用户到会话
ctx.session().put("user", ctx.user().principal().getString("username"));
ctx.response().end("登录成功");
});
// 登出接口,销毁会话
router.get("/logout").handler(ctx -> {
ctx.session().destroy();
ctx.response().end("登出成功");
});
router.get("/protected/profile").handler(ctx -> {
String username = ctx.session().get("user");
ctx.response().end("当前登录用户:" + username);
});
vertx.createHttpServer().requestHandler(router).listen(8080);
}
}
持久化会话存储实现
本地内存存储的会话在服务重启或者多实例部署时会丢失,要实现真正的持久化登录体验,需要使用持久化的会话存储,比如基于Redis的存储。首先添加Redis会话存储的依赖:
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-session-redis</artifactId>
<version>4.5.0</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-redis-client</artifactId>
<version>4.5.0</version>
</dependency>
使用Redis存储会话的配置代码如下:
import io.vertx.core.Vertx;
import io.vertx.ext.auth.properties.PropertyFileAuthentication;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BasicAuthHandler;
import io.vertx.ext.web.handler.SessionHandler;
import io.vertx.ext.web.sstore.redis.RedisSessionStore;
import io.vertx.redis.client.Redis;
import io.vertx.redis.client.RedisOptions;
public class PersistentSessionExample {
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
Router router = Router.router(vertx);
// 配置Redis连接,这里使用本地Redis示例,实际替换为对应地址
RedisOptions redisOptions = new RedisOptions()
.setConnectionString("redis://127.0.0.1:6379");
Redis redisClient = Redis.createClient(vertx, redisOptions);
// 创建Redis会话存储
RedisSessionStore sessionStore = RedisSessionStore.create(vertx, redisClient);
// 创建会话处理器,设置会话过期时间
SessionHandler sessionHandler = SessionHandler.create(sessionStore)
.setSessionTimeout(3600000);
router.route().handler(sessionHandler);
// 认证配置
PropertyFileAuthentication authProvider = PropertyFileAuthentication.create(vertx, "users.properties");
router.route("/protected/*").handler(BasicAuthHandler.create(authProvider));
router.get("/protected/profile").handler(ctx -> {
String username = ctx.session().get("user");
ctx.response().end("当前登录用户:" + username + ",会话已持久化存储");
});
vertx.createHttpServer().requestHandler(router).listen(8080);
}
}
注意事项
- 会话过期时间需要根据业务需求合理设置,过短会影响用户体验,过长会带来安全风险
- 生产环境中认证信息不要使用简单的属性文件,建议对接数据库或者专业的认证服务
- 多实例部署时务必使用Redis等集中式会话存储,避免会话不一致问题
- 敏感操作建议增加二次校验,不要仅依赖会话状态判断用户权限
Vert.x_Web用户认证会话管理持久化登录修改时间:2026-07-04 06:18:31