在分布式應用場景下,我們可以利用網關對請求進行集中處理,實現了低耦合,高內聚的特性。
登陸權限驗證和鑒權的功能都可以在網關層面進行處理:
- 用戶登錄后簽署的jwt保存在header中,用戶信息則保存在redis中
- 網關應該對不需要登錄即可查看的網頁請求放行,即需要一個白名單存放放行請求路徑
- gateway 依賴包已經包含了webflux組件,能夠有效利用線程資源,提高效率,減少不必要的阻塞時間
- spring-cloud-starter-loadbalancer 通過服務名調用并自動輪詢實例,可以在后端代碼(WebClient + @LoadBalanced)中向其他服務請求數據庫數據
- 負載均衡就是把大量請求按照某種算法分攤到多個后端實例上,以提高吞吐量、避免單點熱點,并在實例掛掉時自動剔除。
從上至下:
定義一個日志記錄器,用于打印日志信息。
從配置文件中讀取 JWT 的密鑰。
Jackson 提供的 工具類,用于序列化/反序列化 JSON。
注入你自定義的網關配置類(GatewayConfig)。
用于發起 HTTP 請求,特別是微服務之間的調用。
從配置文件中讀取某個網關地址或標識。
public class AuthFilter implements GlobalFilter,先要實現這個接口
先看幾個輔助函數:
1、writeJson : 渲染json數據到前端,這里用來將錯誤情況下的信息返回給前端,封裝了狀態碼,響應頭等數據
private Mono<Void> writeJson(ServerHttpResponse resp, HttpStatus status, ResponseResult jsonResult) {resp.setStatusCode(status);//401狀態碼resp.getHeaders().add("Content-Type", "application/json;charset=utf-8");try {String json = objectMapper.writeValueAsString(jsonResult);DataBuffer db = resp.bufferFactory().wrap(json.getBytes(StandardCharsets.UTF_8));return resp.writeWith(Mono.just(db));} catch (JsonProcessingException ex) {throw new RuntimeException("json序列化異常", ex);}}
這 6 行代碼就是 WebFlux 版“返回 JSON 響應”的模板:
設狀態碼 → 設頭 → 序列化 → 包成 Netty 緩沖區 → 異步寫出 → 異常兜底。
這里展示一下Mono< T>的區別
對比維度 | Mono<String> | Mono<Void> |
---|---|---|
語義 | 0~1 個字符串 | 0 個數據(只發 onComplete /onError ) |
能拿到值嗎? | 可以 block() / subscribe(s -> …) 拿到具體字符串 | 拿不到任何值;只能知道“事件結束”或“出錯” |
典型用途 | 查數據庫、調接口、讀文件……有返回體 | 寫響應、刪數據、發消息……只關心成功/失敗 |
序列化內容 | 會把字符串寫出去 | 沒有 body,只能寫狀態碼/頭 |
實際發出的 HTTP 包 | 有 Content-Length > 0 的 body | body 長度為 0(只有響應行+頭) |
2、Java的record
private record UriPattern(String[] allowedMethods, String pattern) {//判斷是否匹配某個請求類型private boolean matchMethod(String method) {for (String am : allowedMethods) {if (am.equalsIgnoreCase(method) || "*".equals(am)) {return true;}}return false;}private static UriPattern of(String uri) {String[] parts = uri.split(":");if (parts.length == 1) {return new UriPattern(new String[]{"*"}, parts[0]);} else {return new UriPattern(parts[0].split(","), parts[1]);}}}
等效于下面的類:
import java.util.Arrays;
import java.util.Objects;public final class UriPattern {/* 1. 字段 */private final String[] allowedMethods;private final String pattern;/* 2. 全參構造器 */public UriPattern(String[] allowedMethods, String pattern) {// 防御性復制,防止外部數組被修改this.allowedMethods = Arrays.copyOf(allowedMethods, allowedMethods.length);this.pattern = pattern;}/* 3. 業務方法:判斷方法是否允許 */public boolean matchMethod(String method) {for (String am : allowedMethods) {if (am.equalsIgnoreCase(method) || "*".equals(am)) {return true;}}return false;}/* 4. 靜態工廠:解析配置串 "GET,POST:/api/user/**" */public static UriPattern of(String uri) {String[] parts = uri.split(":");if (parts.length == 1) {// 只有 URI 模式,方法默認通配return new UriPattern(new String[]{"*"}, parts[0]);} else {// parts[0] 是方法列表,parts[1] 是 URI 模式return new UriPattern(parts[0].split(","), parts[1]);}}/* 5. getter(可選,方便外部讀取) */public String[] getAllowedMethods() {return Arrays.copyOf(allowedMethods, allowedMethods.length);}public String getPattern() {return pattern;}/* 6. equals / hashCode / toString 與 record 默認邏輯一致 */@Overridepublic boolean equals(Object o) {if (this == o) return true;if (!(o instanceof UriPattern)) return false;UriPattern that = (UriPattern) o;return Arrays.equals(allowedMethods, that.allowedMethods)&& Objects.equals(pattern, that.pattern);}@Overridepublic int hashCode() {int result = Objects.hash(pattern);result = 31 * result + Arrays.hashCode(allowedMethods);return result;}@Overridepublic String toString() {return "UriPattern[allowedMethods=" + Arrays.toString(allowedMethods)+ ", pattern=" + pattern + ']';}
}
of工廠類最為關鍵,作用是:
配置字符串 | split(“:”) 結果 | parts.length | 進入分支 | 最終 UriPattern |
---|---|---|---|---|
get:/api/v1/user/captcha/** | ["get", "/api/v1/user/captcha/**"] | 2 | else | allowedMethods=["get"], pattern=/api/v1/user/captcha/** |
/api/v1/user/captcha/** | ["/api/v1/user/captcha/**"] | 1 | if | allowedMethods=["*"], pattern=/api/v1/user/captcha/** |
GET,POST:/api/health | ["GET,POST", "/api/health"] | 2 | else | allowedMethods=["GET","POST"], pattern=/api/health |
* | ["*"] | 1 | if | allowedMethods=["*"], pattern=* |
3、hasPerm,檢查請求的uri是否是有權限的,鑒權核心
private boolean hasPerm(String uri, String method, List<String> userPerms, Map<String, List<String>> resourcePermMappings) {PathMatcher matcher = new AntPathMatcher();//1.找到訪問uri所需要的權限for (Map.Entry<String, List<String>> entry : resourcePermMappings.entrySet()) {String resource = entry.getKey();//資源,模式List<String> perms = entry.getValue();UriPattern up = UriPattern.of(resource);if (matcher.match(up.pattern(), uri) && up.matchMethod(method)) {for (String perm : perms) {if (userPerms.contains(perm)) {return true;}}}}return false;}
按資源模式輪詢 → 路徑+方法匹配 → 任一所需權限在用戶列表里即立即放行,全掃完還沒命中就拒絕。
理解這三個輔助方法,思路就很明確了,我們要讀取前端發過來的請求,并通過負載均衡輪詢方式請求rbac服務的資源映射關系,之后就能夠利用hasPerm方法來判斷了,有錯的就返回對應的錯誤信息即可,難點是解析uri
別忘了jwt驗證要先做,之后再鑒權
全部代碼如下:
@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {ServerHttpRequest req = exchange.getRequest();String uri = req.getPath().toString();//當前請求路徑String ctx = req.getPath().contextPath().value();//上下文路徑uri = uri.replace(ctx, "");//無上下文的請求路徑String method = req.getMethod().toString();final String finalUri = uri;//對白名單中的地址直接放行List<String> ignoreUrls = gatewayConfig.getWhiteList();PathMatcher matcher = new AntPathMatcher();for (String pattern : ignoreUrls) {UriPattern up = UriPattern.of(pattern);if (matcher.match(up.pattern(), uri) && up.matchMethod(method)) {return chain.filter(exchange);//直接放行}}String jwt = req.getHeaders().getFirst("Authorization");if (!StringUtils.hasText(jwt)) {jwt = req.getQueryParams().getFirst("jwt");}if (StringUtils.hasText(jwt)) {//校驗tokenJWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(jwtSecret)).build();//從jwt中取出有效數據,進行業務使用,如從Redis中獲取用戶數據try {DecodedJWT dj = jwtVerifier.verify(jwt);//從session或Redis中取出用戶數據Integer userId = dj.getClaim("userId").asInt();//用戶idString username = dj.getAudience().get(0);//用戶名//鑒權System.out.println(userId + ":" + username);//1.找出當前請求所需要的權限,resource_perm_mappingsWebClient webClient = webClientBuilder.build();return webClient.get().uri("http://" + gateway + "/api/v1/rbac/resource_perm_mappings").retrieve().bodyToMono(ResponseResult.class).flatMap(it -> {Map<String, List<String>> resourcePermMappings = (Map<String, List<String>>) it.getData();//2.找出當前用戶所有的權限return webClient.get().uri("http://" + gateway + "/api/v1/rbac/perms/{userId}/true", userId).retrieve().bodyToMono(ResponseResult.class).flatMap(it1 -> {List<String> perms = (List<String>) it1.getData();//當前用戶擁有的所有權限if (hasPerm(finalUri, method, perms, resourcePermMappings)) {//return chain.filter(exchange);//放行String authInfo = userId + ":" + username;authInfo = Base64.getEncoder().encodeToString(authInfo.getBytes());ServerHttpRequest mutatedRequest = exchange.getRequest().mutate().header("x-auth-info", authInfo).build();return chain.filter(exchange.mutate().request(mutatedRequest).build());} else {//鑒權未通過return writeJson(exchange.getResponse(), HttpStatus.FORBIDDEN, ResponseResult.errorResult(-1,"無權訪問"));}});});} catch (JWTVerificationException e) {log.log(Level.SEVERE, "jwt校驗異常", e);return writeJson(exchange.getResponse(), HttpStatus.UNAUTHORIZED,ResponseResult.errorResult(-1,"jwt無效或已過期"));}} else {return writeJson(exchange.getResponse(), HttpStatus.UNAUTHORIZED, ResponseResult.errorResult(-1,"無jwt,請重新認證"));}}
問題來了:A服務遠程調用B服務時,該怎么確定他有沒有權限呢?
答:通過AOP切面編程,先鑒權再進入方法執行。
具體做法:
1、獲取用戶信息放到當前線程中:(Servlet 容器(Tomcat)默認是“一個請求 = 一條線程”全程處理)
上面的鑒權系統,只能進行鑒權,無法將已經鑒權的用戶信息進行保存,因此這里要新建一個攔截器來進行操作
public class AuthHandlerInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest req, @NonNull HttpServletResponse resp, @NonNull Object handler) throws Exception {//從請求頭中獲取認證信息,經過base64編碼過的String base64AuthInfo = req.getHeader(AuthInfo.AUTH_INFO_HEADER_KEY);if (base64AuthInfo == null) {base64AuthInfo = req.getParameter(AuthInfo.AUTH_INFO_HEADER_KEY);//嘗試從請求參數中獲取(針對特殊情況)}if (StringUtils.hasText(base64AuthInfo)) {//base64解碼String authInfo = new String(getDecoder().decode(base64AuthInfo));String[] arr = authInfo.split(":");String userId = arr[0];//用戶id,非主鍵String userName = arr[1];//用戶名//設置到當前線程中AuthInfo.setCurrent(AuthInfo.of(userId, userName));}return true;}
}
這里的AuthInfo是自定義類:
/*** 認證信息記錄類,用于封裝用戶認證相關信息* 使用Java record類型實現,自動生成構造方法、getter、equals、hashCode和toString方法*/
public record AuthInfo(String userId, String username) {/*** 認證信息請求頭名稱常量*/public static final String AUTH_INFO_HEADER_KEY = "x-auth-info";/*** 空認證信息實例,表示匿名用戶*/public static final AuthInfo EMPTY = AuthInfo.of("0", "匿名用戶");/*** 線程本地變量,用于存儲當前線程的認證信息*/private static final ThreadLocal<AuthInfo> INFO_THREAD_LOCAL = new ThreadLocal<>();/*** 創建認證信息實例的工廠方法* @param userId 用戶ID* @param userName 用戶名* @return 新的AuthInfo實例*/public static AuthInfo of(String userId, String userName) {return new AuthInfo(userId, userName);}/*** 獲取當前線程的認證信息* @return 當前線程的認證信息,如果不存在則返回空認證信息*/public static AuthInfo current() {AuthInfo ai = INFO_THREAD_LOCAL.get();return ai != null ? ai : EMPTY;}/*** 設置當前線程的認證信息* @param info 要設置的認證信息,不能為null* @throws NullPointerException 如果傳入的認證信息為null*/public static void setCurrent(AuthInfo info) {Objects.requireNonNull(info, "用戶認證信息不可為空");INFO_THREAD_LOCAL.set(info);}public static void clear() {INFO_THREAD_LOCAL.remove();}
}
2、創建方法攔截器,并加上增加/修改時間等信息
/*** aop,攔截所有業務操作,如果是保存或修改操作,如果參數是繼承自AuditEntity的,則自動填充創建時間和更新時間*/
public class AutoAuditMethodInterceptor implements MethodInterceptor {@Overridepublic Object invoke(@NonNull MethodInvocation invocation) throws Throwable {String method = invocation.getMethod().getName();if (method.equals("save") || method.equals("update")) {Object[] args = invocation.getArguments();if (args.length > 0) {Object arg0 = args[0];//首參if (arg0 instanceof BaseModel ae) {AuthInfo authInfo = AuthInfo.current();ae.setUpdatedBy(authInfo.username());ae.setUpdatedTime(LocalDateTime.now());if (method.equals("save")) {ae.setCreatedBy(authInfo.username());ae.setCreatedTime(LocalDateTime.now());}}}}return invocation.proceed();}
}
3、創建全局配置類
/*** 用于微服務自動獲取當前認證用戶信息的自動配置。* 當前配置類和Advisor上添加@Role(BeanDefinition.ROLE_INFRASTRUCTURE),是為了避免進行代理檢查,導致控制臺出現警告(對程序正常運行無影響)*/
@AutoConfiguration
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public class GlobalAutoConfiguration implements WebMvcConfigurer {private ApplicationContext applicationContext;@Autowiredpublic void setApplicationContext(ApplicationContext applicationContext) {this.applicationContext = applicationContext;}@Overridepublic void addInterceptors(@NonNull InterceptorRegistry registry) {try {AuthHandlerInterceptor interceptor = applicationContext.getBean(AuthHandlerInterceptor.class);registry.addInterceptor(interceptor);} catch (BeansException e) {//do nothing...}}/*** 創建攔截器,獲取當前認證用戶信息** @return 攔截器實例*/@ConditionalOnProperty(prefix = "shoplook2025.services.auto-get-auth", name = "enabled", havingValue = "true", matchIfMissing = true)@ConditionalOnMissingBean(name = "authHandlerInterceptor")@Beanpublic AuthHandlerInterceptor authHandlerInterceptor() {return new AuthHandlerInterceptor();}/*** 創建切面,自動為審計類型的模型類,添加創建時間和更新時間,以及創建人、更新人* 注意:必須必須添加@EnableAspectJAutoProxy注解,否則Advisor不生效** @return 切面實例*/@Role(BeanDefinition.ROLE_INFRASTRUCTURE)@Beanpublic Advisor autoAuditAspect() {AspectJExpressionPointcut pc = new AspectJExpressionPointcut();//execution表達式默認僅匹配指定類中直接聲明的方法。若方法定義在父類或接口中,但未被實現類重寫,則不會被識別pc.setExpression("execution(* com.situ.shoplook2025.*.api.service.impl.*.*(..))");return new DefaultPointcutAdvisor(pc, new AutoAuditMethodInterceptor());}
}