LanguageAspect.java 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package com.backendsys.aspect;
  2. import jakarta.servlet.http.Cookie;
  3. import jakarta.servlet.http.HttpServletRequest;
  4. import org.aspectj.lang.ProceedingJoinPoint;
  5. import org.aspectj.lang.annotation.Around;
  6. import org.aspectj.lang.annotation.Aspect;
  7. import org.springframework.beans.factory.annotation.Value;
  8. import org.springframework.stereotype.Component;
  9. import org.springframework.web.context.request.RequestContextHolder;
  10. import org.springframework.web.context.request.ServletRequestAttributes;
  11. import java.lang.reflect.Field;
  12. import java.util.Arrays;
  13. /**
  14. * 自动在 控制器 的实体类中加入 { lang: 'zh' } 字段
  15. * (此写法要求控制器传参为实体类)
  16. * (详情接口可能要改变传参方式为实体类)
  17. */
  18. @Aspect
  19. @Component
  20. public class LanguageAspect {
  21. @Value("${DEFAULT_LANGUAGE}")
  22. private String DEFAULT_LANGUAGE;
  23. @Around("@annotation(org.springframework.web.bind.annotation.GetMapping) || @annotation(org.springframework.web.bind.annotation.PostMapping) || @annotation(org.springframework.web.bind.annotation.PutMapping) || @annotation(org.springframework.web.bind.annotation.DeleteMapping)")
  24. // @Around("@annotation(org.springframework.web.bind.annotation.GetMapping)")
  25. public Object injectLanguage(ProceedingJoinPoint joinPoint) throws Throwable {
  26. // 获取 HttpServletRequest 对象
  27. ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  28. HttpServletRequest request = attributes.getRequest();
  29. // 从 Cookie 中获取 lang 值
  30. // String langValue = getCookieValue(request, "lang");
  31. // 从 Headers 中获取 lang 值
  32. String langValue = request.getHeader("lang");
  33. if (langValue == null || langValue.isEmpty()) langValue = DEFAULT_LANGUAGE;
  34. // 获取方法参数
  35. Object[] args = joinPoint.getArgs();
  36. // 遍历方法参数,查找带有lang字段的实体类,并设置lang的值
  37. for (Object arg : args) {
  38. setLangValue(arg, langValue);
  39. }
  40. // 调用原始方法
  41. Object result = joinPoint.proceed(args);
  42. return result;
  43. }
  44. // 通过Cookie名称获取Cookie的值
  45. private String getCookieValue(HttpServletRequest request, String name) {
  46. Cookie[] cookies = request.getCookies();
  47. if (cookies != null) {
  48. for (Cookie cookie : cookies) {
  49. if (name.equals(cookie.getName())) {
  50. return cookie.getValue();
  51. }
  52. }
  53. }
  54. return null;
  55. }
  56. // 使用反射设置对象中的lang值
  57. private void setLangValue(Object object, String langValue) throws IllegalAccessException {
  58. if (object != null) {
  59. Class<?> clazz = object.getClass();
  60. Field[] fields = clazz.getDeclaredFields();
  61. // 遍历对象的所有字段
  62. for (Field field : fields) {
  63. // 判断字段是否为lang,并且为String类型
  64. if (field.getName().equals("lang") && field.getType().equals(String.class)) {
  65. field.setAccessible(true);
  66. field.set(object, langValue);
  67. }
  68. }
  69. }
  70. }
  71. }