Аутентификация веб-сокетов — добавление учетных данных в клиент Stomp.jsJAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Аутентификация веб-сокетов — добавление учетных данных в клиент Stomp.js

Сообщение Anonymous »

В соответствии с аутентификацией Spring Doc для веб-сокета используется аутентификация HTTP-запроса (рукопожатие), и когда Spring-Security настроен, дополнительная настройка не требуется.
Я настроил весенняя безопасность:

Код: Выделить всё

@EnableWebSecurity
@EnableMethodSecurity
@EnableRedisIndexedHttpSession(maxInactiveIntervalInSeconds = 60 * 30)
@Configuration
@RequiredArgsConstructor
public class SecurityConfig {

private final UserDetailsService detailsService;
private final RedisIndexedSessionRepository redisIndexedSessionRepository;

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

@Bean
public AuthenticationProvider authenticationProvider(PasswordEncoder passwordEncoder) {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setPasswordEncoder(passwordEncoder);
provider.setUserDetailsService(this.detailsService);
return provider;
}

@Bean
public AuthenticationManager authenticationManager(AuthenticationProvider authenticationProvider) {
return new ProviderManager(authenticationProvider);
}

@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(List.of("http://localhost:3000")); //allows React to access the API from origin on port 3000.  Change accordingly
configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE"));
configuration.setAllowCredentials(true);
configuration.addAllowedHeader("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http
.csrf().disable()
.cors(Customizer.withDefaults())
.authorizeHttpRequests(auth -> {
auth.requestMatchers(
"/api/v1/auth/register/**",
"/api/v1/auth/login"
).permitAll();
auth.anyRequest().authenticated();
})
.sessionManagement(sessionManagement -> sessionManagement
.sessionCreationPolicy(IF_REQUIRED) //
.sessionFixation(SessionManagementConfigurer.SessionFixationConfigurer::newSession) //
.maximumSessions(1) //
.sessionRegistry(sessionRegistry())
)
.logout(out -> out
.logoutUrl("/api/v1/auth/logout")
.invalidateHttpSession(true) // Invalidate all sessions after logout
.deleteCookies("JSESSIONID")
.addLogoutHandler(new CustomLogoutHandler(this.redisIndexedSessionRepository))
.logoutSuccessHandler((request, response, authentication) ->
SecurityContextHolder.clearContext()
)
)
.build();
}

@Bean
public SpringSessionBackedSessionRegistry

Подробнее здесь: [url]https://stackoverflow.com/questions/78685934/authenticating-websockets-adding-credentials-to-stomp-js-client[/url]
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «JAVA»