Search in sources :

Example 1 with NimbusJwsEncoder

use of org.springframework.security.oauth2.jwt.NimbusJwsEncoder in project best-cloud by shanzhaozhen.

the class OAuth2ConfigurerUtils method getJwtEncoder.

public static <B extends HttpSecurityBuilder<B>> JwtEncoder getJwtEncoder(B builder) {
    JwtEncoder jwtEncoder = builder.getSharedObject(JwtEncoder.class);
    if (jwtEncoder == null) {
        jwtEncoder = getOptionalBean(builder, JwtEncoder.class);
        if (jwtEncoder == null) {
            JWKSource<SecurityContext> jwkSource = getJwkSource(builder);
            jwtEncoder = new NimbusJwsEncoder(jwkSource);
        }
        builder.setSharedObject(JwtEncoder.class, jwtEncoder);
    }
    return jwtEncoder;
}
Also used : SecurityContext(com.nimbusds.jose.proc.SecurityContext) JwtEncoder(org.springframework.security.oauth2.jwt.JwtEncoder) NimbusJwsEncoder(org.springframework.security.oauth2.jwt.NimbusJwsEncoder)

Example 2 with NimbusJwsEncoder

use of org.springframework.security.oauth2.jwt.NimbusJwsEncoder in project spring-authorization-server by spring-projects.

the class OAuth2ClientAuthenticationProviderTests method createEncoder.

private static JwtEncoder createEncoder(String secret, String algorithm) {
    SecretKey secretKey = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), algorithm);
    OctetSequenceKey secretKeyJwk = TestJwks.jwk(secretKey).build();
    JWKSource<SecurityContext> jwkSource = (jwkSelector, securityContext) -> jwkSelector.select(new JWKSet(secretKeyJwk));
    return new NimbusJwsEncoder(jwkSource);
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RegisteredClientRepository(org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SecretKeySpec(javax.crypto.spec.SecretKeySpec) JWKSet(com.nimbusds.jose.jwk.JWKSet) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) MacAlgorithm(org.springframework.security.oauth2.jose.jws.MacAlgorithm) TestOAuth2Authorizations(org.springframework.security.oauth2.server.authorization.TestOAuth2Authorizations) Map(java.util.Map) Jwt(org.springframework.security.oauth2.jwt.Jwt) OctetSequenceKey(com.nimbusds.jose.jwk.OctetSequenceKey) ProviderSettings(org.springframework.security.oauth2.server.authorization.config.ProviderSettings) JWKSource(com.nimbusds.jose.jwk.source.JWKSource) OAuth2AuthenticationException(org.springframework.security.oauth2.core.OAuth2AuthenticationException) Instant(java.time.Instant) StandardCharsets(java.nio.charset.StandardCharsets) NoOpPasswordEncoder(org.springframework.security.crypto.password.NoOpPasswordEncoder) SecretKey(javax.crypto.SecretKey) BadJwtException(org.springframework.security.oauth2.jwt.BadJwtException) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SecurityContext(com.nimbusds.jose.proc.SecurityContext) OAuth2ParameterNames(org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames) PkceParameterNames(org.springframework.security.oauth2.core.endpoint.PkceParameterNames) HashMap(java.util.HashMap) JwtEncoder(org.springframework.security.oauth2.jwt.JwtEncoder) Mockito.spy(org.mockito.Mockito.spy) JwtClaimsSet(org.springframework.security.oauth2.jwt.JwtClaimsSet) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ClientAuthenticationMethod(org.springframework.security.oauth2.core.ClientAuthenticationMethod) JwtValidationException(org.springframework.security.oauth2.jwt.JwtValidationException) ClientSettings(org.springframework.security.oauth2.server.authorization.config.ClientSettings) Before(org.junit.Before) OAuth2Authorization(org.springframework.security.oauth2.server.authorization.OAuth2Authorization) RegisteredClient(org.springframework.security.oauth2.server.authorization.client.RegisteredClient) TestKeys(org.springframework.security.oauth2.jose.TestKeys) TestJwks(org.springframework.security.oauth2.jose.TestJwks) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) OAuth2ErrorCodes(org.springframework.security.oauth2.core.OAuth2ErrorCodes) NimbusJwsEncoder(org.springframework.security.oauth2.jwt.NimbusJwsEncoder) TestRegisteredClients(org.springframework.security.oauth2.server.authorization.client.TestRegisteredClients) OAuth2AuthorizationService(org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService) Mockito.verify(org.mockito.Mockito.verify) JoseHeader(org.springframework.security.oauth2.jwt.JoseHeader) ChronoUnit(java.time.temporal.ChronoUnit) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) SignatureAlgorithm(org.springframework.security.oauth2.jose.jws.SignatureAlgorithm) OAuth2TokenType(org.springframework.security.oauth2.core.OAuth2TokenType) Collections(java.util.Collections) AuthorizationGrantType(org.springframework.security.oauth2.core.AuthorizationGrantType) SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) OctetSequenceKey(com.nimbusds.jose.jwk.OctetSequenceKey) JWKSet(com.nimbusds.jose.jwk.JWKSet) SecurityContext(com.nimbusds.jose.proc.SecurityContext) NimbusJwsEncoder(org.springframework.security.oauth2.jwt.NimbusJwsEncoder)

