Completed upgrade to spring boot 3.x

This commit is contained in:
Sambo Chea 2022-12-05 21:31:54 +07:00
parent f52cdf4237
commit 49023d2237
Signed by: sombochea
GPG Key ID: 3C7CF22A05D95490
16 changed files with 71 additions and 42 deletions

8
.gitmodules vendored Normal file
View File

@ -0,0 +1,8 @@
[submodule "cubetiq-security-core"]
path = cubetiq-security-core
url = https://git.cubetiqs.com/cubetiq/cubetiq-security-core.git
branch = main
[submodule "cubetiq-security-web"]
path = cubetiq-security-web
url = https://git.cubetiqs.com/cubetiq/cubetiq-security-web.git
branch = main

1
cubetiq-security-core Submodule

@ -0,0 +1 @@
Subproject commit 612bafe9af476798a40a536c82112c63c8627f4f

1
cubetiq-security-web Submodule

@ -0,0 +1 @@
Subproject commit 62d0e718e59af79db15871bde67affd8c38b15e5

View File

@ -8,7 +8,7 @@ plugins {
} }
dependencies { dependencies {
api(project(":cubetiq-security-jwt")) api(project(":cubetiq-security-web"))
implementation(platform("com.netflix.graphql.dgs:graphql-dgs-platform-dependencies:5.4.3")) implementation(platform("com.netflix.graphql.dgs:graphql-dgs-platform-dependencies:5.4.3"))
implementation("com.netflix.graphql.dgs:graphql-dgs-spring-boot-starter") implementation("com.netflix.graphql.dgs:graphql-dgs-spring-boot-starter")

View File

@ -1,10 +1,19 @@
package com.cubetiqs.graphql.demo package com.cubetiqs.graphql.demo
import com.cubetiqs.sp.security.util.PasswordUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication import org.springframework.boot.runApplication
import org.springframework.security.crypto.password.PasswordEncoder
@SpringBootApplication @SpringBootApplication
class GraphqlDemoApplication class GraphqlDemoApplication @Autowired constructor(
private val passwordEncoder: PasswordEncoder,
) {
init {
PasswordUtils.setEncoder(passwordEncoder)
}
}
fun main(args: Array<String>) { fun main(args: Array<String>) {
runApplication<GraphqlDemoApplication>(*args) runApplication<GraphqlDemoApplication>(*args)

View File

@ -1,38 +1,44 @@
package com.cubetiqs.graphql.demo.config package com.cubetiqs.graphql.demo.config
import com.cubetiqs.graphql.demo.security.AuthService import com.cubetiqs.graphql.demo.security.AuthService
import com.cubetiqs.security.jwt.AuthenticationExceptionEntryPoint import com.cubetiqs.sp.security.EnableCubetiqSecurityModule
import com.cubetiqs.security.jwt.JwtSecurityConfigurer import com.cubetiqs.sp.security.jwt.CubetiqJwtProperties
import com.cubetiqs.sp.security.jwt.JwtSecurityConfigurer
import com.cubetiqs.sp.security.support.AuthenticationExceptionEntryPoint
import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Configuration
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity
import org.springframework.security.config.annotation.web.builders.HttpSecurity import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer
import org.springframework.security.config.http.SessionCreationPolicy import org.springframework.security.config.http.SessionCreationPolicy
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.security.web.SecurityFilterChain
@Configuration @Configuration
@EnableWebSecurity @EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) @EnableMethodSecurity(prePostEnabled = true)
class WebSecurityConfig : WebSecurityConfigurerAdapter() { @EnableCubetiqSecurityModule
@Autowired class WebSecurityConfig @Autowired constructor(
private lateinit var authService: AuthService private val authService: AuthService,
private val cubetiqJwtProperties: CubetiqJwtProperties,
) {
@Bean
fun passwordEncoder(): PasswordEncoder {
return BCryptPasswordEncoder(10)
}
override fun configure(http: HttpSecurity) { @Bean
http.csrf().disable() fun filterChain(http: HttpSecurity): SecurityFilterChain {
.sessionManagement() http.csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
http http.exceptionHandling().authenticationEntryPoint(AuthenticationExceptionEntryPoint())
.exceptionHandling()
.authenticationEntryPoint(AuthenticationExceptionEntryPoint())
http http.apply(JwtSecurityConfigurer(authService, cubetiqJwtProperties))
.apply(JwtSecurityConfigurer(authService))
http http.authorizeHttpRequests().anyRequest().permitAll()
.authorizeRequests()
.anyRequest().permitAll() return http.build()
} }
} }

View File

@ -2,8 +2,8 @@ package com.cubetiqs.graphql.demo.domain
import org.springframework.data.jpa.domain.support.AuditingEntityListener import org.springframework.data.jpa.domain.support.AuditingEntityListener
import java.io.Serializable import java.io.Serializable
import javax.persistence.EntityListeners import jakarta.persistence.EntityListeners
import javax.persistence.MappedSuperclass import jakarta.persistence.MappedSuperclass
@MappedSuperclass @MappedSuperclass
@EntityListeners(AuditingEntityListener::class) @EntityListeners(AuditingEntityListener::class)

View File

@ -8,7 +8,7 @@ import org.springframework.data.annotation.CreatedDate
import org.springframework.data.annotation.LastModifiedDate import org.springframework.data.annotation.LastModifiedDate
import java.math.BigDecimal import java.math.BigDecimal
import java.util.* import java.util.*
import javax.persistence.* import jakarta.persistence.*
@Entity @Entity
@Table(name = "accounts", indexes = [ @Table(name = "accounts", indexes = [

View File

@ -1,9 +1,9 @@
package com.cubetiqs.graphql.demo.domain.account package com.cubetiqs.graphql.demo.domain.account
import java.util.* import java.util.*
import javax.persistence.PostPersist import jakarta.persistence.PostPersist
import javax.persistence.PrePersist import jakarta.persistence.PrePersist
import javax.persistence.PreUpdate import jakarta.persistence.PreUpdate
class AccountEntityListener { class AccountEntityListener {
@PrePersist @PrePersist

View File

@ -7,7 +7,7 @@ import org.hibernate.Hibernate
import org.springframework.data.annotation.CreatedDate import org.springframework.data.annotation.CreatedDate
import org.springframework.data.annotation.LastModifiedDate import org.springframework.data.annotation.LastModifiedDate
import java.util.* import java.util.*
import javax.persistence.* import jakarta.persistence.*
@Entity @Entity
@Table( @Table(

View File

@ -1,8 +1,8 @@
package com.cubetiqs.graphql.demo.domain.user package com.cubetiqs.graphql.demo.domain.user
import java.util.* import java.util.*
import javax.persistence.PrePersist import jakarta.persistence.PrePersist
import javax.persistence.PreUpdate import jakarta.persistence.PreUpdate
class UserEntityListener { class UserEntityListener {
@PrePersist @PrePersist

View File

@ -4,7 +4,7 @@ import com.cubetiqs.graphql.demo.context.GMutation
import com.cubetiqs.graphql.demo.dgmodel.DgsConstants import com.cubetiqs.graphql.demo.dgmodel.DgsConstants
import com.cubetiqs.graphql.demo.dgmodel.types.LoginResponse import com.cubetiqs.graphql.demo.dgmodel.types.LoginResponse
import com.cubetiqs.graphql.demo.security.AuthService import com.cubetiqs.graphql.demo.security.AuthService
import com.cubetiqs.security.jwt.util.JwtUtils import com.cubetiqs.sp.security.jwt.util.JwtTokenUtils
import com.netflix.graphql.dgs.DgsMutation import com.netflix.graphql.dgs.DgsMutation
import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Autowired
@ -16,7 +16,10 @@ class LoginMutationResolver {
@DgsMutation(field = DgsConstants.MUTATION.Login) @DgsMutation(field = DgsConstants.MUTATION.Login)
fun login(username: String, password: String): LoginResponse { fun login(username: String, password: String): LoginResponse {
val auth = authService.login(username, password) val auth = authService.login(username, password)
val token = JwtUtils.encryptToken(auth) val token = JwtTokenUtils.createTokens(auth)
return LoginResponse(token) return LoginResponse(
accessToken = token.accessToken,
refreshToken = token.refreshToken,
)
} }
} }

View File

@ -7,7 +7,7 @@ import com.cubetiqs.graphql.demo.domain.user.User
import com.cubetiqs.graphql.demo.domain.user.UserInput import com.cubetiqs.graphql.demo.domain.user.UserInput
import com.cubetiqs.graphql.demo.domain.user.UserMapper import com.cubetiqs.graphql.demo.domain.user.UserMapper
import com.cubetiqs.graphql.demo.repository.UserRepository import com.cubetiqs.graphql.demo.repository.UserRepository
import com.cubetiqs.security.jwt.util.JwtUtils import com.cubetiqs.sp.security.util.PasswordUtils
import com.netflix.graphql.dgs.DgsMutation import com.netflix.graphql.dgs.DgsMutation
import com.netflix.graphql.dgs.exceptions.DgsEntityNotFoundException import com.netflix.graphql.dgs.exceptions.DgsEntityNotFoundException
import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Autowired
@ -30,7 +30,7 @@ class UserMutationResolver @Autowired constructor(
@DgsMutation(field = DgsConstants.MUTATION.ChangeUserPassword) @DgsMutation(field = DgsConstants.MUTATION.ChangeUserPassword)
fun changePassword(input: UserChangePasswordInput): User { fun changePassword(input: UserChangePasswordInput): User {
val user = userRepository.queryByUsername(input.username).orElse(null) ?: throw DgsEntityNotFoundException("User not found!") val user = userRepository.queryByUsername(input.username).orElse(null) ?: throw DgsEntityNotFoundException("User not found!")
user.password = JwtUtils.passwordEncoder().encode(input.password) user.password = PasswordUtils.encode(input.password)
return userRepository.save(user) return userRepository.save(user)
} }
} }

View File

@ -1,7 +1,8 @@
package com.cubetiqs.graphql.demo.security package com.cubetiqs.graphql.demo.security
import com.cubetiqs.graphql.demo.domain.user.User import com.cubetiqs.graphql.demo.domain.user.User
import com.cubetiqs.security.jwt.util.JwtUtils import com.cubetiqs.sp.security.jwt.util.JwtTokenUtils
import com.cubetiqs.sp.security.util.PasswordUtils
import org.springframework.security.core.GrantedAuthority import org.springframework.security.core.GrantedAuthority
import org.springframework.security.core.authority.SimpleGrantedAuthority import org.springframework.security.core.authority.SimpleGrantedAuthority
import org.springframework.security.core.userdetails.UserDetails import org.springframework.security.core.userdetails.UserDetails
@ -47,7 +48,7 @@ data class AuthDetails(
} }
fun isPasswordValid(password: String): Boolean { fun isPasswordValid(password: String): Boolean {
return JwtUtils.passwordEncoder().matches(password, this.getPassword()) return PasswordUtils.matches(password, this.getPassword())
} }
companion object { companion object {

View File

@ -14,7 +14,8 @@ input UserInput {
} }
type LoginResponse { type LoginResponse {
token: String accessToken: String
refreshToken: String
} }
input UserChangePasswordInput { input UserChangePasswordInput {

View File

@ -1,4 +1,3 @@
rootProject.name = "spring-graphql-demo" rootProject.name = "spring-graphql-demo"
include("dgs-graphql") include("cubetiq-security-core", "cubetiq-security-web", "dgs-graphql")
include("cubetiq-security-jwt")