我的带有 Spring Web MVC 的 Spring Boot 2.2.0 应用程序在反向代理后面运行。Spring 如何正确处理X-Forwarded-{Prefix,Host,Proto}-headers 以识别向服务器发出的实际请求?
我知道围绕控制器方法编写AOP建议的标准方法,并且如果在控制器方法中声明,则可以访问HttpServletRequest arg.
但我的情况是我有一个翻译服务,目前是会话范围的维护用户的翻译区域设置.我觉得这使得服务状态有限,而且我不希望它是会话范围的,因为我认为它确实是Singleton.但是有多个地方调用了翻译服务方法,因此我不想更改签名以在这些方法中添加请求/语言环境.问题是翻译服务方法的所有调用者都无法访问HttpServletRequest(不是控制器方法)?我是否可以围绕翻译服务方法编写一个方面,并以某种方式神奇地访问HttpServletRequest,无论它是否在调用者的上下文中可用?
@Service
public class TranslationService {
public void translate(String key) {
...
}
}
@Aspect
@Component
public class LocaleFinder {
@PointCut("execution(* TranslationService.translate(..))")
private void fetchLocale() {}
@Around("fetchLocale()") // in parameter list
public void advice(JoinPoint joinpoint, HttpServletRequest request) { .... }
}
Run Code Online (Sandbox Code Playgroud)
如果现在,translate的调用者没有HttpServletRequest,我不能在建议中得到请求吗?有解决方法吗?