tengo un problema con una aplicación en springboot 2.7.1 que actualicé a la versión 3.1.1 mi aplicación usa CustomOpaqueTokenIntrospector para validar un token, el problema es que en la versión 3.1.1 "WebSecurityConfigurerAdapter", "cors"," antMatchers", "oauth2ResourceServer().opaqueToken()" están en deprecadas, sinceramente, soy nuevo en esta autenticación y autorización. Podrían ayudarme con una respuesta de como podría corregir mi problema, les adjunto el código de mis clases de configuración.
Clase OpaqueSecurityConfig:
package ec.edu.espe.generalapi.Config.security;
import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.oauth2.server.resource.introspection.OpaqueTokenIntrospector;
@Configuration
public class OpaqueSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors();
http
.authorizeRequests(authz -> authz
.antMatchers(HttpMethod.GET, "/public/**", "/v3/api-docs/**", "/swagger-ui/**", "/swagger-ui.html", "/apiGeneral-docs/**", "swagger-ui-GeneralApi/**", "/swagger-ui-GeneralApi.html/**").permitAll()
.antMatchers(HttpMethod.POST, "/public/**").permitAll()
.anyRequest().authenticated())
.oauth2ResourceServer().opaqueToken();
}
@Bean
OpaqueTokenIntrospector tokenIntrospector(RestTemplateBuilder builder, OAuth2ResourceServerProperties resourceServerProps) {
return new CustomOpaqueTokenIntrospector(builder, resourceServerProps);
}
}
Class CustomOpaqueTokenIntrospector:
package ec.edu.espe.generalapi.Config.security;
import org.springframework.boot.autoconfigure.security.oauth2.resource.OAuth2ResourceServerProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal;
import org.springframework.security.oauth2.server.resource.introspection.NimbusOpaqueTokenIntrospector;
import org.springframework.security.oauth2.server.resource.introspection.OpaqueTokenIntrospector;
import org.springframework.web.client.RestOperations;
import java.time.Duration;
public class CustomOpaqueTokenIntrospector implements OpaqueTokenIntrospector {
private OAuth2ResourceServerProperties.Opaquetoken opaqueTokenProps;
private RestTemplateBuilder builder;
CustomOpaqueTokenIntrospector(RestTemplateBuilder builder, OAuth2ResourceServerProperties resourceServerProps) {
this.opaqueTokenProps = resourceServerProps.getOpaquetoken();
this.builder = builder;
}
@Override
public OAuth2AuthenticatedPrincipal introspect(String token) {
// System.out.println(token);
RestOperations restOperations = builder
.defaultHeader("Authorization", "Bearer " + token)
.setConnectTimeout(Duration.ofSeconds(60))
.setReadTimeout(Duration.ofSeconds(60))
.build();
return new NimbusOpaqueTokenIntrospector(opaqueTokenProps.getIntrospectionUri(), restOperations).introspect(token);
}
}
and finally:
build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '3.1.3'
id 'io.spring.dependency-management' version '1.1.3'
}
group = 'example'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '17'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server:3.0.0'
implementation 'org.springframework.boot:spring-boot-gradle-plugin:2.7.0'
compileOnly 'org.projectlombok:lombok'
implementation 'org.springdoc:springdoc-openapi-ui:1.6.12'
implementation 'org.springdoc:springdoc-openapi-security:1.6.12'
implementation group: 'org.postgresql', name: 'postgresql', version: '42.3.1'
runtimeOnly 'com.nimbusds:oauth2-oidc-sdk:9.2.4'
implementation 'org.springframework.boot:spring-boot-starter-actuator:2.6.1'
runtimeOnly 'com.oracle.database.jdbc:ojdbc8'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
Agradezco cualquier ayuda o recomendación para solucionar mi error. Muchas Gracias