Tôi nghĩ rằng một trong những giải pháp tôi có thể nghĩ ra được cách sử dụng một HttpSessionListener, Nếu bạn thực hiện một người biết lắng nghe phiên bạn có thể nắm bắt được thời gian và khi một phiên người dùng mới được tạo ra và bị phá hủy, Bạn có thể tận dụng mùa xuân giữ bối cảnh an ninh của bạn để có được một tổ chức của các uniquename/userid của người dùng đăng nhập
tôi đang nghĩ đến một số điều như thế này
public class SesssionListenerImpl implements HttpSessionListener
{
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent)
{
String uniqueName = SecurityContextHolder.getContext().getAuthentication().getName();
String sessionId = httpSessionEvent.getSession().getId();
long beginTimeInSeconds = System.currentTimeMillis()/1000;
//create a record and persist to data base with sessionId, uniquename, sessionBeginTime
}
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent)
{
SecurityContextHolder.getContext().getAuthentication().getPrincipal();
httpSessionEvent.getSession().getId();
long endTime = System.currentTimeMillis()/1000;
//load the record based on sessionId
//update the record with sessionEndTime
}
}
đó được cho biết có rất ít xuống bên để t cách tiếp cận của mình, Nếu phiên HTTP không bao giờ bị vô hiệu thì bạn sẽ kết thúc với một số phiên không có thời gian kết thúc.
- Nếu bạn nhẹ nhàng có thể thúc giục cơ sở người dùng của bạn để đăng xuất tất cả các thời gian như thực hành tốt (mặc dù không phải là một giải pháp thực tế)
- Bạn có thể làm có thể cơ thể vào tải và kiểm tra nếu người dùng đang rời khỏi tên miền của bạn hoặc sử dụng các cửa sổ nút đóng để đóng cửa sổ và bắn một vô hiệu phiên để nắm bắt thời gian kết thúc
CẬP NHẬT
Bạn một Tôi nghĩ bạn có thể sử dụng cơ chế sự kiện ứng dụng mùa xuân, để làm điều đó thêm vào web.xml của bạn, Trình nghe này xuất bản các sự kiện phiên HTTP khác khôn ngoan, bạn sẽ không thể truy cập các sự kiện phiên ngay cả khi bạn triển khai ApplicationListener
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
Bây giờ thêm một lớp mới mà thực hiện ApplicationListener
@Service
public class ApplicationSecurityListener implements ApplicationListener<ApplicationEvent>
{
@Override
public void onApplicationEvent(ApplicationEvent event)
{
if (event instanceof AuthorizationFailureEvent)
{
AuthorizationFailureEvent authorizationFailureEvent = (AuthorizationFailureEvent) event;
System.out.println ("not authorized:" + authorizationFailureEvent);
}
else if (event instanceof AuthenticationFailureBadCredentialsEvent)
{
AuthenticationFailureBadCredentialsEvent badCredentialsEvent = (AuthenticationFailureBadCredentialsEvent) event;
System.out.println ("badCredentials:" + badCredentialsEvent);
}
//login success event
else if (event instanceof AuthenticationSuccessEvent)
{
AuthenticationSuccessEvent authenticationSuccessEvent = (AuthenticationSuccessEvent) event;
//this will provide user id and password but no session, get source has all the user information in security context
System.out.println ("AuthenticationSuccessEvent:" + authenticationSuccessEvent.getSource());
}
//this event will published if you add the HttpSessionEventPublisher to web.xml
else if (event instanceof SessionDestroyedEvent)
{
SessionDestroyedEvent sessinEvent = (SessionDestroyedEvent) event;
System.out.println ("SessionDestroyedEvent:" + sessinEvent.getId());
//load session if it is not empty
if(sessinEvent.getSecurityContext() != null)
{
System.out.println ("SessionDestroyedEvent:" + sessinEvent.getSecurityContext().getAuthentication().getName());
//update the session with endTime
}
}
else
{
//System.out.println ("undefined: " + event.getClass().getName());
}
}
}
có một sự kiện nếu bạn muốn chụp logout bởi chính nó, bạn có thể thực hiện LogoutHandler cung cấp cho bạn truy cập vào logut sự kiện.
Nguồn
2011-12-19 15:07:12
Dường như Bộ lọc bảo mật mùa xuân cho phép tôi đăng ký bất cứ khi nào người dùng đăng nhập và đăng xuất.Giải pháp sẽ tồn tại trong cơ sở dữ liệu (như bạn đã nói) userID, thời gian đăng nhập và thời gian đăng xuất, sử dụng các bộ lọc bảo mật! –