From 3cf19c23cb4985b9885b39c701b0443fc2496839 Mon Sep 17 00:00:00 2001 From: kashiuno Date: Wed, 8 Jan 2025 08:48:54 +0300 Subject: [PATCH] Delete old code and change port for local development --- build.gradle.kts | 2 +- .../KotlinRegisteredClientRowMapper.kt | 66 ------------------- .../qr_access_auth_server/SecurityConfig.kt | 15 +---- src/main/resources/application.yaml | 2 + 4 files changed, 5 insertions(+), 80 deletions(-) delete mode 100644 src/main/kotlin/ru/vyatsu/qr_access_auth_server/KotlinRegisteredClientRowMapper.kt diff --git a/build.gradle.kts b/build.gradle.kts index 5c89058..8090d5a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -27,7 +27,7 @@ dependencies { testImplementation("org.jetbrains.kotlin:kotlin-test-junit5") testRuntimeOnly("org.junit.platform:junit-platform-launcher") implementation("org.yaml:snakeyaml") - + implementation("com.fasterxml.jackson.module:jackson-module-kotlin") } kotlin { diff --git a/src/main/kotlin/ru/vyatsu/qr_access_auth_server/KotlinRegisteredClientRowMapper.kt b/src/main/kotlin/ru/vyatsu/qr_access_auth_server/KotlinRegisteredClientRowMapper.kt deleted file mode 100644 index a486cb3..0000000 --- a/src/main/kotlin/ru/vyatsu/qr_access_auth_server/KotlinRegisteredClientRowMapper.kt +++ /dev/null @@ -1,66 +0,0 @@ -package ru.vyatsu.qr_access_auth_server - -import org.springframework.security.oauth2.core.AuthorizationGrantType -import org.springframework.security.oauth2.core.ClientAuthenticationMethod -import org.springframework.security.oauth2.server.authorization.client.JdbcRegisteredClientRepository.RegisteredClientRowMapper -import org.springframework.security.oauth2.server.authorization.client.RegisteredClient -import org.springframework.util.StringUtils -import java.sql.ResultSet - -class KotlinRegisteredClientRowMapper : RegisteredClientRowMapper() { - - override fun mapRow(rs: ResultSet, rowNum: Int): RegisteredClient? { - val clientIdIssuedAt = rs.getTimestamp("client_id_issued_at") - val clientSecretExpiresAt = rs.getTimestamp("client_secret_expires_at") - val clientAuthenticationMethods = - StringUtils.commaDelimitedListToSet(rs.getString("client_authentication_methods")) - val authorizationGrantTypes = StringUtils.commaDelimitedListToSet(rs.getString("authorization_grant_types")) - val redirectUris = StringUtils.commaDelimitedListToSet(rs.getString("redirect_uris")) - val postLogoutRedirectUris = StringUtils.commaDelimitedListToSet(rs.getString("post_logout_redirect_uris")) - val clientScopes = StringUtils.commaDelimitedListToSet(rs.getString("scopes")) - val builder = RegisteredClient.withId(rs.getString("id")) - .clientId(rs.getString("client_id")) - .clientIdIssuedAt(clientIdIssuedAt?.toInstant()) - .clientSecret(rs.getString("client_secret")) - .clientSecretExpiresAt(clientSecretExpiresAt?.toInstant()) - .clientName(rs.getString("client_name")) - .clientAuthenticationMethods { authenticationMethods -> - clientAuthenticationMethods.forEach { authenticationMethod -> - authenticationMethods.add(resolveClientAuthenticationMethod(authenticationMethod)) - } - } - .authorizationGrantTypes { grantTypes -> - authorizationGrantTypes.forEach { grantType -> - grantTypes.add(resolveAuthorizationGrantType(grantType)) - } - } - .redirectUris { uris -> uris.addAll(redirectUris) } - .postLogoutRedirectUris { uris -> - uris.addAll(postLogoutRedirectUris) - } - .scopes { scopes -> scopes.addAll(clientScopes) } - return builder.build() - } - - private fun resolveAuthorizationGrantType(authorizationGrantType: String): AuthorizationGrantType { - return if (AuthorizationGrantType.AUTHORIZATION_CODE.value == authorizationGrantType) { - AuthorizationGrantType.AUTHORIZATION_CODE - } else if (AuthorizationGrantType.CLIENT_CREDENTIALS.value == authorizationGrantType) { - AuthorizationGrantType.CLIENT_CREDENTIALS - } else { - if (AuthorizationGrantType.REFRESH_TOKEN.value == authorizationGrantType) AuthorizationGrantType.REFRESH_TOKEN - else AuthorizationGrantType(authorizationGrantType) - } - } - - private fun resolveClientAuthenticationMethod(clientAuthenticationMethod: String): ClientAuthenticationMethod { - return if (ClientAuthenticationMethod.CLIENT_SECRET_BASIC.value == clientAuthenticationMethod) { - ClientAuthenticationMethod.CLIENT_SECRET_BASIC - } else if (ClientAuthenticationMethod.CLIENT_SECRET_POST.value == clientAuthenticationMethod) { - ClientAuthenticationMethod.CLIENT_SECRET_POST - } else { - if (ClientAuthenticationMethod.NONE.value == clientAuthenticationMethod) ClientAuthenticationMethod.NONE - else ClientAuthenticationMethod(clientAuthenticationMethod) - } - } -} \ No newline at end of file diff --git a/src/main/kotlin/ru/vyatsu/qr_access_auth_server/SecurityConfig.kt b/src/main/kotlin/ru/vyatsu/qr_access_auth_server/SecurityConfig.kt index dec916f..af008bb 100644 --- a/src/main/kotlin/ru/vyatsu/qr_access_auth_server/SecurityConfig.kt +++ b/src/main/kotlin/ru/vyatsu/qr_access_auth_server/SecurityConfig.kt @@ -1,6 +1,5 @@ package ru.vyatsu.qr_access_auth_server -import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.nimbusds.jose.jwk.JWKSet import com.nimbusds.jose.jwk.RSAKey import com.nimbusds.jose.jwk.source.ImmutableJWKSet @@ -13,13 +12,11 @@ import org.springframework.jdbc.core.JdbcTemplate import org.springframework.security.config.annotation.web.builders.HttpSecurity import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity import org.springframework.security.core.userdetails.UserDetailsService -import org.springframework.security.jackson2.SecurityJackson2Modules import org.springframework.security.oauth2.jwt.JwtDecoder import org.springframework.security.oauth2.server.authorization.client.JdbcRegisteredClientRepository import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository import org.springframework.security.oauth2.server.authorization.config.annotation.web.configuration.OAuth2AuthorizationServerConfiguration import org.springframework.security.oauth2.server.authorization.config.annotation.web.configurers.OAuth2AuthorizationServerConfigurer -import org.springframework.security.oauth2.server.authorization.jackson2.OAuth2AuthorizationServerJackson2Module import org.springframework.security.oauth2.server.authorization.settings.AuthorizationServerSettings import org.springframework.security.provisioning.InMemoryUserDetailsManager import org.springframework.security.web.SecurityFilterChain @@ -30,7 +27,7 @@ import java.security.interfaces.RSAPublicKey import java.util.* @Configuration -@EnableWebSecurity(debug = true) +@EnableWebSecurity class SecurityConfig { @Bean @Order(1) @@ -61,15 +58,7 @@ class SecurityConfig { @Bean fun registeredClientRepository(operations: JdbcTemplate): RegisteredClientRepository { - val clientRepository = JdbcRegisteredClientRepository(operations) - val clientRowMapper = KotlinRegisteredClientRowMapper() - val classLoader = JdbcRegisteredClientRepository::class.java.classLoader - val objectMapper = jacksonObjectMapper() - objectMapper.registerModules(SecurityJackson2Modules.getModules(classLoader)) - objectMapper.registerModule(OAuth2AuthorizationServerJackson2Module()) - clientRowMapper.setObjectMapper(objectMapper) - clientRepository.setRegisteredClientRowMapper(clientRowMapper) - return clientRepository + return JdbcRegisteredClientRepository(operations) } @Bean diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index ed7e5e5..06ef2b7 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -1,3 +1,5 @@ +server: + port: 8081 spring: application: name: qr-access-auth-server