Я пытаюсь сделать проект с API с аутентификацией когнито. Для этого мне нужен CustomAuthenticationProvider, чтобы получать роли пользователя из другого микросервиса. Но он никогда не вызывается.
application.yml
spring:
security:
oauth2:
client:
registration:
cognito:
clientId: xxxx
clientSecret: xxxxx
scope: openid
redirectUriTemplate: "http://localhost:8080/login/oauth2/code/cognito"
clientName: xxxx
provider:
cognito:
issuerUri: https://cognito-idp.us-west-2.amazonaws.com/us-west-2_xxxx
usernameAttribute: cognito:username
build.gradle
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
compile 'org.springframework.security:spring-security-oauth2-jose:5.3.3.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-oauth2-client'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework:spring-tx:5.2.7.RELEASE'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
securityConfig
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity( securedEnabled = true )
@ComponentScan( "com.project" )
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private CustomAuthenticationProvider authProvider;
@Override
protected void configure ( final AuthenticationManagerBuilder auth ) throws Exception {
auth.authenticationProvider( this.authProvider );
}
@Override
protected void configure ( final HttpSecurity http ) throws Exception {
http.cors();
http.csrf().disable();
http.authorizeRequests()
.antMatchers( "/public/**" )
.permitAll()
.antMatchers( "/user/**" )
.hasRole( "USER" )
.anyRequest()
.authenticated()
.and()
.oauth2Client()
.and()
.authenticationProvider( new CustomAuthenticationProvider() );
}
}
CustomAuthenticationProvider
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
@Override
public Authentication authenticate ( final Authentication authentication ) throws AuthenticationException {
String userName = authentication.getName();
System.out.println( userName );
return new AWSAuthentication( userName, Arrays.asList( new SimpleGrantedAuthority( "ROLE_USER" ) ) );
}
@Override
public boolean supports ( Class<?> authentication ) {
return authentication.equals( AWSAuthentication.class );
}
}
Ошибка не возникает, только 403.
Я видел этот пример , но он устарел.
Может ли кто-нибудь помочь мне с подсказкой или примером ?