当前位置: 首页 > 工具软件 > Restangular > 使用案例 >

Restangular 拦截器

厍胤运
2023-12-01

1、后台部分:

package com.bio.framework.util.spring;

import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.aop.MethodBeforeAdvice;

import com.bio.base.annotation.SetCORNResponseHeader;
import com.bio.base.util.domain.ObjectUtils;
import com.bio.framework.dto.LpsMemberDto;

/**
 * @author jianana
 * 
 */
public class ResponseHeaderAdvice implements MethodBeforeAdvice {

	@Override
	public void before(Method method, Object[] args, Object target) throws Throwable {
		SetCORNResponseHeader setCORNResponseHeader = method.getAnnotation(SetCORNResponseHeader.class);
		if ((setCORNResponseHeader != null && !setCORNResponseHeader.isNeeded()) || ObjectUtils.isEmpty(setCORNResponseHeader)) {
			return;
		}
		String usernm = null;
		LpsMemberDto currentMemberInfo = null;
		for (Object arg : args) {
			if (arg instanceof HttpServletRequest) {
				usernm = ((HttpServletRequest) arg).getHeader("usernm");
				currentMemberInfo = (LpsMemberDto)((HttpServletRequest) arg).getSession().getAttribute("currentMemberInfo");
			}
		}
		for (Object arg : args) {
			if (arg instanceof HttpServletResponse) {
				if(ObjectUtils.isEmpty(usernm) || !currentMemberInfo.getUsernm().equals(usernm)){
					((HttpServletResponse) arg).setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
				}
				return;
			}
		}
	}
	
}
controller中需要拦截的方法@SetCORNResponseHeader(isNeeded=true)

2、app部分run中配置:

      Restangular.addFullRequestInterceptor(function (element, operation, route, url, headers, params, httpConfig) {
        return {
          headers: {usernm: "21312313213"}
        };
      });
      Restangular.setErrorInterceptor(function(res) {
        $state.go("login");
        return false;
      });



 类似资料: