gpt4 book ai didi

spring - 使用 Spring Security 的在线用户

转载 作者:行者123 更新时间:2023-12-04 20:49:02 25 4
gpt4 key购买 nike

我正在使用 Spring Security,我想知道当前有哪些用户在线。我首先尝试使用 SessionRegistryImpl 的方法和 <session-management session-authentication-strategy-ref="..." ... /> ,但我想这个列表存储在内存中,我想避免它(这将是一个巨大的网站,很多用户同时在线,列表会变得很大)。如果我错了,请纠正我。

我尝试的第二种方法是使用监听器和 HttpSessionListener界面和自定义 AuthenticationManager并将“在线标志”存储在数据库中。基本上,标志在 authenticate(...) 中设置为 true我的身份验证管理器的方法并在 sessionDestroyed(...) 中设置为 false我的 session 监听器的方法。

web.xml:

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<display-name>Test</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/security.xml</param-value>
</context-param>

<!-- Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
<listener-class>my.package.SessionListener</listener-class>
</listener>

<servlet>
<servlet-name>test</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<session-config>
<session-timeout>1</session-timeout>
</session-config>
</web-app>

Spring 安全配置:

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<beans:bean id="authenticationManager" class="my.package.security.AuthenticationManager" />

<http disable-url-rewriting="true" authentication-manager-ref="authenticationManager">
<!--<intercept-url pattern="/" access="ROLE_ANONYMOUS" />-->
<intercept-url pattern="/login*" access="ROLE_ANONYMOUS" />
<intercept-url pattern="/favicon.ico" access="ROLE_ANONYMOUS" />
<intercept-url pattern="/*" access="ROLE_USER" />
<form-login login-processing-url="/authorize" login-page="/login" authentication-failure-url="/login-failed" />
<logout logout-url="/logout" logout-success-url="/login" />
<remember-me data-source-ref="dataSource" />
</http>
</beans:beans>

我的.package.SessionListener:

public class SessionListener implements HttpSessionListener
{
public void sessionCreated(HttpSessionEvent httpSessionEvent)
{

}

public void sessionDestroyed(HttpSessionEvent httpSessionEvent)
{
UserJpaDao userDao = WebApplicationContextUtils.getWebApplicationContext(httpSessionEvent.getSession().getServletContext()).getBean(UserJpaDao.class);

Authentication a = SecurityContextHolder.getContext().getAuthentication();
if(a != null)
{
User loggedInUser = userDao.findByAlias(a.getName());

if(loggedInUser != null)
{
loggedInUser.setOnline(false);
userDao.save(loggedInUser);
}
}
}
}

我的.package.security.AuthenticationManager:

public class AuthenticationManager implements org.springframework.security.authentication.AuthenticationManager
{
@Autowired
UserJpaDao userDao;

public Authentication authenticate(Authentication authentication) throws AuthenticationException
{
User loggedInUser = null;
Collection<? extends GrantedAuthority> grantedAuthorities = null;

...

loggedInUser = userDao.findByAlias(authentication.getName());
if(loggedInUser != null)
{
// Verify password etc.
loggedInUser.setOnline(true);
userDao.save(loggedInUser);
}
else
{
loggedInUser = null;
throw new BadCredentialsException("Unknown username");
}

return new UsernamePasswordAuthenticationToken(loggedInUser, authentication.getCredentials(), grantedAuthorities);
}
}

sessionCreatedsessionDestroyed被正确触发,但是 SecurityContextHolder.getContext().getAuthentication();始终为空。

更新:几乎一切都运行良好。唯一的问题是,当 session 因超时而过期时,SecurityContextHolder.getContext().getAuthentication()sessionDestroyed(...) 中返回 null方法。当手动触发注销时,它工作得很好。

有人可以帮助我吗?非常感谢任何提示。

谢谢

最佳答案

您不能使用 SecurityContextHolder 在您的 SessionListener 中获取 Principal,因为它仅在请求的上下文中有效。

你需要的所有信息都在 session 本身

例子:

@Override
public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session = se.getSession();
SecurityContext context = (SecurityContext)session.getAttribute("SPRING_SECURITY_CONTEXT");
Authentication authentication = context.getAuthentication();
Object principal = authentication.getPrincipal();

// Your code goes here

}

关于spring - 使用 Spring Security 的在线用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9211662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com