ServiceAuthRestInterceptor.java 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. String clientToken = request.getHeader("x-auth-client-" + applicationName);
  41. String client = request.getHeader("x-auth-client");
  42. try {
  43. if (StringUtils.isEmpty(clientToken)) {
  44. String clientName = JwtUtils.getUsername(client);
  45. // authServer校验 客户端是否合法&能否有权限访问
  46. if (Boolean.TRUE.equals(clientAuthProvider.verify(applicationName, clientName.split("\\.")[0], clientName.split("\\.")[1]))) {
  47. // 给该客户端签名
  48. String signToken = JwtUtils.sign(JwtUtils.generateSalt(), "xP3La8IhZjl4fmWXD.AYVH5tor5bn-Rr", 3600*12);
  49. response.addHeader("x-auth-client-response", signToken);
  50. log.info("auth-server verify success, sign with {}", signToken);
  51. return super.preHandle(request, response, handler);
  52. }
  53. } else {
  54. // jwt校验
  55. if (!JwtUtils.isTokenExpired(clientToken) && JwtUtils.verifyToken(clientToken, "xP3La8IhZjl4fmWXD.AYVH5tor5bn-Rr") != null) {
  56. log.info("local service jwt verify success");
  57. return super.preHandle(request, response, handler);
  58. }
  59. }
  60. } catch (Exception e) {
  61. log.error(e.getMessage(), e);
  62. throw new ClientForbiddenException("Client verfy error");
  63. }
  64. throw new ClientForbiddenException("Client is Forbidden!");
  65. }
  66. }