spring boot websocket 自定义 response header

自定义websocket response header信息

  • 使用serverlet Filter 过滤器实现 创建过滤器类 WebsocketFilter 即可
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Component
@WebFilter(filterName = "WebsocketFilter", urlPatterns = "/*")
public class WebsocketFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig){
        System.out.println("执行filter");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        System.out.println("doing Filter ... ");
        response.setHeader("test","123456789");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}

posted @ 2020-09-10 12:15  jasondayee  阅读(1511)  评论(0编辑  收藏  举报