Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 控制单个用户的并发会话数_Java_Spring_Session_Authentication_Spring Security - Fatal编程技术网

Java 控制单个用户的并发会话数

Java 控制单个用户的并发会话数,java,spring,session,authentication,spring-security,Java,Spring,Session,Authentication,Spring Security,我一直在阅读如何控制单个用户并发会话的数量。到目前为止,我得到的是: @Configuration @EnableWebSecurity public class SecurityAdapter extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { ExpressionUr

我一直在阅读如何控制单个用户并发会话的数量。到目前为止,我得到的是:

@Configuration
@EnableWebSecurity
public class SecurityAdapter extends WebSecurityConfigurerAdapter
{
    @Override
    protected void configure(HttpSecurity http)
        throws Exception
    {
        ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry authorizeRequests = http.authorizeRequests();

        authorizeRequests.antMatchers(HttpMethod.OPTIONS, "**").permitAll();
        authorizeRequests.antMatchers("/logon").permitAll();
        authorizeRequests.anyRequest().authenticated();

        http
            .csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());

        http.httpBasic();

        http.sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.NEVER);

        http.sessionManagement().maximumSessions(1);
        http.sessionManagement().sessionFixation().migrateSession();
    }

    @Bean
    public HttpSessionEventPublisher httpSessionEventPublisher()
    {
        return new HttpSessionEventPublisher();
    }
}
@配置
@启用Web安全性
公共类SecurityAdapter扩展了WebSecurity配置适配器
{
@凌驾
受保护的无效配置(HttpSecurity http)
抛出异常
{
ExpressionUrlAuthorizationConfigurer.ExpressionInterceptUrlRegistry AuthorizationRequests=http.authorizeRequests();
authorizeRequests.antMatchers(HttpMethod.OPTIONS,“**”).permitAll();
authorizeRequests.antMatchers(“/logon”).permitAll();
authorizeRequests.anyRequest().authorized();
http
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
http.httpBasic();
http.sessionManagement()
.sessionCreationPolicy(sessionCreationPolicy.NEVER);
http.sessionManagement().maximumSessions(1);
http.sessionManagement().sessionFixation().migrateSession();
}
@豆子
公共HttpSessionEventPublisher HttpSessionEventPublisher()
{
返回新的HttpSessionEventPublisher();
}
}

为了测试它,我打开了两个浏览器(FireFox和Chrome),并尝试一个接一个地登录它们。我希望一旦第二个登录,第一个就不能再使用这些服务了。但是第一个仍然登录,其会话ID仍然有效。有人能帮我找到问题吗?

因为我不想让Spring创建我的会话(即当用户使用Http基本身份验证进行身份验证时)。我将在
/logon
API中手动创建会话。换句话说,我确实使用会话,但只有在我验证它们时才使用。我不确定我是否遵循了您的要求。我有一些API需要有效的会话ID或Http基本身份验证才能工作。我现在用会话ID给他们打电话。我希望第一个浏览器的会话ID在第二个浏览器登录后无效。但是,在第二个身份验证之后,第一个仍然可以使用服务器。我绝对确信这一点。我以前一直在追那个鬼,所以我知道你在说什么。我可以肯定地确认我的请求通过了,因为它们带有会话ID。@dur肯定。但让我纠正你一点。第二个请求应该会破坏第一个会话。当第三个请求进入时,没有与其携带的ID匹配的会话。对我来说,“注销”是一个请求,要求服务器销毁某些会话,提供要销毁的会话ID。话虽如此,第三个请求不起作用。我有理由这么认为。会话被销毁是一个基于您可以做的事情(释放资源、制作日志或其他任何事情)的操作。此操作应在第二个请求传入时发生。否则,第三个请求可能永远不会出现,这意味着操作永远不会发生,这意味着现在您有两个会话供一个用户使用。悖论。然而,这不是q所问的