开发者社区> 问答> 正文

Spring启动GenericFilterBean,客户端返回过滤器返回错误代码和响应头

我有一个过滤器

package com.vs.security.filter;

import org.springframework.stereotype.Component;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class TokenFilter extends GenericFilterBean {

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

    HttpServletRequest httpRequest = asHttp(servletRequest);
    HttpServletResponse httpResponse = asHttp(servletResponse);
    boolean explicitlyTrue = true;

    if (explicitlyTrue) {
        httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Provided Information is Invalid");
        httpResponse.addHeader("SC_UNAUTHORIZED", "Provided Information is Invalid");

        return;
    }


    filterChain.doFilter(servletRequest, servletResponse);

}

private HttpServletRequest asHttp(ServletRequest request) {
    return (HttpServletRequest) request;
}

private HttpServletResponse asHttp(ServletResponse response) {
    return (HttpServletResponse) response;
}
}

我对vuejs的要求

form () {
    debugger
    this.$axios.get(this.$dbServer+this.endpoint+'form')
      .then((res) => {
        debugger
        this.fd = res.data.fd;
      }).catch(() => { this.notifyOnFailure(this.oopsMessage) })
  }

正如我发送的那样SC_UNAUTHORIZED = 401,但是在客户端,我无法获取此401。我什至在浏览器检查中看到“网络”选项卡(如下所示)。此外,我还要添加标题。但无法在客户端获取此标头值。

Request URL: http://********:8081/*******/form
Referrer Policy: no-referrer-when-downgrade
Content-Type: application/json
Date: Sun, 15 Mar 2020 06:29:00 GMT
Transfer-Encoding: chunked
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Connection: keep-alive
Host: localhost:8081
Origin: http://*******:8080
Referer: http://*******:8080/
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) 
Chrome/80.0.3987.132 Safari/537.36

问题来源:Stack Overflow

展开
收起
montos 2020-03-25 13:26:45 2021 0
1 条回答
写回答
取消 提交回答
  • 你要的交换顺序sendError和addHeader电话

    @Bean
        GenericFilterBean genericFilterBean() {
            return new GenericFilterBean() {
                @Override
                public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
                    HttpServletResponse resp = ((HttpServletResponse) response);
                    resp.addHeader("SC_UNAUTHORIZED", "Provided Information is Invalid");
                    resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Provided Information is Invalid");
                }
            };
        }
    

    结果:

    HTTP/1.1 401
    SC_UNAUTHORIZED: Provided Information is Invalid
    Content-Type: text/html;charset=UTF-8
    Content-Language: en-US
    Content-Length: 320
    Date: Sun, 15 Mar 2020 06:47:10 GMT
    Keep-Alive: timeout=60
    Connection: keep-alive
    

    回答来源:Stack Overflow

    2020-03-25 13:27:24
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多