`

iintercepter

阅读更多
HandlerInterceptor 拦截

基于response为RESTfulMessage


public class LoginIntercepter  implements HandlerInterceptor {

    public   boolean preHandle(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse, Object handler) throws java.lang.Exception{
        String uri=httpServletRequest.getRequestURI();
        String user=(String) httpServletRequest.getSession().getAttribute("user");
    if(!StringUtils.isEmpty(user)){
        return true;
    }
        if(handler instanceof HandlerMethod){
            HandlerMethod handlerMethod=(HandlerMethod)handler;
            Method method=handlerMethod.getMethod();
            AccessRequired accessRequired=method.getAnnotation(AccessRequired.class);
            if(accessRequired!=null){
                RESTfulMessage resTfulMessage=RESTfulMessage.error().message("not login").build();
                httpServletResponse.setCharacterEncoding("UTF-8");
                httpServletResponse.setContentType("application/json;charset=utf-8");
                httpServletResponse.getWriter().write(JsonUtil.objToJson(resTfulMessage));
            }
        }

        return true;
    }

    @Override
public   void postHandle(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse, java.lang.Object o, org.springframework.web.servlet.ModelAndView modelAndView) throws java.lang.Exception{}

    @Override
public void afterCompletion(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse, java.lang.Object o, java.lang.Exception e) throws java.lang.Exception{}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics