带有nginx和tomcat的不区分大小写的URI

时间:2017-01-11 10:44:44

标签: java tomcat nginx https

应用程序部署在tomcat和nginx之前使用。所有请求都由nginx获取并传递给Tomcat。 重定向到nginx但不是tomcat。如何在tomcat上获取请求。

在nginx端使用nginx配置中的代码处理url不区分大小写 //配置文件代码

location ~* "^/[Aa][Bb][Cc]/(.*)$"
    {       
                proxy_set_header X-Forwarded-Host $host;
                proxy_set_header X-Forwarded-Server $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_pass http://127.0.0.1:8008/abc/$1?$args;
        }

其余部分由tomcat处理 我有一个过滤器类,我被要求uri并将其转换为小写并转发请求的uri。

@WebFilter("/AuthenticationFilter")
public class AuthenticationFilter implements Filter {

    public void init(FilterConfig fConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException
    {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        String url =request.getRequestURI();
        String uri =request.getRequestURI() +(request.getQueryString() != null ? "?" + request.getQueryString() : "");

        if (!url.toLowerCase().equals(url))
        {
            String newURI = uri.toLowerCase();
            HttpServletRequestWrapper wrappedRequest = new RequestUriChangingWrapper(request, newURI);

            if(request.getMethod().equalsIgnoreCase("POST"))
            {
                StringBuilder stringBuilder = new StringBuilder (1000);
                Scanner scanner = new Scanner(req.getInputStream ());

                while (scanner.hasNextLine())
                {
                    stringBuilder.append(scanner.nextLine());
                }
                wrappedRequest. setAttribute ("login_param", stringBuilder.toString());
            }
            else
            {
                //response.sendRedirect(newURI); it sends "redirect" command on browser. So browser will try to ask "127.0.0.1" on itself computer ))
            }

            RequestDispatcher
            dispatcher = wrappedRequest. getRequestDispatcher (request.getScheme() + "://" + request. getServerName () + newURI);
            dispatcher . forward (wrappedRequest, response);
        }
        else
        {
            chain.doFilter(req, res);
        }
    }

    @Override
    public void destroy() {
        //
    }

    public class RequestUriChangingWrapper extends HttpServletRequestWrapper {
        private String originalValue;
        private String newValue;
        public RequestUriChangingWrapper(HttpServletRequest request, String newValue) {
            super(request);
            this.newValue = newValue;
            this.originalValue = request.getRequestURI();
        }
        public String getRequestURI() {

                return newValue;
        }
    }
}

0 个答案:

没有答案