SecurityContextHolder可以获取登录的用户,此对象中存储了用户的认证信息。
例如获取登录用户名:
String username=(String)
SecurityContextHolder.getContext().getAuthentication().getPrincipal();
RequestContextHolder可以获取请求对象,此对象是当前线程中请求对象的持有者。
例如获取IP地址:
ServletRequestAttributes requestAttributes =
(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
String ip=requestAttributes.getRequest().getRemoteAddr();