首先是攔截類AuthInterceptor.java中的具體邏輯,主要是有繼承BaseInterceptor.java 類,而這個(gè)BaseInterceptor.java類繼承了HandlerInterceptorAdapter類,然后攔截類AuthInterceptor.java實(shí)現(xiàn)了其中的preHandle方法
以下是全部代碼
攔截器主要邏輯類——AuthInterceptor.java
package com.magnet.movie.rest.interceptor;
import com.alibaba.dubbo.config.annotation.Reference;
import com.magnet.movie.common.entity.User;
import com.magnet.movie.common.vo.MessageKey;
import com.magnet.movie.data.api.UserService;
import com.magnet.movie.rest.annotation.DisableAuth;
import com.magnet.movie.rest.utils.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Slf4j
@Component
public class AuthInterceptor extends BaseInterceptor {
@Reference
private UserService userService;
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
if (!(handler instanceof HandlerMethod)) {
return true;
}
//放行邏輯
HandlerMethod method = (HandlerMethod) handler;
DisableAuth auth = method.getMethod().getAnnotation(DisableAuth.class);
if (isDisableAuth(auth)) {
return super.preHandle(request, response, handler);
}
//獲取token
String accessToken = getAuthToken(request);
if (StringUtils.isBlank(accessToken)) {
setResponse(request, response, MessageKey.ACCESS_TOKEN_IS_NULL_CODE,"Error: token is null");
return false;
}
// 3.查詢token是否正確
User account = userService.findAccessTokenIsValid(accessToken);
if (account == null) {
setResponse(request, response, MessageKey.ACCESS_TOKEN_IS_INVALID_CODE,"Error: token is invalid");
return false;
}
// 將userId寫入到request請(qǐng)求中
request.setAttribute(Constants.REQUEST_ATTR_USER_ID, account.getId());
request.setAttribute("UserToken", accessToken);
request.setAttribute("UserId", account.getId());
return true;
}
private static boolean isDisableAuth(DisableAuth auth) {
return auth != null;
}
/**
* 獲取http請(qǐng)求頭部或者參數(shù)中的token值
*
* @param request
* http請(qǐng)求傳遞的值
* @return 返回token
*/
private String getAuthToken(HttpServletRequest request) {
String token = request.getHeader("accessToken");
if (token == null) {
token = request.getParameter("accessToken");
}
return token;
}
}
父類BaseInterceptor.java 這個(gè)類里面的代碼其實(shí)與我要做的token校驗(yàn)關(guān)系不大
package com.magnet.movie.rest.interceptor;
import java.io.IOException;
import java.io.Writer;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.alibaba.fastjson.JSON;
@Slf4j
public abstract class BaseInterceptor extends HandlerInterceptorAdapter {
public void setResponse(HttpServletRequest request,
HttpServletResponse response, String messageKey,String message) {
response.setContentType("application/json;charset=UTF-8");
try (Writer writer = response.getWriter()) {
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("code", messageKey);
resultMap.put("message", message);
logger(request, resultMap);
JSON.writeJSONString(writer, resultMap);
writer.flush();
} catch (IOException e) {
log.error("respose 設(shè)置操作異常:" + e);
}
}
public void setResponse(HttpServletRequest request,
HttpServletResponse response, String messageKey) {
setResponse(request,response,messageKey,"OK");
}
/**
* 記錄日志
*/
private void logger(HttpServletRequest request, Map<String, Object> resultMap) {
StringBuffer msg = new StringBuffer();
msg.append("異常攔截日志:");
msg.append("[uri:").append(request.getRequestURI()).append("]");
Enumeration<String> enumer = request.getParameterNames();
while (enumer.hasMoreElements()) {
String name = enumer.nextElement();
String[] values = request.getParameterValues(name);
msg.append("[").append(name).append("=");
if (values != null) {
int i = 0;
for (String value : values) {
i++;
msg.append(value);
if (i < values.length) {
msg.append("|");
}
}
}
msg.append("]");
}
msg.append(JSON.toJSONString(resultMap));
log.warn(msg.toString());
}
}
回到攔截器邏輯類中會(huì)發(fā)現(xiàn)我重寫了preHandle方法
這個(gè)方法的作用:
原文鏈接 : https://blog.csdn.net/azhegps/article/details/99342891
這樣我們所有的請(qǐng)求都會(huì)經(jīng)過(guò)攔截器的邏輯戳粒,如果請(qǐng)求不符合邏輯就會(huì)被攔截到悔叽,并不會(huì)執(zhí)行到方法內(nèi)部爽篷。
有的接口需要token校驗(yàn)宵蕉,有的并不需要酝静,所以還要一個(gè)放行的邏輯。
可以直接用自定義注解來(lái)實(shí)現(xiàn)
自定義注解@DisableAuth 代碼
package com.magnet.movie.rest.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 非鑒權(quán)注解羡玛,Controller使用此注解,過(guò)濾器將不攔截
* @author hudahua
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
@Inherited
public @interface DisableAuth {
}
上面四個(gè)注解就是java的四大元注解宗苍,具體的用法如下(出自百度):
原文鏈接 : https://blog.csdn.net/qq_26125865/article/details/82881481
在需要放行的方法上加上自定義注解@DisableAuth