@yARRRR

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

SpringSecurityで多重ログイン不可にできない

解決したいこと

多重ログイン不可にできません。

SpringBoot2.6.3にてSpringSecurityを使用して、ログイン機能を作っていました。
同一アカウントを複数ユーザーが使用できないようmaxmumSessionsを1に設定も有効化されません。

当方、初心者にて理解が至らずご迷惑おかけすることがあるかもしれませんが、何卒宜しくお願い致します。

発生している問題・エラー

エラーは無

該当するソースコード

@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class Config extends WebSecurityConfigurerAdapter{

	private final UserDetailsService userdetailservice;
	
	@Autowired
	private CustomWebAuthenticationDetailsSource authenticationDetailsSource;
	
	@Bean
	public BCryptPasswordEncoder passwordEncoder() {
		return new BCryptPasswordEncoder();
	}
	
	
	
	
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.authorizeRequests()
				.antMatchers("/register*","/login*","/register_account/confirm*","/badUser","/resendConfirmationEmail"
							,"/qrcode*").permitAll()
				.anyRequest().authenticated()
				.and()
			.formLogin()
				.authenticationDetailsSource(authenticationDetailsSource)
				.loginPage("/login")
				.permitAll()
				.defaultSuccessUrl("/myTask", true)
				;
		
		http.logout()
				.logoutUrl("/logout")
				;
		
		http.sessionManagement(session -> session
									.maximumSessions(1)
									.maxSessionsPreventsLogin(true)
												)
			;
				
	}
	
	
	
	@Override
	protected void configure(AuthenticationManagerBuilder authenticationManager) throws Exception {
		/*authenticationManager.userDetailsService(userdetailservice)
		.passwordEncoder(passwordEncoder());
		*/
		authenticationManager.authenticationProvider(authProvider());
	}
	
	@Override
	public void configure(WebSecurity web) throws Exception {
		web.ignoring().antMatchers("/webjars/**", "/css/**","/js/**","/backImage/**");
	}
	
	@Bean
	public DaoAuthenticationProvider authProvider() {
		CustomAuthenticationProvider authProvider = new CustomAuthenticationProvider();
		authProvider.setUserDetailsService(userdetailservice);
		authProvider.setPasswordEncoder(passwordEncoder());
		return authProvider;
	}

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

自分で試したこと

.authenticationDetailsSource(authenticationDetailsSource)が追加した2要素認証のコードなのですが、こちらを省いたプロジェクトを使用し、下記2つのコードの追加で多重ログイン不可にできることは確認しました。

http.sessionManagement(session -> session
                                 .maximumSessions(1)
								.maxSessionsPreventsLogin(true)
												)
			;


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

No Answers yet.

Your answer might help someone💌