飞滴出行网约车2022-乘客服务 拦截器注册与token访问拦截

master
yh 3 years ago
parent acae4b1aa7
commit aba6820dac

@ -0,0 +1,18 @@
package com.mashibing.apipassenger.interceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new JwtInterceptor())
// 拦截所有路径
.addPathPatterns("/**")
// 部份取消拦截路径
.excludePathPatterns(("/noauthTest"));
}
}

@ -0,0 +1,47 @@
package com.mashibing.apipassenger.interceptor;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.internalcommon.util.JwtUtils;
import net.sf.json.JSONObject;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public class JwtInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
boolean result = true;
String resutltString = "" ;
String token = request.getHeader ( "Authorization") ;
try {
JwtUtils.parseToken(token);
}catch (SignatureVerificationException e){
resutltString="token sign error";
result=false;
}catch (TokenExpiredException e){
resutltString="token time out";
result = false;
}catch (AlgorithmMismatchException e){
resutltString="token AlgorithmMismatchException";
result=false;
}catch (Exception e) {
resutltString = "token invalid";
result = false;
}
if (!result){
PrintWriter out = response.getWriter();
out.print(JSONObject.fromObject( ResponseResult.fail(resutltString) ).toString());
}
return result;
}
}

@ -51,7 +51,7 @@ public class ResponseResult<T> {
* @return
*/
public static <T> ResponseResult fail(T data){
return new ResponseResult().setData(data);
return new ResponseResult().setCode(CommonStatusEnum.FAIL.getCode()).setMessage(CommonStatusEnum.FAIL.getValue()).setData(data);
}
}

Loading…
Cancel
Save