【发布时间】:2020-03-17 16:30:17
【问题描述】:
我的安全配置有问题,我需要创建一个正在访问我的应用程序的 Ip 列表。
我的问题是以动态方式为我的所有应用程序请求获取 Ip。 但是我不想为我的应用程序的所有请求添加 HttpServletRequest,我想要的是在每个请求之前调用的方法,甚至在 SecurityConfig 事件之前。
我尝试做的是使用 AuthenticationProvider 获取请求 HttpServletRequest 然后获取我的客户端的 Ip。问题是我找不到创建单个类的方法,它连接到我的所有请求。这是我的代码:
public abstract class IPAddressBasedAuthenticationProvider implements AuthenticationProvider {
/**
* Context http request
*/
@Autowired
private HttpServletRequest request;
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String ipAddress = request.getRemoteAddr();
System.out.println(ipAddress);
return authentication;
}
}
我的想法是我的请求将在此代码中传递,然后显示我的客户 Ip。
我尝试在我的控制器类中自动装配它,但我的应用程序没有执行。 我可以做些什么来完成这项工作?
【问题讨论】:
-
How to extract IP Address in Spring MVC Controller get call? 的可能重复项只需通过添加此参数
HttpServletRequest request在请求映射级别注入 -
尝试使用 Spring AOP 并处理 HttpServletRequest stackoverflow.com/questions/19271807/…
-
我不想改变我的应用程序的每个请求。
标签: java spring spring-boot servlets