TranslationInterceptor.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // package com.backendsys.config.Interceptor;
  2. // import com.backendsys.config.Security.utils.TokenUtil;
  3. // import jakarta.servlet.http.Cookie;
  4. // import jakarta.servlet.http.HttpServletRequest;
  5. // import jakarta.servlet.http.HttpServletResponse;
  6. // import org.springframework.beans.factory.annotation.Autowired;
  7. // import org.springframework.beans.factory.annotation.Value;
  8. // import org.springframework.data.redis.core.StringRedisTemplate;
  9. // import org.springframework.stereotype.Component;
  10. // import org.springframework.web.servlet.HandlerInterceptor;
  11. // import java.util.concurrent.TimeUnit;
  12. // @Component
  13. // public class TranslationInterceptor implements HandlerInterceptor {
  14. // @Autowired
  15. // private TokenUtil tokenService;
  16. // @Autowired
  17. // private StringRedisTemplate stringRedisTemplate;
  18. // @Value("${TOKEN_DURATION_MEMBER}")
  19. // private Long TOKEN_DURATION_MEMBER;
  20. // @Value("${DEFAULT_LANGUAGE}")
  21. // private String DEFAULT_LANGUAGE;
  22. // @Override
  23. // public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
  24. // // 获取 Cookie 中的 lang 值
  25. // String lang = getLangFromCookie(request.getCookies());
  26. // // 将 lang 存入线程本地变量,以便后续使用
  27. // // LocaleContextHolder.setLocale(new Locale(lang));
  28. // // request.setAttribute("lang", lang);
  29. // // 将 lang 存入 Redis
  30. // String langRedisKey = "lang:" + tokenService.getLoginUUID();
  31. // System.out.println("langRedisKey = " + langRedisKey);
  32. // stringRedisTemplate.opsForValue().set(langRedisKey, lang, TOKEN_DURATION_MEMBER, TimeUnit.MILLISECONDS);
  33. // return true;
  34. // }
  35. // // 获取 Cookie 中的 lang 值
  36. // private String getLangFromCookie(Cookie[] cookies) {
  37. // if (cookies != null) {
  38. // for (Cookie cookie : cookies) {
  39. // if ("lang".equals(cookie.getName())) {
  40. // return cookie.getValue();
  41. // }
  42. // }
  43. // }
  44. // return DEFAULT_LANGUAGE; // 默认语言 (application.yml - DEFAULT_LANGUAGE)
  45. // }
  46. // }