我希望你能帮助我,我有一个Spring Interceptor,它根据控制器方法的@RequestMapping中配置的URL和参数对用户进行授权(parameters)传递给控制器。所有这些请求参数都是使用@RequestParam注解配置的。我需要检索从@ RequestParam,这样我就可以使用这些参数来验证url是否被正确的用户访问,以及是否允许用户传入documentId。请让我知道这是否可能。当我请求.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>
1条答案
按热度按时间lstz6jyr1#
现在,我也在努力实现同样的目标。所以我最后一次尝试:
request.getAttribute("org.springframework.web.servlet.HandlerMapping.uriTemplateVariables")
提供参数值。
但我不确定,这是正确方法