Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 获取拦截器中RequestParam的值_Spring_Spring Mvc_Spring Security - Fatal编程技术网

Spring 获取拦截器中RequestParam的值

Spring 获取拦截器中RequestParam的值,spring,spring-mvc,spring-security,Spring,Spring Mvc,Spring Security,我希望你能帮助我。我有一个Spring拦截器,可以根据控制器方法的@RequestMapping中配置的URL和传递给控制器的参数(参数)来授权用户。所有这些请求参数都是使用@RequestParam注释配置的。我需要在拦截器中检索从@RequestParam传递的值,以便使用这些参数验证url是否已被正确的用户访问,以及是否允许用户传入documentId。如果可能的话,请告诉我。当我请求.getParameter(“documentId”)时,我什么也得不到。我有一些代码如下 (控制器方法)

我希望你能帮助我。我有一个Spring拦截器,可以根据控制器方法的@RequestMapping中配置的URL和传递给控制器的参数(参数)来授权用户。所有这些请求参数都是使用@RequestParam注释配置的。我需要在拦截器中检索从@RequestParam传递的值,以便使用这些参数验证url是否已被正确的用户访问,以及是否允许用户传入documentId。如果可能的话,请告诉我。当我请求.getParameter(“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);
}
@覆盖
公共布尔预处理(最终HttpServletRequest请求、最终HttpServletResponse响应、最终对象处理程序)引发IOException{
if(HandlerMethod的处理程序实例){
最终HandlerMethod HandlerMethod=(HandlerMethod)处理程序;
final RequestMapping requstMapping=handlerMethod.getMethodAnnotation(RequestMapping.class);
if(requstMapping!=null){
final AuthorizeRequestAuthorizeRequestAnnotation=handlerMethod.getMethodAnnotation(AuthorizeRequest.class);
if(authorizeRequestAnnotation!=null){
试一试{
checkAccess(请求、请求映射、handlerMethod);
}捕获(最终安全异常e){
响应发送错误(HttpServletResponse.SC_禁止,“不允许您执行此功能”);
//返回false;
}捕获(最终异常e){
响应发送错误(HttpServletResponse.SC\u错误请求);
//返回false;
}
}
}
}
返回true;
}
私有void checkAccess(final-HttpServletRequest-req、final-RequestMapping-RequestMapping、final-HandlerMethod-HandlerMethod)抛出SecurityException{
最终映射参数=Maps.newHashMap();
final RequestMethod[]methods=requestMapping.method();
final MethodParameter[]methodParameters=handlerMethod.getMethodParameters();
对于(最终MethodParameter MethodParameter:methodParameters){
字符串参数name=null;
final RequestParam RequestParam=methodParameter.getParameterAnnotation(RequestParam.class);
if(requestParam!=null){
parameterName=requestParam.value();
参数.put(parameterName,req.getParameter(parameterName));
}
}
final RuleValidator RuleValidator=rulesConfiguration.get(requestMapping.value()[0]);
验证(参数);
}

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

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

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

request.getAttribute(“org.springframework.web.servlet.HandlerMapping.uriTemplateVariables”)

为您提供参数的值


但我不确定这是否正确。

request.getParameter(“documentId”),
应该可以工作。如果没有,那是因为您实际上没有发布
documentId
,或者是因为您的拦截器没有正确配置,并且没有在控制器之前执行。谢谢您的回复。拦截器配置正确,请求正在流向拦截器,但request.getParameter(“documentId”)始终返回null:(好,您是否检查了发布的请求以查看“documentId”是的,如果我删除了拦截器,就会发送documentId。下面是我的拦截器配置,没有其他拦截器
<mvc:interceptors>
   <bean class="mypackage.SecurityInterceptor" />
</mvc:interceptors>