Example 3 with NimbusJwsEncoder

use of org.springframework.security.oauth2.jwt.NimbusJwsEncoder in project spring-authorization-server by spring-projects.

the class OidcClientRegistrationTests method init.

@BeforeClass
public static void init() {
    JWKSet jwkSet = new JWKSet(TestJwks.DEFAULT_RSA_JWK);
    jwkSource = (jwkSelector, securityContext) -> jwkSelector.select(jwkSet);
    clientJwkSet = new JWKSet(TestJwks.generateRsaJwk().build());
    jwtClientAssertionEncoder = new NimbusJwsEncoder((jwkSelector, securityContext) -> jwkSelector.select(clientJwkSet));
    db = new EmbeddedDatabaseBuilder().generateUniqueName(true).setType(EmbeddedDatabaseType.HSQL).setScriptEncoding("UTF-8").addScript("org/springframework/security/oauth2/server/authorization/oauth2-authorization-schema.sql").addScript("org/springframework/security/oauth2/server/authorization/client/oauth2-registered-client-schema.sql").build();
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) JdbcOperations(org.springframework.jdbc.core.JdbcOperations) MockMvcResultMatchers.jsonPath(org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath) OAuth2AuthorizationServerConfiguration(org.springframework.security.config.annotation.web.configuration.OAuth2AuthorizationServerConfiguration) SpringTestRule(org.springframework.security.config.test.SpringTestRule) RegisteredClientRepository(org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Autowired(org.springframework.beans.factory.annotation.Autowired) JdbcRegisteredClientRepository(org.springframework.security.oauth2.server.authorization.client.JdbcRegisteredClientRepository) JWKSet(com.nimbusds.jose.jwk.JWKSet) HttpSecurity(org.springframework.security.config.annotation.web.builders.HttpSecurity) After(org.junit.After) MockWebServer(okhttp3.mockwebserver.MockWebServer) EmbeddedDatabaseBuilder(org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder) OAuth2ResourceServerConfigurer(org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer) Jwt(org.springframework.security.oauth2.jwt.Jwt) ProviderSettings(org.springframework.security.oauth2.server.authorization.config.ProviderSettings) JdbcOAuth2AuthorizationService(org.springframework.security.oauth2.server.authorization.JdbcOAuth2AuthorizationService) JWKSource(com.nimbusds.jose.jwk.source.JWKSource) AfterClass(org.junit.AfterClass) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) Instant(java.time.Instant) RequestMatcher(org.springframework.security.web.util.matcher.RequestMatcher) EmbeddedDatabaseType(org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) NoOpPasswordEncoder(org.springframework.security.crypto.password.NoOpPasswordEncoder) MockResponse(okhttp3.mockwebserver.MockResponse) MockHttpOutputMessage(org.springframework.mock.http.MockHttpOutputMessage) SecurityContext(com.nimbusds.jose.proc.SecurityContext) OAuth2ParameterNames(org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames) BeforeClass(org.junit.BeforeClass) OAuth2AuthorizationResponseType(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationResponseType) RegisteredClientParametersMapper(org.springframework.security.oauth2.server.authorization.client.JdbcRegisteredClientRepository.RegisteredClientParametersMapper) MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse) JwtEncoder(org.springframework.security.oauth2.jwt.JwtEncoder) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) JwtClaimsSet(org.springframework.security.oauth2.jwt.JwtClaimsSet) MockMvc(org.springframework.test.web.servlet.MockMvc) MockMvcResultMatchers.status(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status) OAuth2AccessTokenResponseHttpMessageConverter(org.springframework.security.oauth2.core.http.converter.OAuth2AccessTokenResponseHttpMessageConverter) MockMvcRequestBuilders.post(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post) ClientAuthenticationMethod(org.springframework.security.oauth2.core.ClientAuthenticationMethod) MvcResult(org.springframework.test.web.servlet.MvcResult) OAuth2AccessToken(org.springframework.security.oauth2.core.OAuth2AccessToken) EmbeddedDatabase(org.springframework.jdbc.datasource.embedded.EmbeddedDatabase) ClientSettings(org.springframework.security.oauth2.server.authorization.config.ClientSettings) Before(org.junit.Before) MockMvcResultMatchers.header(org.springframework.test.web.servlet.result.MockMvcResultMatchers.header) RegisteredClient(org.springframework.security.oauth2.server.authorization.client.RegisteredClient) TestJwks(org.springframework.security.oauth2.jose.TestJwks) Customizer(org.springframework.security.config.Customizer) Test(org.junit.Test) OAuth2AccessTokenResponse(org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse) NimbusJwsEncoder(org.springframework.security.oauth2.jwt.NimbusJwsEncoder) TestRegisteredClients(org.springframework.security.oauth2.server.authorization.client.TestRegisteredClients) OAuth2AuthorizationService(org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService) SecurityFilterChain(org.springframework.security.web.SecurityFilterChain) HttpStatus(org.springframework.http.HttpStatus) JoseHeader(org.springframework.security.oauth2.jwt.JoseHeader) ChronoUnit(java.time.temporal.ChronoUnit) Rule(org.junit.Rule) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) JwtDecoder(org.springframework.security.oauth2.jwt.JwtDecoder) EnableWebSecurity(org.springframework.security.config.annotation.web.configuration.EnableWebSecurity) SignatureAlgorithm(org.springframework.security.oauth2.jose.jws.SignatureAlgorithm) OidcClientRegistration(org.springframework.security.oauth2.core.oidc.OidcClientRegistration) MockMvcRequestBuilders.get(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get) Bean(org.springframework.context.annotation.Bean) OidcClientRegistrationHttpMessageConverter(org.springframework.security.oauth2.core.oidc.http.converter.OidcClientRegistrationHttpMessageConverter) Collections(java.util.Collections) AuthorizationGrantType(org.springframework.security.oauth2.core.AuthorizationGrantType) JWKSet(com.nimbusds.jose.jwk.JWKSet) EmbeddedDatabaseBuilder(org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder) NimbusJwsEncoder(org.springframework.security.oauth2.jwt.NimbusJwsEncoder) BeforeClass(org.junit.BeforeClass)

