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 {
api(project(":cubetiq-security-jwt"))
api(project(":cubetiq-security-web"))
implementation(platform("com.netflix.graphql.dgs:graphql-dgs-platform-dependencies:5.4.3"))
implementation("com.netflix.graphql.dgs:graphql-dgs-spring-boot-starter")

View File

@ -1,10 +1,19 @@
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.runApplication
import org.springframework.security.crypto.password.PasswordEncoder
@SpringBootApplication
class GraphqlDemoApplication
class GraphqlDemoApplication @Autowired constructor(
private val passwordEncoder: PasswordEncoder,
) {
init {
PasswordUtils.setEncoder(passwordEncoder)
}
}
fun main(args: Array<String>) {
runApplication<GraphqlDemoApplication>(*args)

View File

@ -1,38 +1,44 @@
package com.cubetiqs.graphql.demo.config
import com.cubetiqs.graphql.demo.security.AuthService
import com.cubetiqs.security.jwt.AuthenticationExceptionEntryPoint
import com.cubetiqs.security.jwt.JwtSecurityConfigurer
import com.cubetiqs.sp.security.EnableCubetiqSecurityModule
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.context.annotation.Bean
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.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.crypto.bcrypt.BCryptPasswordEncoder
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.security.web.SecurityFilterChain
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
class WebSecurityConfig : WebSecurityConfigurerAdapter() {
@Autowired
private lateinit var authService: AuthService
@EnableMethodSecurity(prePostEnabled = true)
@EnableCubetiqSecurityModule
class WebSecurityConfig @Autowired constructor(
private val authService: AuthService,
private val cubetiqJwtProperties: CubetiqJwtProperties,
) {
@Bean
fun passwordEncoder(): PasswordEncoder {
return BCryptPasswordEncoder(10)
}
override fun configure(http: HttpSecurity) {
http.csrf().disable()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
@Bean
fun filterChain(http: HttpSecurity): SecurityFilterChain {
http.csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
http
.exceptionHandling()
.authenticationEntryPoint(AuthenticationExceptionEntryPoint())
http.exceptionHandling().authenticationEntryPoint(AuthenticationExceptionEntryPoint())
http
.apply(JwtSecurityConfigurer(authService))
http.apply(JwtSecurityConfigurer(authService, cubetiqJwtProperties))
http
.authorizeRequests()
.anyRequest().permitAll()
http.authorizeHttpRequests().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 java.io.Serializable
import javax.persistence.EntityListeners
import javax.persistence.MappedSuperclass
import jakarta.persistence.EntityListeners
import jakarta.persistence.MappedSuperclass
@MappedSuperclass
@EntityListeners(AuditingEntityListener::class)

View File

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

View File

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

View File

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

View File

@ -1,8 +1,8 @@
package com.cubetiqs.graphql.demo.domain.user
import java.util.*
import javax.persistence.PrePersist
import javax.persistence.PreUpdate
import jakarta.persistence.PrePersist
import jakarta.persistence.PreUpdate
class UserEntityListener {
@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.types.LoginResponse
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 org.springframework.beans.factory.annotation.Autowired
@ -16,7 +16,10 @@ class LoginMutationResolver {
@DgsMutation(field = DgsConstants.MUTATION.Login)
fun login(username: String, password: String): LoginResponse {
val auth = authService.login(username, password)
val token = JwtUtils.encryptToken(auth)
return LoginResponse(token)
val token = JwtTokenUtils.createTokens(auth)
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.UserMapper
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.exceptions.DgsEntityNotFoundException
import org.springframework.beans.factory.annotation.Autowired
@ -30,7 +30,7 @@ class UserMutationResolver @Autowired constructor(
@DgsMutation(field = DgsConstants.MUTATION.ChangeUserPassword)
fun changePassword(input: UserChangePasswordInput): User {
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)
}
}

View File

@ -1,7 +1,8 @@
package com.cubetiqs.graphql.demo.security
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.authority.SimpleGrantedAuthority
import org.springframework.security.core.userdetails.UserDetails
@ -47,7 +48,7 @@ data class AuthDetails(
}
fun isPasswordValid(password: String): Boolean {
return JwtUtils.passwordEncoder().matches(password, this.getPassword())
return PasswordUtils.matches(password, this.getPassword())
}
companion object {

View File

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

View File

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