Hi there, I was wondering if it were possibile to get the ip address in the beforeloginevent ? Is there any mechanism to get the httprequest object ?
Hi,
- Anywhere in the code of the web or portal modules you can make use of the standard Spring class holding the original ServletHttpRequest object -
RequestContextHolder.currentRequestAttributes()
see java - How to extract IP Address in Spring MVC Controller get call? - Stack Overflow
see how it is used in the platform code: com.haulmont.cuba.portal.security.PortalAuthenticationProvider#authenticate
Another wrapper class holding the same information is com.haulmont.cuba.web.sys.RequestContext#getRequest, can be used in web module.
- If you mean BeforeLoginEvent of the core module - then you should cast
beforeLoginEvent.getCredentials()
toAbstractClientCredentials
and take ipAddress fromAbstractClientCredentials#ipAddress
.
See usage example in the com.haulmont.cuba.security.auth.checks.IpMaskUserAccessChecker#check class:
if (credentials instanceof AbstractClientCredentials) {
AbstractClientCredentials clientCredentials = (AbstractClientCredentials) credentials;
...
String ipAddress = clientCredentials.getIpAddress();
Thanks, perfect.