java - 使用spring boot无法防止spring security中同一用户的多个并发登录
问题描述
** 在这里我编辑了我的代码,以查看我目前正在为此目的使用内存身份验证
这是我的安全配置文件,我想要实现的是一个用户没有并发会话。使用此代码,我可以在多个选项卡上使用同一用户登录。即使用户有一个活动会话,任何人都可以帮我解决这个问题。我是 Spring 安全部门的新手 提前致谢**
@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();
}
}
解决方案
简单的检查可以帮助您在这里找到类似的问题。
很可能您没有在登录类中实现/覆盖正确的hashCode
和equals
方法。User
你必须做这样的事情,取决于你如何合理地确定所谓的“相同”用户:
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;
}
}
推荐阅读
- apache-spark - 为什么我们需要 hadoop 或 java 在集群模式下进行 spark-submit (k8s Spark CRD)
- r - 使用 jtools 包绘制带有偏移量的模型预测
- github - 使用 GitHub Actions 容器映像构建的输出来提供标签值
- javascript - 如何使用 Vuejs 设置变量
- heroku - 无法在 Login.interactive 处读取未定义的属性“id”
- reactjs - 触发具有 React 状态的 aria-live 区域
- javascript - 如何遍历多级 JSON 对象并从其中一个键和值创建一个新的 Javascript 对象
- python - 如何在 Python 的 Pandas 数据框中的列中删除符号“/”之后的每个值?
- javascript - Mongoose - find() 在没有传递参数时不返回任何内容,但在传递参数时返回数据
- angular - 如何根据 typescript/angular 数组中的属性值对对象进行子集化?