拦截器实现切面编程,实现仅供参考
1、拦截器管理类:
import com.RiskInterceptor;
import com.UserInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.annotation.Resource;
@Configuration
public class InterceptorConfiguration implements WebMvcConfigurer {
//风控拦截方法
@Resource
private RiskInterceptor riskInterceptor;
//用户状态拦截器方法
@Resource
private UserInterceptor userInterceptor;
@Bean
public WebMvcConfigurer webMvcConfigurer() {
return new WebMvcConfigurer() {
/**
* 添加拦截器
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(riskInterceptor)
.addPathPatterns("/cocoon/*")//拦截的请求路径
.excludePathPatterns("*.icon")//不拦截的请求路径
.excludePathPatterns("/cocoon/nest/**")//不拦截的请求路径
;
registry.addInterceptor(userInterceptor)
.addPathPatterns("/cocoon/*")
;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
}
};
}
}
2、拦截方法2:
import com.MtiResult;
import com.MtiLoginUtil;
import com.RiskService;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class RiskInterceptor extends BaseInterceptor implements HandlerInterceptor {
@Resource
private RiskService riskService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) {
String pin = MtiLoginUtil.getPin(request);
if (pin == null) { return true; }
try {
boolean riskStatus = riskService.get(pin);
if (!riskStatus) {
return true;
}
return false;
} catch (Exception e) {
Log.error("用户pin:" + pin + ",风控异常", e);
}
return true;
}
}
3、拦截方法3:
import com.MtiResult;
import com.MtiLoginUtil;
import com.UserService;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class UserInterceptor extends BaseInterceptor implements HandlerInterceptor {
@Resource
private UserService userService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) {
String pin = MtiLoginUtil.getPin(request);
if (pin == null) { return true; }
try {
int userType = userService.get(pin);//用户身份
if (1==userType) {//是否新人
return true;
}
return false;
} catch (Exception e) {
Log.error("用户pin:" + pin + ",风控异常", e);
}
return true;
}
}
4、拦截方法公共类:
import com.MtiResult;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public abstract class BaseInterceptor {
protected void writeResponse(MtiResult result, HttpServletResponse response) {
try {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print(JSONObject.toJSONString(result));
out.flush();
out.close();
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}