Skip to content

Commit a70d555

Browse files
evgeniychebanjzheaux
authored andcommitted
Resource Server Finds JwtAuthenticationConverter Beans
Fixes gh-8185
1 parent 9a42a02 commit a70d555

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/resource/OAuth2ResourceServerConfigurer.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
* </ul>
124124
*
125125
* @author Josh Cummings
126+
* @author Evgeniy Cheban
126127
* @since 5.1
127128
* @see BearerTokenAuthenticationFilter
128129
* @see JwtAuthenticationProvider
@@ -280,8 +281,7 @@ public class JwtConfigurer {
280281
private AuthenticationManager authenticationManager;
281282
private JwtDecoder decoder;
282283

283-
private Converter<Jwt, ? extends AbstractAuthenticationToken> jwtAuthenticationConverter =
284-
new JwtAuthenticationConverter();
284+
private Converter<Jwt, ? extends AbstractAuthenticationToken> jwtAuthenticationConverter;
285285

286286
JwtConfigurer(ApplicationContext context) {
287287
this.context = context;
@@ -315,6 +315,14 @@ public OAuth2ResourceServerConfigurer<H> and() {
315315
}
316316

317317
Converter<Jwt, ? extends AbstractAuthenticationToken> getJwtAuthenticationConverter() {
318+
if (this.jwtAuthenticationConverter == null) {
319+
if (this.context.getBeanNamesForType(JwtAuthenticationConverter.class).length > 0) {
320+
this.jwtAuthenticationConverter = this.context.getBean(JwtAuthenticationConverter.class);
321+
} else {
322+
this.jwtAuthenticationConverter = new JwtAuthenticationConverter();
323+
}
324+
}
325+
318326
return this.jwtAuthenticationConverter;
319327
}
320328

config/src/test/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/resource/OAuth2ResourceServerConfigurerTests.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@
160160
* Tests for {@link OAuth2ResourceServerConfigurer}
161161
*
162162
* @author Josh Cummings
163+
* @author Evgeniy Cheban
163164
*/
164165
public class OAuth2ResourceServerConfigurerTests {
165166
private static final String JWT_TOKEN = "token";
@@ -1452,6 +1453,80 @@ public void configureWhenUsingBothAuthenticationManagerResolverAndOpaqueThenWiri
14521453
.hasMessageContaining("authenticationManagerResolver");
14531454
}
14541455

1456+
@Test
1457+
public void getJwtAuthenticationConverterWhenNoConverterSpecifiedThenTheDefaultIsUsed() {
1458+
ApplicationContext context =
1459+
this.spring.context(new GenericWebApplicationContext()).getContext();
1460+
1461+
OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer =
1462+
new OAuth2ResourceServerConfigurer(context).jwt();
1463+
1464+
assertThat(jwtConfigurer.getJwtAuthenticationConverter()).isInstanceOf(JwtAuthenticationConverter.class);
1465+
}
1466+
1467+
@Test
1468+
public void getJwtAuthenticationConverterWhenConverterBeanSpecified() {
1469+
JwtAuthenticationConverter converterBean = new JwtAuthenticationConverter();
1470+
1471+
GenericWebApplicationContext context = new GenericWebApplicationContext();
1472+
context.registerBean(JwtAuthenticationConverter.class, () -> converterBean);
1473+
this.spring.context(context).autowire();
1474+
1475+
OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer =
1476+
new OAuth2ResourceServerConfigurer(context).jwt();
1477+
1478+
assertThat(jwtConfigurer.getJwtAuthenticationConverter()).isEqualTo(converterBean);
1479+
}
1480+
1481+
@Test
1482+
public void getJwtAuthenticationConverterWhenConverterBeanAndAnotherOnTheDslThenTheDslOneIsUsed() {
1483+
JwtAuthenticationConverter converter = new JwtAuthenticationConverter();
1484+
JwtAuthenticationConverter converterBean = new JwtAuthenticationConverter();
1485+
1486+
GenericWebApplicationContext context = new GenericWebApplicationContext();
1487+
context.registerBean(JwtAuthenticationConverter.class, () -> converterBean);
1488+
this.spring.context(context).autowire();
1489+
1490+
OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer =
1491+
new OAuth2ResourceServerConfigurer(context).jwt();
1492+
jwtConfigurer.jwtAuthenticationConverter(converter);
1493+
1494+
assertThat(jwtConfigurer.getJwtAuthenticationConverter()).isEqualTo(converter);
1495+
}
1496+
1497+
@Test
1498+
public void getJwtAuthenticationConverterWhenDuplicateConverterBeansAndAnotherOnTheDslThenTheDslOneIsUsed() {
1499+
JwtAuthenticationConverter converter = new JwtAuthenticationConverter();
1500+
JwtAuthenticationConverter converterBean = new JwtAuthenticationConverter();
1501+
1502+
GenericWebApplicationContext context = new GenericWebApplicationContext();
1503+
context.registerBean("converterOne", JwtAuthenticationConverter.class, () -> converterBean);
1504+
context.registerBean("converterTwo", JwtAuthenticationConverter.class, () -> converterBean);
1505+
this.spring.context(context).autowire();
1506+
1507+
OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer =
1508+
new OAuth2ResourceServerConfigurer(context).jwt();
1509+
jwtConfigurer.jwtAuthenticationConverter(converter);
1510+
1511+
assertThat(jwtConfigurer.getJwtAuthenticationConverter()).isEqualTo(converter);
1512+
}
1513+
1514+
@Test
1515+
public void getJwtAuthenticationConverterWhenDuplicateConverterBeansThenThrowsException() {
1516+
JwtAuthenticationConverter converterBean = new JwtAuthenticationConverter();
1517+
1518+
GenericWebApplicationContext context = new GenericWebApplicationContext();
1519+
context.registerBean("converterOne", JwtAuthenticationConverter.class, () -> converterBean);
1520+
context.registerBean("converterTwo", JwtAuthenticationConverter.class, () -> converterBean);
1521+
this.spring.context(context).autowire();
1522+
1523+
OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer =
1524+
new OAuth2ResourceServerConfigurer(context).jwt();
1525+
1526+
assertThatCode(jwtConfigurer::getJwtAuthenticationConverter)
1527+
.isInstanceOf(NoUniqueBeanDefinitionException.class);
1528+
}
1529+
14551530
// -- support
14561531

14571532
@EnableWebSecurity

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/JwtAuthenticationConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)