有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法在spring security with spring boot中阻止同一用户的多个并发登录

** 在这里,我编辑了我的代码,以查看我目前正在为此目的使用内存中身份验证

这是我的安全配置文件,我想要实现的是一个用户没有并发会话。 使用此代码,我可以在多个选项卡上与同一用户登录。 即使用户有活动会话 有人能帮我吗。我是Spring security的新手 提前谢谢**

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    public void configureGlobalSecurity(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser(username).password(password).roles("OPERATIONAL");
        auth.inMemoryAuthentication().withUser(sysUsername).password(sysPassword).roles("OPERATIONAL");
    }
    
    @Override
    public void configure(WebSecurity web) throws Exception {

        web.ignoring().antMatchers("/initialPage").and().ignoring().antMatchers("/WEB-INF/**").and().ignoring()
                .antMatchers("/sp/processRequest").antMatchers("/WEB-INF/**").and().ignoring()
                .antMatchers("/sp/rest/getChannelCodeOnMID").antMatchers("/createRazorpayOrder").and().ignoring()
                .antMatchers("/createRazorpayOrder").antMatchers("/sp/rest/getNonPreferredEntities").and().ignoring()
                .antMatchers("/sp/rest/getNonPreferredEntities").antMatchers("/sp/rest/getChargesDetailsOnChannel")
                .and().ignoring().antMatchers("/sp/rest/getChargesDetailsOnChannel").antMatchers("/paymentResponse")
                .and().ignoring().antMatchers("/paymentResponse").antMatchers("/RedirectpaymentResponse").and()
                .ignoring().antMatchers("/RedirectpaymentResponse").and().ignoring().antMatchers("/testMerchantPage")
                .and().ignoring().antMatchers("/sp/rest/generateChecksum").and().ignoring().antMatchers("/pushResponse")
                .and().ignoring().antMatchers("/sp/rest/getServiceDetails")
                .and().ignoring().antMatchers("/errorPage")
                .and().ignoring().antMatchers("/getResponse")
                .and().ignoring().antMatchers("/css/**")
                .and().ignoring().antMatchers("/js/**")
                .and().ignoring().antMatchers("/img/qrcode/**")
                .and().ignoring().antMatchers("/sp/rest/getCancelRequest")
                .and().ignoring().antMatchers("/sp/rest/checkTxnStatusOnOrderId")
                .and().ignoring().antMatchers("/pushResponse")
                .and().ignoring().antMatchers("/paymentStatusApi");
        
    }

    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        
        http.csrf().requireCsrfProtectionMatcher(new AntPathRequestMatcher("**/login")).and().authorizeRequests().antMatchers("**/api/v1/getPricingDtls").permitAll()
        .antMatchers("/merchantCreationOrView").hasRole("OPERATIONAL").and().authorizeRequests()
        .antMatchers("/merchantCreation").hasRole("OPERATIONAL")
        .antMatchers("/merchantView").hasRole("OPERATIONAL")
        .antMatchers("/merchantRenderedView").hasRole("OPERATIONAL")
        .antMatchers("/viewMerchantDetails").hasRole("OPERATIONAL")
        .antMatchers("/saveMerchantDetails").hasRole("OPERATIONAL")
        .antMatchers("/download").hasRole("OPERATIONAL")
        .antMatchers("/downloadSettlement").hasRole("OPERATIONAL")
        .and().formLogin().loginPage("/login").
        defaultSuccessUrl("/merchantCreationOrView",true).failureUrl("/login?error").permitAll()
        .and()
          .sessionManagement()
          .sessionCreationPolicy(SessionCreationPolicy.NEVER)
          .maximumSessions(1)
          .maxSessionsPreventsLogin(true).
          sessionRegistry(sessionRegistry())
          ;
    }

    @Bean
    SessionRegistry sessionRegistry() {         
        return new SessionRegistryImpl();
    }
    @Bean
    public static ServletListenerRegistrationBean httpSessionEventPublisher() { //(5)
        return new ServletListenerRegistrationBean(new HttpSessionEventPublisher());
    }
    
    
    
    @Bean
    public CustomBasicAuthenticationEntryPoint getBasicAuthenticationEntryPoint() {
        
        return new CustomBasicAuthenticationEntryPoint();
    }
    

    

}



共 (1) 个答案

  1. # 1 楼答案

    简单的检查可以帮助您在这里找到类似的question

    很可能您没有在登录的User类中实现/重写正确的hashCodeequals方法

    您必须这样做,这取决于您在逻辑上如何确定所谓的“相同”用户:

     public class User implements UserDetails, Serializable {
        private static final long serialVersionUID = 1L;
    
        // ...
    
        @Override
        public boolean equals(Object obj) {
            if (obj instanceof User) {
              return username.equals( ((User) obj).getUsername() );
            }
            return false;
        }
    
        @Override
        public int hashCode() {
            return username != null ? username.hashCode() : 0;
        }
    }