ServiceAuthRestInterceptor.java 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package com.yaozhitech.spring5.intercept;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.http.HttpServletResponse;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.util.StringUtils;
  7. import org.springframework.web.method.HandlerMethod;
  8. import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
  9. import com.yaozhitech.spring5.annotation.IgnoreClientToken;
  10. import com.yaozhitech.spring5.common.exception.auth.ClientForbiddenException;
  11. import com.yaozhitech.spring5.provider.AuthServerProvider;
  12. import com.yaozhitech.spring5.utils.JwtUtils;
  13. import lombok.extern.slf4j.Slf4j;
  14. /**
  15. * 微服务之间的认证
  16. * @author EDZ
  17. *
  18. */
  19. @Slf4j
  20. public class ServiceAuthRestInterceptor extends HandlerInterceptorAdapter {
  21. @Autowired
  22. private AuthServerProvider clientAuthProvider;
  23. @Value("${spring.application.name}")
  24. private String applicationName;
  25. @Override
  26. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  27. HandlerMethod handlerMethod = (HandlerMethod) handler;
  28. // ip
  29. // if (ClientUtil.isLocalhost(request)) {
  30. // return super.preHandle(request, response, handler);
  31. // }
  32. // 配置该注解,说明不进行服务拦截
  33. IgnoreClientToken annotation = handlerMethod.getBeanType().getAnnotation(IgnoreClientToken.class);
  34. if (annotation == null) {
  35. annotation = handlerMethod.getMethodAnnotation(IgnoreClientToken.class);
  36. }
  37. if (annotation != null) {
  38. return super.preHandle(request, response, handler);
  39. }
  40. if (request.getServletPath().startsWith("/actuator")) {
  41. return super.preHandle(request, response, handler);
  42. }
  43. String clientToken = request.getHeader("x-auth-client-" + applicationName);
  44. String client = request.getHeader("x-auth-client");
  45. try {
  46. if (StringUtils.isEmpty(clientToken)) {
  47. String clientName = JwtUtils.getUsername(client);
  48. // authServer校验 客户端是否合法&能否有权限访问
  49. if (Boolean.TRUE.equals(clientAuthProvider.verify(applicationName, clientName.split("\\.")[0],
  50. clientName.split("\\.")[1]))) {
  51. // 给该客户端签名
  52. String signToken = JwtUtils.sign(JwtUtils.generateSalt(), "xP3La8IhZjl4fmWXD.AYVH5tor5bn-Rr",
  53. 3600 * 12);
  54. response.addHeader("x-auth-client-response", signToken);
  55. log.info("auth-server verify success, sign with {}", signToken);
  56. return super.preHandle(request, response, handler);
  57. }
  58. } else {
  59. // jwt校验
  60. if (!JwtUtils.isTokenExpired(clientToken)
  61. && JwtUtils.verifyToken(clientToken, "xP3La8IhZjl4fmWXD.AYVH5tor5bn-Rr") != null) {
  62. log.info("local service jwt verify success");
  63. return super.preHandle(request, response, handler);
  64. }
  65. }
  66. } catch (Exception e) {
  67. log.error(e.getMessage(), e);
  68. throw new ClientForbiddenException("Client verfy error");
  69. }
  70. throw new ClientForbiddenException("Client is Forbidden!");
  71. }
  72. }