AOP拦截器

登录校验注解,controller方法上加注解后不验证是否登录

package com.luding.asset.common.anno;

/**
 * @Description [ 不需要登陆 ]
 * @Date 2020/7/21 11:07
 * @Author zsj
 */
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface NoNeedLogin {
}

权限校验注解,controller上加该方法不需要校验是否有权访问该接口

package com.luding.asset.common.anno;

/**
 * @Description [ 不需要权限验证 ]
 * @Date 2020/7/21 11:08
 * @Author zsj
 */
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface NoValidPrivilege {

}

 

package com.ahies.zgstm.interceptor;

import com.ahies.zgstm.common.anno.NoNeedLogin;
import com.ahies.zgstm.common.anno.NoValidPrivilege;
import com.ahies.zgstm.util.ExecutionContext;
import com.ahies.zgstm.util.PmsStringUtils;
import com.ahies.zgstm.util.ResponseResult;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @Description [ 登录拦截器 ]
 * @Date 2020/7/21 11:15
 * @Author zsj
 */
@Component
public class PmstAuthenticationInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //跨域设置
        this.crossDomainConfig(response);
        boolean isHandlerMethod = handler instanceof HandlerMethod;
        if (!isHandlerMethod) {
            return true;
        }

        //不需要登录的注解
        Boolean isNoNeedLogin = ((HandlerMethod) handler).getMethodAnnotation(NoNeedLogin.class) != null;
        if (isNoNeedLogin) {
            return true;
        }


//        //需要做token校验, 消息头的token优先于请求query参数的token
//        String xHeaderToken = request.getHeader(TOKEN_NAME);
//        String xRequestToken = request.getParameter(TOKEN_NAME);
//        String xAccessToken = null != xHeaderToken ? xHeaderToken : xRequestToken;
//        if (null == xAccessToken) {
//            this.outputResult(response, LoginResponseCodeConst.LOGIN_ERROR);
//            return false;
//        }
//
//        //根据token获取登录用户
//        RequestTokenBO requestToken = loginTokenService.getEmployeeTokenInfo(xAccessToken);
//        if (null == requestToken) {
//            this.outputResult(response, LoginResponseCodeConst.LOGIN_ERROR);
//            return false;
//        }

        //判断接口权限
        String methodName = ((HandlerMethod) handler).getMethod().getName();
        String className = ((HandlerMethod) handler).getBeanType().getName();
        List<String> list = PmsStringUtils.splitConvertToList(className, "\\.");
        String controllerName = list.get(list.size() - 1);
        Method m = ((HandlerMethod) handler).getMethod();
        Class<?> cls = ((HandlerMethod) handler).getBeanType();
        boolean isClzAnnotation = cls.isAnnotationPresent(NoValidPrivilege.class);
        boolean isMethodAnnotation = m.isAnnotationPresent(NoValidPrivilege.class);
        NoValidPrivilege noValidPrivilege = null;
        if (isClzAnnotation) {
            noValidPrivilege = cls.getAnnotation(NoValidPrivilege.class);
        } else if (isMethodAnnotation) {
            noValidPrivilege = m.getAnnotation(NoValidPrivilege.class);
        }
        //不需验证权限
        if (noValidPrivilege != null) {

            return true;
        }
//        //需要验证权限
//        Boolean privilegeValidPass = privilegeEmployeeService.checkEmployeeHavePrivilege(requestToken, controllerName, methodName);
//        if (! privilegeValidPass) {
//            this.outputResult(response, LoginResponseCodeConst.NOT_HAVE_PRIVILEGES);
//            return false;
//        }
//        ExecutionContext.setContextMap();
        return true;
    }

    /**
     * 配置跨域
     *
     * @param response
     */
    private void crossDomainConfig(HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE, PATCH");
        response.setHeader("Access-Control-Expose-Headers", "*");
        response.setHeader("Access-Control-Allow-Headers", "Authentication,Origin, X-Requested-With, Content-Type, " + "Accept, x-access-token");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Expires ", "-1");
    }

    /**
     * 错误输出
     *
     * @param response
     * @param
     * @throws IOException
     */
    private void outputResult(HttpServletResponse response, String message) throws IOException {
        ResponseResult<Object> wrap = new ResponseResult<>();
        wrap.setStatus(1001);
        wrap.setMessage(message);
        String msg = JSONObject.toJSONString(wrap);
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().write(msg);
        response.flushBuffer();
    }
}

配置拦截器

package com.ahies.zgstm.config;

import com.ahies.zgstm.interceptor.PmstAuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Description TODO
 * @Date 2020/7/21 12:18
 * @Author zsj
 */
@Configuration
public class PmsWebAppConfig implements WebMvcConfigurer {

    @Autowired
     PmstAuthenticationInterceptor pmstAuthenticationInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(pmstAuthenticationInterceptor).addPathPatterns("/**")
        .excludePathPatterns("/druid");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

非ban必选

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值