【问题标题】:Getting value of RequestParam within an Interceptor在拦截器中获取 RequestParam 的值
【发布时间】:2014-09-12 20:17:00
【问题描述】:

我希望你能帮助我。我有一个 Spring 拦截器,可以根据控制器方法的 @RequestMapping 中配置的 URL 和传递给控制器​​的参数(参数)来授权用户。所有这些请求参数都是使用 @RequestParam 注释配置的。我需要检索从拦截器中的@RequestParam 传递的值,以便我可以使用这些参数来验证正确的用户是否访问了 url,以及是否允许用户传入 documentId。请让我知道这是否可能。当我做 request.getParameter("documentId") 时,我什么也没得到。我有一些代码如下

(控制器方法)

@RequestMapping(value = "/viewDocument.html")
public ModelAndView viewDocument(@RequestParam("documentId");

拦截类

@Override
public boolean preHandle(final HttpServletRequest req, final HttpServletResponse resp, final Object handler) throws IOException {

    if (handler instanceof HandlerMethod) {
        final HandlerMethod handlerMethod = (HandlerMethod) handler;
        final RequestMapping requstMapping = handlerMethod.getMethodAnnotation(RequestMapping.class);

        if (requstMapping != null) {
            final AuthorizeRequest authorizeRequestAnnotation = handlerMethod.getMethodAnnotation(AuthorizeRequest.class);
            if (authorizeRequestAnnotation != null) {
                try {
                    checkAccess(req, requstMapping, handlerMethod);                        
                } catch (final SecurityException e) {
                    resp.sendError(HttpServletResponse.SC_FORBIDDEN, "You are not allowed to perform this function");
                    // return false;
                } catch (final Exception e) {
                    resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
                    // return false;
                }
            }
        }
    }

    return true;
}

private void checkAccess(final HttpServletRequest req, final RequestMapping requestMapping, final HandlerMethod handlerMethod) throws SecurityException {

    final Map<String, Object> arguments = Maps.newHashMap();

    final RequestMethod[] methods = requestMapping.method();
    final MethodParameter[] methodParameters = handlerMethod.getMethodParameters();

    for (final MethodParameter methodParameter : methodParameters) {
        String parameterName = null;

        final RequestParam requestParam = methodParameter.getParameterAnnotation(RequestParam.class);
        if (requestParam != null) {
            parameterName = requestParam.value();
            arguments.put(parameterName, req.getParameter(parameterName));
        }
    }

    final RuleValidator ruleValidator = rulesConfiguration.get(requestMapping.value()[0]);
    ruleValidator.validate(arguments);
}

这是我正在使用的 GET 方法。是的,如果我删除拦截器,则会发送 documentId。下面是我的拦截器配置

<mvc:interceptors>
   <bean class="mypackage.SecurityInterceptor" />
</mvc:interceptors>

【问题讨论】:

  • request.getParameter("documentId"), 应该可以工作。如果不是,那是因为您实际上没有发布documentId,或者是因为您的拦截器配置不正确,并且没有在您的控制器之前执行。
  • 感谢您的回复。拦截器配置正确,请求流向拦截器,但 request.getParameter("documentId") 总是返回 null :(
  • 好的,您检查过发布的请求是否真的发送了“documentId”?之前还有其他拦截器吗?你可以为拦截器添加你的spring mvc配置吗? ty
  • 这是我正在使用的 GET 方法。是的,如果我删除拦截器,则会发送 documentId。下面是我对拦截器的配置
  • 没有其他拦截器

标签: spring spring-mvc spring-security


【解决方案1】:

目前,我正在尝试实现同样的目标。所以我最后一次尝试:

request.getAttribute("org.springframework.web.servlet.HandlerMapping.uriTemplateVariables")

给你参数的值。

但我不确定这是不是正确的方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    • 2010-10-24
    • 2013-05-15
    • 1970-01-01
    • 2021-04-14
    相关资源
    最近更新 更多