Resource Server Finds JwtAuthenticationConverter Beans

Fixes gh-8185
This commit is contained in:
Evgeniy Cheban
2020-04-13 02:01:12 +03:00
committed by Josh Cummings
parent 9a42a028e7
commit a70d55552b
3 changed files with 86 additions and 3 deletions

View File

@@ -160,6 +160,7 @@ import static org.springframework.web.bind.annotation.RequestMethod.POST;
* Tests for {@link OAuth2ResourceServerConfigurer}
*
* @author Josh Cummings
* @author Evgeniy Cheban
*/
public class OAuth2ResourceServerConfigurerTests {
private static final String JWT_TOKEN = "token";
@@ -1452,6 +1453,80 @@ public class OAuth2ResourceServerConfigurerTests {
.hasMessageContaining("authenticationManagerResolver");
}
@Test
public void getJwtAuthenticationConverterWhenNoConverterSpecifiedThenTheDefaultIsUsed() {
ApplicationContext context =
this.spring.context(new GenericWebApplicationContext()).getContext();
OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer =
new OAuth2ResourceServerConfigurer(context).jwt();
assertThat(jwtConfigurer.getJwtAuthenticationConverter()).isInstanceOf(JwtAuthenticationConverter.class);
}
@Test
public void getJwtAuthenticationConverterWhenConverterBeanSpecified() {
JwtAuthenticationConverter converterBean = new JwtAuthenticationConverter();
GenericWebApplicationContext context = new GenericWebApplicationContext();
context.registerBean(JwtAuthenticationConverter.class, () -> converterBean);
this.spring.context(context).autowire();
OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer =
new OAuth2ResourceServerConfigurer(context).jwt();
assertThat(jwtConfigurer.getJwtAuthenticationConverter()).isEqualTo(converterBean);
}
@Test
public void getJwtAuthenticationConverterWhenConverterBeanAndAnotherOnTheDslThenTheDslOneIsUsed() {
JwtAuthenticationConverter converter = new JwtAuthenticationConverter();
JwtAuthenticationConverter converterBean = new JwtAuthenticationConverter();
GenericWebApplicationContext context = new GenericWebApplicationContext();
context.registerBean(JwtAuthenticationConverter.class, () -> converterBean);
this.spring.context(context).autowire();
OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer =
new OAuth2ResourceServerConfigurer(context).jwt();
jwtConfigurer.jwtAuthenticationConverter(converter);
assertThat(jwtConfigurer.getJwtAuthenticationConverter()).isEqualTo(converter);
}
@Test
public void getJwtAuthenticationConverterWhenDuplicateConverterBeansAndAnotherOnTheDslThenTheDslOneIsUsed() {
JwtAuthenticationConverter converter = new JwtAuthenticationConverter();
JwtAuthenticationConverter converterBean = new JwtAuthenticationConverter();
GenericWebApplicationContext context = new GenericWebApplicationContext();
context.registerBean("converterOne", JwtAuthenticationConverter.class, () -> converterBean);
context.registerBean("converterTwo", JwtAuthenticationConverter.class, () -> converterBean);
this.spring.context(context).autowire();
OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer =
new OAuth2ResourceServerConfigurer(context).jwt();
jwtConfigurer.jwtAuthenticationConverter(converter);
assertThat(jwtConfigurer.getJwtAuthenticationConverter()).isEqualTo(converter);
}
@Test
public void getJwtAuthenticationConverterWhenDuplicateConverterBeansThenThrowsException() {
JwtAuthenticationConverter converterBean = new JwtAuthenticationConverter();
GenericWebApplicationContext context = new GenericWebApplicationContext();
context.registerBean("converterOne", JwtAuthenticationConverter.class, () -> converterBean);
context.registerBean("converterTwo", JwtAuthenticationConverter.class, () -> converterBean);
this.spring.context(context).autowire();
OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer =
new OAuth2ResourceServerConfigurer(context).jwt();
assertThatCode(jwtConfigurer::getJwtAuthenticationConverter)
.isInstanceOf(NoUniqueBeanDefinitionException.class);
}
// -- support
@EnableWebSecurity