Spring 3.x - how do I redirect from a display that returns data?

I have a method in my controller:

@RequestMapping(value="getData", method=RequestMethod.GET) @ResponseBody public List<MyDataObj> getData() { return myService.getData(); } 

Data is returned as JSON or xsl, depending on the request.

If the person making the request is not allowed access to the data that I need to redirect the user to the "unauthorized" page, then something like this:

 @RequestMapping(value="getData", method=RequestMethod.GET) @ResponseBody public List<MyDataObj> getData() { if (!isAuthorized()) { // redirect to notAuthorized.jsp } return myService.getData(); } 

All the examples I've seen using Spring require the method to return either String or ModelAndView . I was thinking about using HttpServletResponse.sendRedirect() , but all my JSPs are under WEB-INF and cannot be reached directly.

How can I refuse access to the data request url?

+8
java redirect spring spring-mvc
source share
4 answers

A more elegant solution might be to use HandlerInterceptor , which will check for authorization by blocking any requests that are not allowed to continue. If the request then reaches your controller, you can consider it OK.

+5
source share

The answer is below. But your specific case will most likely work with a different approach.

 @RequestMapping(value = "/someUrl", method = RequestMethod.GET) @ResponseBody public Response someMethod(String someData, HttpServletResponse response){ if(someData=="redirectMe"){ response.sendRedirect(ppPageUrl); } ... } 
+4
source share

Another approach is a filter. You can move all security logic to a filter and save clean code in controllers.

+1
source share

Pretty simple:

Send error status to client

 response.setStatus(HttpServletResponse.SC_NO_CONTENT); 

and handle the same with ajax callback handler and redirect. :)

0
source share

All Articles