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"));
}
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");
}
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();
}
Aggregations