update
This commit is contained in:
@@ -1,44 +1,39 @@
|
||||
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 flag = true;
|
||||
if ("/api/auth/signup".equals(ctx.path()) ||
|
||||
"/api/auth/signin".equals(ctx.path()) ||
|
||||
"/demo".equals(ctx.path()) ||
|
||||
"/api/auth/logout".equals(ctx.path())
|
||||
|
||||
)
|
||||
flag = false;
|
||||
if (flag)
|
||||
if (null != ctx.header("Authorization")) {
|
||||
String token = ctx.header("Authorization");
|
||||
token = token.replace("Bearer ", "");
|
||||
System.out.println("token:" + token);
|
||||
Claims claims = JwtUtils.parseJwt(token);
|
||||
System.out.println("claims:" + claims);
|
||||
if (null != claims) {
|
||||
System.out.println("username:" + claims.get("user_name"));
|
||||
System.out.println("exp:" + claims.get("exp"));
|
||||
} else {
|
||||
ctx.status(401);
|
||||
ctx.render(Result.failure(401, ""));
|
||||
}
|
||||
} else {
|
||||
ctx.status(401);
|
||||
ctx.render(Result.failure(401, ""));
|
||||
}
|
||||
chain.doIntercept(ctx, mainHandler);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user