zuul网关 在过滤器返回参数中文乱码
2019-05-30 15:43
2955 查看
zuul网关配置文件:
中文乱码在配置文件中配置
##处理中文乱码
spring:
http:
encoding:
charset: UTF-8
enabled: true
force: true
###注册 中心 eureka: client: serviceUrl: defaultZone: http://localhost:8100/eureka/ server: port: 80 ###网关名称 spring: application: name: service-zuul ##处理中文乱码 http: encoding: charset: UTF-8 enabled: true force: true ### 配置网关反向代理 zuul: routes: ###自己命名 api-member: ### 以 /api-member/访问转发到会员服务 path: /api-member/** ##会员在注册中心的别名 serviceId: member api-order: ### 以 /api-order/访问转发到订单服务 path: /api-order/** ##订单在注册中心的别名 serviceId: order
过滤器代码
filterType返回类型
- PRE: 这种过滤器在请求被路由之前调用。我们可利用这种过滤器实现身份验证、在集群中选择请求的微服务、记录调试信息等。
- ROUTING:这种过滤器将请求路由到微服务。这种过滤器用于构建发送给微服务的请求,并使用Apache HttpClient或Netfilx Ribbon请求微服务。
- POST:这种过滤器在路由到微服务以后执行。这种过滤器可用来为响应添加标准的HTTP Header、收集统计信息和指标、将响应从微服务发送给客户端等。
- ERROR:在其他阶段发生错误时执行该过滤器。
import com.netflix.discovery.util.StringUtil; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.exception.ZuulException; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Component public class TokeFilter extends ZuulFilter { /** * pre:可以在请求被路由之前调用 * route:在路由请求时候被调用 * post:在route和error过滤器之后被调用 * error:处理请求时发生错误时被调用 * */ @Override public String filterType() { return "pre"; } //过滤器执行顺序 @Override public int filterOrder() { return 0; } //是否开启过滤器 @Override public boolean shouldFilter() { return true; } @Override public Object run() throws ZuulException { //获取上下文 RequestContext context=RequestContext.getCurrentContext(); HttpServletRequest request= context.getRequest(); String username=request.getParameter("userName"); if(StringUtils.isEmpty(username)){ context.setSendZuulResponse(false); context.setResponseBody("没有登录"); return null; } return null; } }
启动类
@SpringBootApplication @EnableEurekaClient @EnableZuulProxy public class ZuulStar { public static void main(String[] args) { SpringApplication.run(ZuulStar.class,args); }
(adsbygoogle = window.adsbygoogle || []).push({});
相关文章推荐
- 关于在spring boot中通过zuul网关上传文件使用MultipartFile获取的方式出现文件名中文乱码的问题
- springmvc 请求参数中文乱码 ,@ResponseBody 返回中文乱码问题
- 使用过滤器(Filter)解决请求参数中文乱码问题(复杂方式)
- struts2.2.3 配置中文乱码拦截过滤器 解决接收中文参数乱码问题
- java 过滤器(参数传递中文乱码)
- 解决php调用带输出参数的MySQL返回中文时乱码
- 零xml配置Spring MVC过滤器解决中文参数乱码
- struts2.2.3 配置中文乱码拦截过滤器 解决接收中文参数乱码问题
- struts2.2.3 配置中文乱码拦截过滤器 解决接收中文参数乱码问题
- 使用过滤器(Filter)解决请求参数中文乱码问题(复杂方式)
- 写一个函数,2 个参数,1 个字符串,1 个字节数,返回截取的字符串,要 求字符串中的中文不能出现乱码
- 使用过滤器(Filter)解决请求参数中文乱码问题(复杂方式)
- 写一个函数, 2个参数, 1个字符串, 1个字节数,返回截取的字符串,要 求字符串中的中文不能出现乱码: 如 ( “ 我ABC”, 4) 应该截为 “ 我AB”,输入( “ 我 ABC汉DEF”, 6
- jsp页面 URL传中文参数到Action里面出现乱码,用过滤器和控制器都解决
- struts2.2.3 配置中文乱码拦截过滤器 解决接收中文参数乱码问题
- 路由网关Zuul请求参数GBK乱码问题
- js,action,fusioncharts传参数中文乱码问题
- jquery获取URL中参数,解决中文乱码问题
- 使用过滤器解决中文乱码
- struts+ajax中文参数乱码解决问题