小编Pra*_*ddy的帖子

如何为Spring RESTful Web服务创建Spring Interceptor

我有一些没有web.xml的Spring RESTful(RestControllers)Web服务,我使用Spring启动来启动服务.

我想为Web服务添加授权层,并希望在实际调用Web服务本身之前将所有http请求路由到一个前端控制器.(我有一个代码来模拟autherisation层的会话行为,根据我从客户端发送的每个httpRequest生成的密钥来验证用户).

是否有任何标准Spring解决方案将所有请求路由到过滤器/前端控制器?

提前谢谢,Praneeth

编辑:添加我的代码

控制器:`

@RestController
public class UserService {
    UserDAO userDAO = new UserDAO();

    @RequestMapping(value="/login", method = RequestMethod.POST)
    @LoginRequired
    public String login(@RequestParam(value="user_name") String userName, @RequestParam(value="password") String password, HttpServletRequest request){
        return userDAO.login(userName, password);
    }
}`
Run Code Online (Sandbox Code Playgroud)

拦截器:

`

public class AuthenticationInterceptor implements HandlerInterceptor  {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
        System.out.println("In Interceptor");
        //return super.preHandle(request, response, handler);
        return true;
    }
    @Override
    public void postHandle( HttpServletRequest request, HttpServletResponse response,
            Object handler, ModelAndView modelAndView) throws Exception …
Run Code Online (Sandbox Code Playgroud)

spring-mvc spring-restcontroller spring-rest

15
推荐指数
2
解决办法
5万
查看次数