I need to find those user who are logged in our application.
We are using Spring Security and there must be a way to find out users' IPs.
I think these information are stored in their sessions. In Spring Security, the current sessions are stored in SessionRegistry. From this class I can have list of authenticated users and some session information. (Using getAllPrincipals
, getAllSessions
and getSessionInformation
)
The question is, how can I have access to current users' IPs? Consider we have to give service to a known region only.
The SessionInformation is not much help as it does not contain much information.
相关问题
- java.lang.IllegalArgumentException: Cannot set to
- Spring Data MongoDB - lazy access to some fields
- Declaring an explict object dependency in Spring
- Decoding body parameters with Spring
- Spring Integration - Inbound file endpoint. How to
相关文章
- java JDK动态代理和cglib动态代理最后获取的代理对象都为null的问题
- org.xml.sax.SAXParseException; lineNumber: 7; colu
- SpringMVC如何把File封装到Map中?
- Page指令 的EnableSessionState="ReadOnly",怎么在web.confi
- Spring: controller inheritance using @Controller a
- How to load @Configuration classes from separate J
- Java spring framework - how to set content type?
- Java/Spring MVC: provide request context to child
I think that the check be achieved by using hasIpAddress http expression
See section 15.2 Web Security Expressions
If you want more flexibility, you can implement your own IP address check service, based on IpAddressMatcher:
bean implementation:
update: you can try to get current user IP this way:
update The information about the relation between IP addresses and sessions can only be gathered from the different sources(like listening to AuthenticationSuccessEvent and SessionDestroyedEvent events, implementing a filter or using an AOP interceptor). Spring Security doesn't store such information because it's useless, as IP address has some meaning only while the server is processing a ServletRequest.
IP address may change(user may be using a proxy), so we can only audit different kinds of events like logging in with some credentials, accessing a service from a different IP, or doing some suspicious activity.
You can use HttpServletRequest for getting user's IP address. (Developers of SpringSecurity do this in the same way in their expression hasIpAddress(...) that is placed in WebSecurityExpressionRoot class).
For example you can get HttpServletRequest in 2 ways:
1) Using RequestContextHolder:
2) Using autowiring:
I took this from here.
Then using HttpServletRequest you can get Ip address in such way:
And here how addresses are compared in spring security:
And IpAddressMatcher class:
EDIT:
According to related questions here and here you can add user's IP to the session using custom filter. And then get this information from session related to the user where it will be necessary. For example you can put user's IP info like this:
You can get IP address from WebAuthenticationDetails object, which can be obtained from Authentication instance.