import com.alpha.redis.service.RedisService;
import com.alpha.web.config.RequestInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 并发拦截器
* @author hui66
*
*/
@Service
public class ConcurrenceInterceptor extends RequestInterceptor {
@Autowired
private RedisService redisService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
return beginControlErupt(request, response);
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
endControlErupt(request);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
endControlErupt(request);
}
public void endControlErupt(HttpServletRequest request) {
String uri = request.getRequestURI();
uri = uri.replace("//", "/");
String autoKey = "currence";
if(SessionInterceptor.getCurrentCustomer()!=null) {
if(uri.contains("/ptn")) {
String userKey = request.getParameter("openId");
if(userKey == null) {
userKey = request.getParameter("phone");
}
if(userKey!=null) {
autoKey = uri+":"+userKey;
}else {
return ;
}
}else {
autoKey = uri+":"+SessionInterceptor.getCurrentCustomer().getUserId();
}
}else if(SessionInterceptor.getSysUser()!=null){
autoKey = uri+":"+SessionInterceptor.getSysUser().getId();
}
endControlErupt(autoKey,uri);
}
public boolean beginControlErupt(HttpServletRequest request,HttpServletResponse response) {
String uri = request.getRequestURI();
if(uri.contains("/upload")) {
return true;
}
String key = uri;
key = key.replace("//", "/");
String autoKey = "currence";
if(SessionInterceptor.getCurrentCustomer()!=null) {
if(uri.contains("/ptn")) {
String userKey = request.getParameter("openId");
if(userKey == null) {
userKey = request.getParameter("phone");
}
if(userKey!=null) {
autoKey = key+":"+userKey;
}else {
return true;
}
}else {
autoKey = key+":"+SessionInterceptor.getCurrentCustomer().getUserId();
}
}else if(SessionInterceptor.getSysUser()!=null){
autoKey = key+":"+SessionInterceptor.getSysUser().getId();
}
return beginControlErupt(autoKey,uri,response);
}
@Override
public int getOrder() {
return 3;
}
@Override
public String getPathPatterns() {
return "/**";
}
}
网友评论