Search in sources :

Example 1 with JwtAuthenticationConverter

use of org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter in project spring-security by spring-projects.

the class OAuth2ResourceServerBeanDefinitionParserTests method requestWhenJwtAuthenticationConverterThenUsed.

@Test
public void requestWhenJwtAuthenticationConverterThenUsed() throws Exception {
    this.spring.configLocations(xml("MockJwtDecoder"), xml("MockJwtAuthenticationConverter"), xml("JwtAuthenticationConverter")).autowire();
    Converter<Jwt, JwtAuthenticationToken> jwtAuthenticationConverter = (Converter<Jwt, JwtAuthenticationToken>) this.spring.getContext().getBean("jwtAuthenticationConverter");
    given(jwtAuthenticationConverter.convert(any(Jwt.class))).willReturn(new JwtAuthenticationToken(TestJwts.jwt().build(), Collections.emptyList()));
    JwtDecoder jwtDecoder = this.spring.getContext().getBean(JwtDecoder.class);
    given(jwtDecoder.decode(anyString())).willReturn(TestJwts.jwt().build());
    // @formatter:off
    this.mvc.perform(get("/").header("Authorization", "Bearer token")).andExpect(status().isNotFound());
    // @formatter:on
    verify(jwtAuthenticationConverter).convert(any(Jwt.class));
}
Also used : JwtAuthenticationToken(org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken) Jwt(org.springframework.security.oauth2.jwt.Jwt) NimbusJwtDecoder(org.springframework.security.oauth2.jwt.NimbusJwtDecoder) JwtDecoder(org.springframework.security.oauth2.jwt.JwtDecoder) Converter(org.springframework.core.convert.converter.Converter) Test(org.junit.jupiter.api.Test)

Example 2 with JwtAuthenticationConverter

use of org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter in project spring-security by spring-projects.

the class OAuth2ResourceServerConfigurerTests method getJwtAuthenticationConverterWhenConverterBeanSpecified.

@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);
}
Also used : JwtAuthenticationConverter(org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 3 with JwtAuthenticationConverter

use of org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter in project spring-security by spring-projects.

the class OAuth2ResourceServerConfigurerTests method getJwtAuthenticationConverterWhenConverterBeanAndAnotherOnTheDslThenTheDslOneIsUsed.

@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);
}
Also used : JwtAuthenticationConverter(org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 4 with JwtAuthenticationConverter

use of org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter in project spring-security by spring-projects.

the class OAuth2ResourceServerConfigurerTests method requestWhenJwtAuthenticationConverterConfiguredOnDslThenIsUsed.

@Test
public void requestWhenJwtAuthenticationConverterConfiguredOnDslThenIsUsed() throws Exception {
    this.spring.register(JwtDecoderConfig.class, JwtAuthenticationConverterConfiguredOnDsl.class, BasicController.class).autowire();
    Converter<Jwt, JwtAuthenticationToken> jwtAuthenticationConverter = this.spring.getContext().getBean(JwtAuthenticationConverterConfiguredOnDsl.class).getJwtAuthenticationConverter();
    given(jwtAuthenticationConverter.convert(JWT)).willReturn(JWT_AUTHENTICATION_TOKEN);
    JwtDecoder jwtDecoder = this.spring.getContext().getBean(JwtDecoder.class);
    given(jwtDecoder.decode(anyString())).willReturn(JWT);
    // @formatter:off
    this.mvc.perform(get("/").with(bearerToken(JWT_TOKEN))).andExpect(status().isOk());
    // @formatter:on
    verify(jwtAuthenticationConverter).convert(JWT);
}
Also used : JwtAuthenticationToken(org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken) Jwt(org.springframework.security.oauth2.jwt.Jwt) NimbusJwtDecoder(org.springframework.security.oauth2.jwt.NimbusJwtDecoder) JwtDecoder(org.springframework.security.oauth2.jwt.JwtDecoder) Test(org.junit.jupiter.api.Test)

Example 5 with JwtAuthenticationConverter

use of org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter in project spring-security by spring-projects.

the class OAuth2ResourceServerConfigurerTests method getJwtAuthenticationConverterWhenDuplicateConverterBeansAndAnotherOnTheDslThenTheDslOneIsUsed.

@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);
}
Also used : JwtAuthenticationConverter(org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)6 JwtAuthenticationConverter (org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter)5 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)4 Jwt (org.springframework.security.oauth2.jwt.Jwt)2 JwtDecoder (org.springframework.security.oauth2.jwt.JwtDecoder)2 NimbusJwtDecoder (org.springframework.security.oauth2.jwt.NimbusJwtDecoder)2 JwtAuthenticationToken (org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken)2 ModuleAuthenticationImpl (com.evolveum.midpoint.authentication.impl.module.authentication.ModuleAuthenticationImpl)1 OidcResourceServerModuleWebSecurityConfiguration (com.evolveum.midpoint.authentication.impl.module.configuration.OidcResourceServerModuleWebSecurityConfiguration)1 OidcResourceServerProvider (com.evolveum.midpoint.authentication.impl.provider.OidcResourceServerProvider)1 NoUniqueBeanDefinitionException (org.springframework.beans.factory.NoUniqueBeanDefinitionException)1 Converter (org.springframework.core.convert.converter.Converter)1 HttpSecurity (org.springframework.security.config.annotation.web.builders.HttpSecurity)1 SecurityFilterChain (org.springframework.security.web.SecurityFilterChain)1