|
160 | 160 | * Tests for {@link OAuth2ResourceServerConfigurer}
|
161 | 161 | *
|
162 | 162 | * @author Josh Cummings
|
| 163 | + * @author Evgeniy Cheban |
163 | 164 | */
|
164 | 165 | public class OAuth2ResourceServerConfigurerTests {
|
165 | 166 | private static final String JWT_TOKEN = "token";
|
@@ -1452,6 +1453,80 @@ public void configureWhenUsingBothAuthenticationManagerResolverAndOpaqueThenWiri
|
1452 | 1453 | .hasMessageContaining("authenticationManagerResolver");
|
1453 | 1454 | }
|
1454 | 1455 |
|
| 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 | + |
1455 | 1530 | // -- support
|
1456 | 1531 |
|
1457 | 1532 | @EnableWebSecurity
|
|
0 commit comments