Как реализовать Spring Boot Security OAuth2 с помощью Keyclaok и SwaggerJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Как реализовать Spring Boot Security OAuth2 с помощью Keyclaok и Swagger

Сообщение Anonymous »

Я хочу иметь возможность войти в систему через пользовательский интерфейс Swagger, используя Keycloak и Spring Boot Security. Пока что я могу войти в систему с помощью токена, сгенерированного на предъявителя, через почтальона, но как настроить вход с учетными данными пользователя?
Конфигурация безопасности:

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

@Configuration
@EnableWebSecurity
@EnableMethodSecurity
@RequiredArgsConstructor
public class SecurityConfig {
private static final String[] AUTH_WHITELIST = {"/swagger-resources", "/swagger-resources/**", "/configuration/ui",
"/configuration/security", "/swagger-ui.html", "/webjars/**", "/v3/api-docs/**", "v3/api-docs",
"/api/public/**", "/api/public/authenticate", "/actuator/*", "/swagger-ui/**", "/api-docs/**"};

private final JwtAuthConverter jwtAuthConverter;

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.csrf(csrf -> csrf.disable());
http.authorizeHttpRequests(auth -> auth.requestMatchers(AUTH_WHITELIST).permitAll().anyRequest().authenticated());
http.oauth2ResourceServer(o2 -> o2.jwt(jwt -> jwt.jwtAuthenticationConverter(jwtAuthConverter)));
http.sessionManagement(s -> s.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
return http.build();
}
}
Конвертер:

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

@Component
public class JwtAuthConverter implements Converter {

private final JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();

@Value("${jwt.auth.converter.principle_attribute}")
private String principleAttribute;
@Value("${jwt.auth.converter.resource-id}")
private String resourceId;

@Override
public AbstractAuthenticationToken convert(@NonNull Jwt jwt) {
Collection authorities = Stream.concat(jwtGrantedAuthoritiesConverter.convert(jwt).stream(), extractResourceRoles(jwt).stream()).collect(Collectors.toSet());
return new JwtAuthenticationToken(jwt, authorities, getPrincipleClaimName(jwt));
}

private String getPrincipleClaimName(Jwt jwt) {
String claimName = JwtClaimNames.SUB;
if (principleAttribute != null) {
claimName = principleAttribute;
}
return jwt.getClaim(claimName);
}

private Collection

Подробнее здесь: [url]https://stackoverflow.com/questions/77022971/how-to-implement-spring-boot-security-oauth2-with-keyclaok-and-swagger[/url]
Ответить

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

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

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

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

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