37 lines
1.1 KiB
Kotlin
37 lines
1.1 KiB
Kotlin
package com.chantha.jdbc.controller
|
|
|
|
import com.chantha.jdbc.security.User
|
|
import com.chantha.jdbc.security.UserRepo
|
|
import com.sun.istack.NotNull
|
|
import org.springframework.beans.factory.annotation.Autowired
|
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
|
|
import org.springframework.stereotype.Controller
|
|
import org.springframework.web.bind.annotation.GetMapping
|
|
import org.springframework.web.bind.annotation.ModelAttribute
|
|
import org.springframework.web.bind.annotation.PostMapping
|
|
import org.springframework.web.bind.annotation.RequestParam
|
|
|
|
|
|
@Controller
|
|
class UserController @Autowired constructor(private val userRepo: UserRepo){
|
|
|
|
fun encoder():BCryptPasswordEncoder{
|
|
return BCryptPasswordEncoder()
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/register")
|
|
fun register():String{
|
|
return "register"
|
|
}
|
|
|
|
@PostMapping("/register")
|
|
fun saveRegister(@NotNull @ModelAttribute("user") user:User?):String{
|
|
user!!.password=encoder().encode(user.password)
|
|
user.roles="ROLES_${user.roles}"
|
|
userRepo.save(user)
|
|
return "register"
|
|
}
|
|
} |