Search in sources :

Example 31 with Converter

use of org.springframework.core.convert.converter.Converter in project spring-security by spring-projects.

the class JwtAuthenticationConverterTests method convertWithOverriddenGrantedAuthoritiesConverter.

@Test
public void convertWithOverriddenGrantedAuthoritiesConverter() {
    Jwt jwt = TestJwts.jwt().claim("scope", "message:read message:write").build();
    Converter<Jwt, Collection<GrantedAuthority>> grantedAuthoritiesConverter = (token) -> Arrays.asList(new SimpleGrantedAuthority("blah"));
    this.jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(grantedAuthoritiesConverter);
    AbstractAuthenticationToken authentication = this.jwtAuthenticationConverter.convert(jwt);
    Collection<GrantedAuthority> authorities = authentication.getAuthorities();
    assertThat(authorities).containsExactly(new SimpleGrantedAuthority("blah"));
}
Also used : Test(org.junit.jupiter.api.Test) Converter(org.springframework.core.convert.converter.Converter) Arrays(java.util.Arrays) AbstractAuthenticationToken(org.springframework.security.authentication.AbstractAuthenticationToken) Collection(java.util.Collection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) TestJwts(org.springframework.security.oauth2.jwt.TestJwts) Jwt(org.springframework.security.oauth2.jwt.Jwt) GrantedAuthority(org.springframework.security.core.GrantedAuthority) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) AbstractAuthenticationToken(org.springframework.security.authentication.AbstractAuthenticationToken) Jwt(org.springframework.security.oauth2.jwt.Jwt) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Collection(java.util.Collection) Test(org.junit.jupiter.api.Test)

Example 32 with Converter

use of org.springframework.core.convert.converter.Converter in project spring-security by spring-projects.

the class DelegatingJwtGrantedAuthoritiesConverterTests method convertWhenConverterThenAuthorities.

@Test
public void convertWhenConverterThenAuthorities() {
    DelegatingJwtGrantedAuthoritiesConverter converter = new DelegatingJwtGrantedAuthoritiesConverter(((jwt) -> AuthorityUtils.createAuthorityList("one")));
    Jwt jwt = TestJwts.jwt().build();
    Collection<GrantedAuthority> authorities = converter.convert(jwt);
    assertThat(authorityListToOrderedSet(authorities)).containsExactly("one");
}
Also used : Test(org.junit.jupiter.api.Test) Converter(org.springframework.core.convert.converter.Converter) Collection(java.util.Collection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) TestJwts(org.springframework.security.oauth2.jwt.TestJwts) Jwt(org.springframework.security.oauth2.jwt.Jwt) AuthorityUtils(org.springframework.security.core.authority.AuthorityUtils) LinkedHashSet(java.util.LinkedHashSet) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Jwt(org.springframework.security.oauth2.jwt.Jwt) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Test(org.junit.jupiter.api.Test)

Example 33 with Converter

use of org.springframework.core.convert.converter.Converter in project spring-framework by spring-projects.

the class ConversionServiceFactoryBeanTests method createDefaultConversionServiceWithSupplements.

@Test
public void createDefaultConversionServiceWithSupplements() {
    ConversionServiceFactoryBean factory = new ConversionServiceFactoryBean();
    Set<Object> converters = new HashSet<>();
    converters.add(new Converter<String, Foo>() {

        @Override
        public Foo convert(String source) {
            return new Foo();
        }
    });
    converters.add(new ConverterFactory<String, Bar>() {

        @Override
        public <T extends Bar> Converter<String, T> getConverter(Class<T> targetType) {
            return new Converter<>() {

                @SuppressWarnings("unchecked")
                @Override
                public T convert(String source) {
                    return (T) new Bar();
                }
            };
        }
    });
    converters.add(new GenericConverter() {

        @Override
        public Set<ConvertiblePair> getConvertibleTypes() {
            return Collections.singleton(new ConvertiblePair(String.class, Baz.class));
        }

        @Override
        @Nullable
        public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
            return new Baz();
        }
    });
    factory.setConverters(converters);
    factory.afterPropertiesSet();
    ConversionService service = factory.getObject();
    assertThat(service.canConvert(String.class, Integer.class)).isTrue();
    assertThat(service.canConvert(String.class, Foo.class)).isTrue();
    assertThat(service.canConvert(String.class, Bar.class)).isTrue();
    assertThat(service.canConvert(String.class, Baz.class)).isTrue();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) GenericConverter(org.springframework.core.convert.converter.GenericConverter) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ConversionService(org.springframework.core.convert.ConversionService) Converter(org.springframework.core.convert.converter.Converter) GenericConverter(org.springframework.core.convert.converter.GenericConverter) Nullable(org.springframework.lang.Nullable) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

Converter (org.springframework.core.convert.converter.Converter)33 Test (org.junit.jupiter.api.Test)25 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)15 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)13 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)12 ClientRegistration (org.springframework.security.oauth2.client.registration.ClientRegistration)12 StandardCharsets (java.nio.charset.StandardCharsets)10 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)10 BDDMockito.given (org.mockito.BDDMockito.given)10 Mockito.mock (org.mockito.Mockito.mock)10 OAuth2AccessTokenResponse (org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse)9 JWK (com.nimbusds.jose.jwk.JWK)8 Collections (java.util.Collections)8 Function (java.util.function.Function)8 SecretKeySpec (javax.crypto.spec.SecretKeySpec)8 MockResponse (okhttp3.mockwebserver.MockResponse)8 MockWebServer (okhttp3.mockwebserver.MockWebServer)8 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)8 AfterEach (org.junit.jupiter.api.AfterEach)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8