У меня очень простая конфигурация Keycloak, расширяющая KeycloakWebSecurityConfigurerAdapter
.
@Configuration
@EnableWebSecurity
@ComponentScan(basePackageClasses = KeycloakSecurityComponents.class)
class KeycloakConfiguration extends KeycloakWebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
KeycloakAuthenticationProvider keycloakAuthenticationProvider =
keycloakAuthenticationProvider();
keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());
auth.authenticationProvider(keycloakAuthenticationProvider);
}
@Bean
public KeycloakSpringBootConfigResolver KeycloakConfigResolver() {
return new KeycloakSpringBootConfigResolver();
}
@Bean
@Override
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http
.csrf()
.disable()
.authorizeRequests()
.antMatchers("/api*")
.hasRole("user")
.anyRequest()
.permitAll();
}
}
- Запрос с недопустимым токеном через Postman возвращает HTTP-статус 401 или 403 и параметры x-frame: DENY.
- Запрос с действительным токеном через Postman возвращает HTTP-статус 200 с правильным результатом (тело, ...)
- Запрос без токена через пользовательский интерфейс swagger возвращает HTTP-статус 200 с правильным результатом (тело,...) и параметрами x-frame: DENY.
Я бы предположил, что запрос через пользовательский интерфейс swagger также возвращает статус HTTP 403 или 401. Что мне здесь не хватает? Не настроил пользовательский интерфейс swagger для использования какого-либо токена.
pom.xml
:
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-spring-boot-starter</artifactId>
</dependency>
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.keycloak.bom</groupId>
<artifactId>keycloak-adapter-bom</artifactId>
<version>14.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
application.properties
:
keycloak.auth-server-url=http://localhost:8081/auth
keycloak.realm=myrealm
keycloak.resource=myapp
keycloak.public-client=true