package com.cyynote.controller; import io.jsonwebtoken.Claims; import org.noear.solon.annotation.Component; import org.noear.solon.core.handle.Context; import org.noear.solon.core.handle.Handler; import org.noear.solon.core.handle.Result; import org.noear.solon.core.route.RouterInterceptor; import org.noear.solon.core.route.RouterInterceptorChain; import org.noear.solon.sessionstate.jwt.JwtUtils; @Component public class JwtInterceptor implements RouterInterceptor { public void doIntercept(Context ctx, Handler mainHandler, RouterInterceptorChain chain) throws Throwable { boolean requireAuth = !("/api/auth/signup".equals(ctx.path()) || "/api/auth/signin".equals(ctx.path()) || "/demo".equals(ctx.path()) || "/api/auth/logout".equals(ctx.path()) || "OPTIONS".equalsIgnoreCase(ctx.method())); if (requireAuth) { String token = ctx.header("Authorization"); if (token == null || token.isBlank()) { ctx.status(401); ctx.render(Result.failure(401, "未登录或登录已过期")); return; } token = token.replace("Bearer ", ""); Claims claims = JwtUtils.parseJwt(token); if (claims == null) { ctx.status(401); ctx.render(Result.failure(401, "未登录或登录已过期")); return; } } chain.doIntercept(ctx, mainHandler); } }