思路:通过过滤器针对某一类接口链接进行拦截,并调用HttpServletRequestWrapper 修改入参参数。具体如下
1.第一步骤自定义过滤器对具体方法过滤
2.第二步骤 自定义ChannelRequest 类
重写 getParameterNames 方法添加需要增加的参数 ,重写getParameterNames 方法 对增加的参数初始化赋值
/**
* 动态添加渠道信息
* @author songfz
* @date 2019/7/9 10:34
*/
public class ChannelRequest extends HttpServletRequestWrapper {
private static final LoggerLOG = LoggerFactory.getLogger(ChannelRequest.class);
private Stringtoken ;
private StringchannelId;
private final ConsulPropsconsulProps = ApplicationContextHolder.getBean(ConsulProps.class);
public ChannelRequest(HttpServletRequest request) {
super(request);
token = request.getHeader(consulProps.getHeaderToken());
channelId = request.getParameter("channelId");
}
/**
* 重写 getParameterNames 添加需要的变量名称
* @return
*/
@Override
public Enumeration getParameterNames() {
Enumeration parameterNames =super.getParameterNames();
ArrayList list = Collections.list(parameterNames);
list.add("channelId");
return Collections.enumeration(list);
}
/**
* 重写 getParameterValues 对添加的变量赋值
* @param name
* @return
*/
@Override
public String[] getParameterValues(String name) {
// 判断当前用户管理员还是渠道用户
if("channelId".equalsIgnoreCase(name) && StringUtils.isEmpty(channelId)){
ManagerTypeDto managerTypeDto = ApplicationContextHolder.getBean(AuthService.class).getManagerChannelIdByToken(token);
if(managerTypeDto.getType().equals(Manager.ManagerType.CHANNEL.name())){
channelId = managerTypeDto.getCode();
LOG.info("获取的渠道值:"+channelId);
return new String[]{channelId};
}
}
return super.getParameterValues(name);
}
}
网友评论