登录校验注解,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");
}
}