72.安心技术梳理 - 前置校验,拦截器,切面编程思想2,干货

拦截器实现切面编程,实现仅供参考

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);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值