use of org.springframework.security.oauth2.jwt.Jwt in project spring-security by spring-projects.
the class OAuth2ResourceServerConfigurerTests method requestWhenRealmNameConfiguredThenUsesOnAccessDenied.
@Test
public void requestWhenRealmNameConfiguredThenUsesOnAccessDenied() throws Exception {
this.spring.register(RealmNameConfiguredOnAccessDeniedHandler.class, JwtDecoderConfig.class).autowire();
JwtDecoder decoder = this.spring.getContext().getBean(JwtDecoder.class);
given(decoder.decode(anyString())).willReturn(JWT);
// @formatter:off
this.mvc.perform(get("/authenticated").with(bearerToken("insufficiently_scoped"))).andExpect(status().isForbidden()).andExpect(header().string(HttpHeaders.WWW_AUTHENTICATE, startsWith("Bearer realm=\"myRealm\"")));
// @formatter:on
}
use of org.springframework.security.oauth2.jwt.Jwt in project spring-security by spring-projects.
the class OAuth2ResourceServerConfigurerTests method getJwtDecoderWhenConfiguredWithDecoderAndJwkSetUriThenLastOneWins.
@Test
public void getJwtDecoderWhenConfiguredWithDecoderAndJwkSetUriThenLastOneWins() {
ApplicationContext context = mock(ApplicationContext.class);
OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer = new OAuth2ResourceServerConfigurer(context).jwt();
JwtDecoder decoder = mock(JwtDecoder.class);
jwtConfigurer.jwkSetUri(JWK_SET_URI);
jwtConfigurer.decoder(decoder);
assertThat(jwtConfigurer.getJwtDecoder()).isEqualTo(decoder);
jwtConfigurer = new OAuth2ResourceServerConfigurer(context).jwt();
jwtConfigurer.decoder(decoder);
jwtConfigurer.jwkSetUri(JWK_SET_URI);
assertThat(jwtConfigurer.getJwtDecoder()).isInstanceOf(NimbusJwtDecoder.class);
}
use of org.springframework.security.oauth2.jwt.Jwt in project spring-security by spring-projects.
the class OAuth2ResourceServerConfigurerTests method getJwtDecoderWhenTwoJwtDecoderBeansThenThrowsException.
@Test
public void getJwtDecoderWhenTwoJwtDecoderBeansThenThrowsException() {
JwtDecoder decoder = mock(JwtDecoder.class);
GenericWebApplicationContext context = new GenericWebApplicationContext();
context.registerBean("decoderOne", JwtDecoder.class, () -> decoder);
context.registerBean("decoderTwo", JwtDecoder.class, () -> decoder);
this.spring.context(context).autowire();
OAuth2ResourceServerConfigurer.JwtConfigurer jwtConfigurer = new OAuth2ResourceServerConfigurer(context).jwt();
assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(() -> jwtConfigurer.getJwtDecoder());
}
use of org.springframework.security.oauth2.jwt.Jwt in project spring-security by spring-projects.
the class OAuth2ResourceServerConfigurerTests method getJwtAuthenticationConverterWhenDuplicateConverterBeansThenThrowsException.
@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();
assertThatExceptionOfType(NoUniqueBeanDefinitionException.class).isThrownBy(jwtConfigurer::getJwtAuthenticationConverter);
}
use of org.springframework.security.oauth2.jwt.Jwt in project spring-security by spring-projects.
the class OAuth2ResourceServerConfigurerTests method requestWhenBearerTokenResolverAllowsRequestBodyThenEitherHeaderOrRequestBodyIsAccepted.
@Test
public void requestWhenBearerTokenResolverAllowsRequestBodyThenEitherHeaderOrRequestBodyIsAccepted() throws Exception {
this.spring.register(AllowBearerTokenInRequestBodyConfig.class, JwtDecoderConfig.class, BasicController.class).autowire();
JwtDecoder decoder = this.spring.getContext().getBean(JwtDecoder.class);
given(decoder.decode(anyString())).willReturn(JWT);
// @formatter:off
this.mvc.perform(get("/authenticated").with(bearerToken(JWT_TOKEN))).andExpect(status().isOk()).andExpect(content().string(JWT_SUBJECT));
this.mvc.perform(post("/authenticated").header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE).param("access_token", JWT_TOKEN)).andExpect(status().isOk()).andExpect(content().string(JWT_SUBJECT));
// @formatter:on
}
Aggregations