这个问题是网关什么的我都配置的对但是前端页面请求过来就直接报404
问题原因
全局过滤器的使用
全局过滤器是作用于所有路由的过滤器,无需单独配置。在Spring Cloud Gateway中,全局过滤器可以用来实现一些统一化处理的业务需求,例如权限认证、IP访问限制等。然而,如果不正确使用全局过滤器,可能会导致一些问题。
解决方法:在使用全局过滤器时,需要注意不要对网关的内部路径进行过滤限制,以免影响网关的正常工作。另外,全局过滤器的使用应该谨慎,避免对其他服务造成影响。
原本配置
package com.heima.wemedia.gateway.filter;
import com.heima.wemedia.gateway.util.AppJwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
/**
* @author L
*/
@Component
@Slf4j
public class AuthorizeFilter implements Ordered, GlobalFilter {
private final HttpHandler httpHandler;
public AuthorizeFilter(HttpHandler httpHandler) {
this.httpHandler = httpHandler;
}
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//1.获取request和response对象
ServerHttpRequest request = exchange.getRequest();
ServerHttpResponse response = exchange.getResponse();
//2.判断是否是登录
if(request.getURI().getPath().contains("/login")) {
//放行
return chain.filter(exchange);
}
//3.获取token
String token = request.getHeaders().getFirst("token");
//4.判断token是否存在
if(StringUtils.isBlank(token)){
response.setStatusCode(HttpStatus.UNAUTHORIZED);
return response.setComplete();
}
//5.判断token是否有效
try {
Claims claimsBody = AppJwtUtil.getClaimsBody(token);
//是否是过期
int result = AppJwtUtil.verifyToken(claimsBody);
if(result == 1 || result == 2){
response.setStatusCode(HttpStatus.UNAUTHORIZED);
return response.setComplete();
}
Object userId = claimsBody.get("id");
//存放到请求头中
ServerHttpRequest serverHttpRequest = request.mutate().headers(h -> {
h.add("userId", userId + "");
}).build();
exchange.mutate().request(serverHttpRequest);
} catch (Exception e) {
e.printStackTrace();
}
//6.放行
return chain.filter(exchange);
}
/**
* 优先级设置 值越小 优先级越高
* @return
*/
@Override
public int getOrder() {
return 0;
}
}
更改后配置
package com.heima.wemedia.gateway.filter;
import com.heima.wemedia.gateway.util.AppJwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
/**
* @author L
*/
@Component
@Slf4j
public class AuthorizeFilter implements Ordered, GlobalFilter {
// private final HttpHandler httpHandler;
// public AuthorizeFilter(HttpHandler httpHandler) {
// this.httpHandler = httpHandler;
// }
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//1.获取request和response对象
ServerHttpRequest request = exchange.getRequest();
ServerHttpResponse response = exchange.getResponse();
//2.判断是否是登录
if(request.getURI().getPath().contains("/login")) {
//放行
return chain.filter(exchange);
}
//3.获取token
String token = request.getHeaders().getFirst("token");
//4.判断token是否存在
if(StringUtils.isBlank(token)){
response.setStatusCode(HttpStatus.UNAUTHORIZED);
return response.setComplete();
}
//5.判断token是否有效
try {
Claims claimsBody = AppJwtUtil.getClaimsBody(token);
//是否是过期
int result = AppJwtUtil.verifyToken(claimsBody);
if(result == 1 || result == 2){
response.setStatusCode(HttpStatus.UNAUTHORIZED);
return response.setComplete();
}
Object userId = claimsBody.get("id");
//存放到请求头中
ServerHttpRequest serverHttpRequest = request.mutate().headers(h -> {
h.add("userId", userId + "");
}).build();
exchange.mutate().request(serverHttpRequest);
} catch (Exception e) {
e.printStackTrace();
}
//6.放行
return chain.filter(exchange);
}
/**
* 优先级设置 值越小 优先级越高
* @return
*/
@Override
public int getOrder() {
return 0;
}
}
HttpHandler 的问题
在 Spring Cloud Gateway 中,HttpHandler 通常不需要直接传入自定义过滤器。由于你在 AuthorizeFilter 中注入了 HttpHandler,这可能导致一些冲突。可以尝试移除 HttpHandler 的注入,或者查看它是否在其他地方被不当使用。
问题成功解决
标签:网关,exchange,import,springframework,filter,token,404,org,失效 From: https://www.cnblogs.com/freps/p/18487965