All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cloud.hedou.abp.starter.AbpAutoConfiguration.kt Maven / Gradle / Ivy

Go to download

When the functions of ABP cannot meet service requirements, the Spring Boot framework can be used to expand its own services to make use of abundant Java frameworks on the market.

There is a newer version: 1.0.1
Show newest version
package cloud.hedou.abp.starter

import cloud.hedou.abp.auth.AbpGrantedAuthoritiesConverter
import cloud.hedou.abp.auth.AbpWebMvcConfigurer
import cloud.hedou.abp.auth.ApbSecurityConfiguration
import cloud.hedou.abp.common.RemoteCommonService
import cloud.hedou.abp.identity.RemoteIdentityService
import cloud.hedou.abp.remote.AbpHttpClient
import cloud.hedou.abp.remote.HttpClient
import com.fasterxml.jackson.databind.ObjectMapper
import com.github.benmanes.caffeine.cache.Caffeine
import com.nimbusds.jose.JOSEObjectType
import com.nimbusds.jose.proc.DefaultJOSEObjectTypeVerifier
import okhttp3.HttpUrl
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.AutoConfigureAfter
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.cache.CacheManager
import org.springframework.cache.annotation.EnableCaching
import org.springframework.cache.caffeine.CaffeineCache
import org.springframework.cache.caffeine.CaffeineCacheManager
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Import
import org.springframework.security.oauth2.jwt.JwtDecoder
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder
import org.springframework.security.oauth2.server.resource.web.DefaultBearerTokenResolver
import java.util.concurrent.TimeUnit

@EnableCaching
@Import(ApbSecurityConfiguration::class, AbpWebMvcConfigurer::class, AbpContext::class)
@Configuration(proxyBeanMethods = false)
@AutoConfigureAfter(JacksonAutoConfiguration::class)
@EnableConfigurationProperties(AbpProperties::class)
class AbpAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    fun caffeineCacheManager(): CacheManager {
        return CaffeineCacheManager()
    }

    @Bean
    fun abpHttpClient(@Value("\${abp.server.url}") serverUrl: String, objectMapper: ObjectMapper): HttpClient {
        return AbpHttpClient(serverUrl, objectMapper)
    }

    @Bean
    fun remoteIdentityService(httpClient: HttpClient): RemoteIdentityService {
        return RemoteIdentityService(httpClient)
    }

    @Bean
    fun remoteCommonService(httpClient: HttpClient): RemoteCommonService {
        return RemoteCommonService(httpClient)
    }

    @Bean
    fun abpJwtDecoder(@Value("\${abp.server.url}") serverUrl: String): JwtDecoder {
        // 初始化jwks地址
        val jwkUri = HttpUrl.get(serverUrl)
            .newBuilder()
            .port(44318)
            .encodedPath("/.well-known/openid-configuration/jwks")
            .build()
            .toString()
        // 初始化缓存
        val cache = Caffeine.newBuilder()
            .expireAfterWrite(8, TimeUnit.HOURS)
            .maximumSize(128)
            .build()
        return NimbusJwtDecoder
            .withJwkSetUri(jwkUri)
            .cache(CaffeineCache("jwks", cache, false))
            .jwtProcessorCustomizer {
                it.setJWSTypeVerifier(DefaultJOSEObjectTypeVerifier(JOSEObjectType("at+jwt")))
            }
            .build()
    }

    @Bean
    @ConditionalOnMissingBean
    fun abpBearerTokenResolver(): DefaultBearerTokenResolver {
        val bearerTokenResolver = DefaultBearerTokenResolver()
        bearerTokenResolver.setAllowUriQueryParameter(true)
        bearerTokenResolver.setAllowFormEncodedBodyParameter(true)
        return bearerTokenResolver
    }

    @Bean
    @ConditionalOnMissingBean
    fun abpGrantedAuthoritiesConverter(remoteIdentityService: RemoteIdentityService): AbpGrantedAuthoritiesConverter {
        return AbpGrantedAuthoritiesConverter(remoteIdentityService)
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy