parent
b2496067a7
commit
95740e2349
@ -0,0 +1,16 @@
|
||||
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.internal.common.dto.ResponseResult;
|
||||
import com.mashibing.internal.common.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 resultString = "";
|
||||
String token = request.getHeader("Authorization");
|
||||
|
||||
try {
|
||||
|
||||
JwtUtils.parseToken(token);
|
||||
}catch(SignatureVerificationException e){
|
||||
resultString = "token sign error";
|
||||
result = false;
|
||||
}catch(TokenExpiredException e){
|
||||
resultString = "token time out";
|
||||
result = false;
|
||||
}catch(AlgorithmMismatchException e){
|
||||
resultString = "token AlgorithmMismatchException";
|
||||
result = false;
|
||||
}catch(Exception e){
|
||||
resultString = "token invalid";
|
||||
result = false;
|
||||
}
|
||||
|
||||
if(!result){
|
||||
PrintWriter out = response.getWriter();
|
||||
out.println(JSONObject.fromObject(ResponseResult.fail(resultString)).toString());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,8 @@
|
||||
package com.mashibing.internal.common.constant;
|
||||
|
||||
public class IdentityConstant {
|
||||
//乘客身份
|
||||
public static final String PASSENGER_IDENTITY = "1";
|
||||
//司机身份
|
||||
public static final String DRIVER_IDENTITY = "2";
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.mashibing.internal.common.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class TokenResult {
|
||||
|
||||
private String phone;
|
||||
|
||||
private String identity;
|
||||
}
|
Binary file not shown.
Loading…
Reference in new issue