Example 4 with NimbusJwsEncoder

use of org.springframework.security.oauth2.jwt.NimbusJwsEncoder in project spring-authorization-server by spring-projects.

the class OAuth2AuthorizationCodeGrantTests method init.

@BeforeClass
public static void init() {
    JWKSet jwkSet = new JWKSet(TestJwks.DEFAULT_RSA_JWK);
    jwkSource = (jwkSelector, securityContext) -> jwkSelector.select(jwkSet);
    jwtEncoder = new NimbusJwsEncoder(jwkSource);
    providerSettings = ProviderSettings.builder().authorizationEndpoint("/test/authorize").tokenEndpoint("/test/token").build();
    authorizationRequestConverter = mock(AuthenticationConverter.class);
    authorizationRequestAuthenticationProvider = mock(AuthenticationProvider.class);
    authorizationResponseHandler = mock(AuthenticationSuccessHandler.class);
    authorizationErrorResponseHandler = mock(AuthenticationFailureHandler.class);
    securityContextRepository = spy(new HttpSessionSecurityContextRepository());
    db = new EmbeddedDatabaseBuilder().generateUniqueName(true).setType(EmbeddedDatabaseType.HSQL).setScriptEncoding("UTF-8").addScript("org/springframework/security/oauth2/server/authorization/oauth2-authorization-schema.sql").addScript("org/springframework/security/oauth2/server/authorization/oauth2-authorization-consent-schema.sql").addScript("org/springframework/security/oauth2/server/authorization/client/oauth2-registered-client-schema.sql").build();
}
Also used : AuthenticationConverter(org.springframework.security.web.authentication.AuthenticationConverter) AuthenticationSuccessHandler(org.springframework.security.web.authentication.AuthenticationSuccessHandler) HttpSessionSecurityContextRepository(org.springframework.security.web.context.HttpSessionSecurityContextRepository) JWKSet(com.nimbusds.jose.jwk.JWKSet) EmbeddedDatabaseBuilder(org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder) OAuth2AuthorizationCodeRequestAuthenticationProvider(org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeRequestAuthenticationProvider) AuthenticationProvider(org.springframework.security.authentication.AuthenticationProvider) NimbusJwsEncoder(org.springframework.security.oauth2.jwt.NimbusJwsEncoder) AuthenticationFailureHandler(org.springframework.security.web.authentication.AuthenticationFailureHandler) BeforeClass(org.junit.BeforeClass)

Aggregations

NimbusJwsEncoder (org.springframework.security.oauth2.jwt.NimbusJwsEncoder)4 JWKSet (com.nimbusds.jose.jwk.JWKSet)3 SecurityContext (com.nimbusds.jose.proc.SecurityContext)3 JwtEncoder (org.springframework.security.oauth2.jwt.JwtEncoder)3 JWKSource (com.nimbusds.jose.jwk.source.JWKSource)2 Instant (java.time.Instant)2 ChronoUnit (java.time.temporal.ChronoUnit)2 Collections (java.util.Collections)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Before (org.junit.Before)2 BeforeClass (org.junit.BeforeClass)2 Test (org.junit.Test)2 EmbeddedDatabaseBuilder (org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder)2 NoOpPasswordEncoder (org.springframework.security.crypto.password.NoOpPasswordEncoder)2 PasswordEncoder (org.springframework.security.crypto.password.PasswordEncoder)2 AuthorizationGrantType (org.springframework.security.oauth2.core.AuthorizationGrantType)2 ClientAuthenticationMethod (org.springframework.security.oauth2.core.ClientAuthenticationMethod)2 OAuth2ParameterNames (org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames)2 TestJwks (org.springframework.security.oauth2.jose.TestJwks)2 SignatureAlgorithm (org.springframework.security.oauth2.jose.jws.SignatureAlgorithm)2