![](/img/trans.png)
[英]How to add a custom security annotation to Spring MVC controller method
[英]add custom parameters to spring mvc controller
我有一個自定義的調度程序Servlet,它擴展了默認的DispatcherServlet以對所有請求進行一些驗證。
由於我將從請求(getInputStream()-> Map)中獲取所有參數以進行一些驗證,因此我想將參數傳遞給控制器或將參數添加到上下文中,以便可以從cotroller再次獲取它們。
現在,我只是將所有參數放到了全局Map中,但是我想知道是否有一些簡單的方法。
public class CustomDispatcherServlet extends DispatcherServlet {
private static final long serialVersionUID = 7250693017796274410L;
@Override
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
doFilter(request, response);
super.doDispatch(request, response);
}
...
private void doFilter(HttpServletRequest request, HttpServletResponse response) {
WLNResponse<String> error = null;
try {
boolean isSignValid = checkSignValidity(request);
...
private boolean checkSignValidity(HttpServletRequest request) throws IOException {
// pass this params to controller or somewhere I can get from controller
Map<String, Object> params = WebUtils.readParams(request);
...
我在驗證控制器本身中的參數時所采用的方法。 例如
@Controller
public ControllerClass
{
@RequestMapping(value = "/someurl", method = RequestMethod.GET, params = {
"requestParam"})
public void someMethod(@RequestParam(value = "requestParam") String requestParam)
{
System.out.println("This is the value of the RequestParam requestParam " + requestParam);
}
}
這樣,您可以在控制器內進行驗證。
唯一不能解決的是,是否沒有將發出的請求解析為有效的控制器。 為此,我將使用注釋@controllerAdvice
。
當前,我只使用request.setAttribute()將參數放入屬性中並從控制器獲取它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.