如何避免Spring Boot应用中每个用户的并发会话

huangapple go评论80阅读模式
英文:

How to avoid concurrent sessions per user in a spring-boot application

问题

Sure, here's the translated content:

一个使用Spring Boot创建的应用程序具有REST API,具有生成JWT令牌的登录API,但用户可以多次登录。如何避免这种情况?

我尝试了以下配置,但没有成功:

protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .antMatchers("/admin/**").hasRole("ADMIN")
            .antMatchers("/user/**").hasAnyRole("USER", "ADMIN")
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .and()
        .logout()
            .logoutSuccessUrl("/login?logout")
            .and()
        .sessionManagement()
            .maximumSessions(1)
            .maxSessionsPreventsLogin(true)
            .expiredUrl("/login?expired")
            .and()
        .sessionFixation()
            .migrateSession();
}
英文:

a spring-boot application with REST APIs has Login API generates token using JWT
but a user can login multiple times
how can I avoid this scenario?

I tried the following configurations with no luck

    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .antMatchers("/user/**").hasAnyRole("USER", "ADMIN")
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .and()
            .logout()
                .logoutSuccessUrl("/login?logout")
                .and()
            .sessionManagement()
                .maximumSessions(1)
                .maxSessionsPreventsLogin(true)
                .expiredUrl("/login?expired")
                .and()
            .sessionFixation()
                .migrateSession();
    }

答案1

得分: 0

文档 提到您需要发布以下的 bean:

@Bean
public HttpSessionEventPublisher httpSessionEventPublisher() {
    return new HttpSessionEventPublisher();
}

您尝试过这样吗?

英文:

The documentation says that you have to publish the following bean:

@Bean
public HttpSessionEventPublisher httpSessionEventPublisher() {
    return new HttpSessionEventPublisher();
}

Have you tried that?

huangapple
  • 本文由 发表于 2023年5月10日 19:10:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定