Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UrlCleaner#clean(String originUrl) 建议入参增加 HttpServletRequest #2071

Closed
paoyingshidai opened this issue Mar 5, 2021 · 1 comment
Labels
api-change Issues or PRs related to api changes area/integrations Issues or PRs related to integrations with open-source components kind/enhancement Category issues or prs related to enhancement.

Comments

@paoyingshidai
Copy link

我们在对 originUrl 进行清洗的时候发现,这个过程需要对 HttpServletReqeust 的依赖,但是原接口并没有提供足够的入参或者拓展。比如 我想要依赖 springMvc 中 RequestMappingHandlerMapping 的一些方法,这些方法是需要 HttpServletRequest 的,但是 UrlCleaner#clean(String orginUrl) 入参没有提供,所以就难拓展。希望可以完善这个

@sczyh30 sczyh30 added api-change Issues or PRs related to api changes area/integrations Issues or PRs related to integrations with open-source components kind/enhancement Category issues or prs related to enhancement. labels Mar 5, 2021
@cdfive
Copy link
Collaborator

cdfive commented Mar 5, 2021

可通过RequestContextHolder类直接获取HttpServletReqeust,公司项目里定制UrlCleaner#clean(String orginUrl)实现用到了该方法,用于支持热点商品相关接口的限流,已上线运行了几个月没有问题。

代码供参考:

/**
 * 获取HttpServletRequest对象
 */
protected HttpServletRequest getHttpServletRequest() {
    try {
        return ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
    } catch (Exception e) {
        log.error("get http servlet request error", e);
        return null;
    }
}

抽象类RequestContextHolder通过ThreadLocal保存的ServletRequestAttributes对象,里面有HttpServletRequest
HttpServletResponse对象,在spring-webmvc模块里FrameworkServlet类的buildRequestAttributes,initContextHolders方法有设置,然后提供了currentRequestAttributes静态方法供使用。

这样不用改变api

关联issue:#2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-change Issues or PRs related to api changes area/integrations Issues or PRs related to integrations with open-source components kind/enhancement Category issues or prs related to enhancement.
Projects
None yet
Development

No branches or pull requests

3 participants