This commit is contained in:
2024-12-02 18:32:14 +09:00
parent a02349f7a1
commit 576932da3d
16 changed files with 287 additions and 112 deletions
@@ -2,6 +2,7 @@ package kr.lunaticbum.back.lun.configs
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Configuration
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
@@ -17,6 +18,10 @@ class AppConfig : WebMvcConfigurer {
registry.addResourceHandler(resourceHandler).addResourceLocations(resourceLocation)
}
override fun addInterceptors(registry: InterceptorRegistry) {
registry.addInterceptor(BumsInterceptor())
super.addInterceptors(registry)
}
// @Bean
// fun getProperty() : Map<String,String>{
// println("telegramBotKey >>>> $telegramBotKey")
@@ -0,0 +1,43 @@
package kr.lunaticbum.back.lun.configs
import com.google.gson.Gson
import jakarta.servlet.http.Cookie
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import org.springframework.lang.Nullable
import org.springframework.web.servlet.HandlerInterceptor
import org.springframework.web.servlet.ModelAndView
class BumsInterceptor : HandlerInterceptor{
@Throws(Exception::class)
override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean {
println("===============================================")
println("==================== BEGIN ====================")
println("Request URL ===> " + request.requestURL)
return super.preHandle(request, response, handler)
}
val WRITE_PERMISSION_KEY = "PERMISSION"
@Throws(Exception::class)
override fun postHandle(
request: HttpServletRequest,
response: HttpServletResponse,
handler: Any,
@Nullable modelAndView: ModelAndView?
) {
if (request.requestURI.contains("logout") == false && !request.cookies.isNullOrEmpty() && request.cookies.filter { it.name.equals("S33-DATA") && it.value.length > 0 }.size > 0) {
response.addCookie(Cookie("S33-DATA",request.cookies.filter { it.name.equals("S33-DATA") && it.value.length > 0 }.get(0).value))
modelAndView?.modelMap?.put(WRITE_PERMISSION_KEY,"OK")
println("Response modelMap ===> ${Gson().toJson(modelAndView?.modelMap)}")
} else if (request.requestURI.contains("logout")) {
response.addCookie(Cookie("S33-DATA",null))
modelAndView?.modelMap?.put(WRITE_PERMISSION_KEY,"NO")
}
println("==================== END ======================")
println("===============================================")
super.postHandle(request, response, handler, modelAndView)
}
}
@@ -41,6 +41,7 @@ class SecurityConfig {
frameOptionsConfig.disable()
}
}
http.authorizeHttpRequests {
logService.log(it.toString())
it.requestMatchers(HttpMethod.POST,"/user/**").permitAll()
@@ -21,7 +21,6 @@ import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.servlet.ModelAndView
import java.io.*
import java.net.URLDecoder
import java.text.SimpleDateFormat
@@ -47,8 +46,8 @@ class BlogController() {
lateinit var logService: LogService
val WRITE_PERMISSION_KEY = "PERMISSION"
@GetMapping("write/{token}","write")
fun writ(@PathVariable token : String? ) : ModelAndView{
val vm = ModelAndView("content/blog/write")
fun writ(@PathVariable token : String? ) : ResultMV{
val vm = ResultMV("content/blog/write")
if (token.equals(TEMPTOKEN)) {
vm.modelMap.put(WRITE_PERMISSION_KEY,"OK")
vm.modelMap.put(EncTypeKey, EncType11)
@@ -126,8 +125,8 @@ class BlogController() {
}
@GetMapping("viewer/{postId}")
fun viewer(@PathVariable postId : String) : ModelAndView{
val vm = ModelAndView("content/blog/viewer")
fun viewer(@PathVariable postId : String) : ResultMV{
val vm = ResultMV("content/blog/viewer")
postManageg.getPost(postId).block().apply {
this?.title = URLDecoder.decode(this?.title)
this?.content = URLDecoder.decode(this?.content)
@@ -137,9 +136,9 @@ class BlogController() {
}
@GetMapping("modify")
fun modify(@RequestParam("token") token : String?) : ModelAndView{
fun modify(@RequestParam("token") token : String?) : ResultMV{
logService.log("incoming modify")
val vm = ModelAndView("content/blog/modify")
val vm = ResultMV("content/blog/modify")
if (TEMPTOKEN.equals(token)) {
postManageg.find20()?.apply {
forEach {
@@ -160,8 +159,8 @@ class BlogController() {
}
@GetMapping("editor/{postId}")
fun editor(@PathVariable postId : String, @RequestParam("token") token : String?) : ModelAndView{
val vm = ModelAndView("content/blog/editor")
fun editor(@PathVariable postId : String, @RequestParam("token") token : String?) : ResultMV{
val vm = ResultMV("content/blog/editor")
postManageg.getPost(postId).block().apply {
this?.title = URLDecoder.decode(this?.title)
this?.content = URLDecoder.decode(this?.content)
@@ -177,8 +176,8 @@ class BlogController() {
@GetMapping("recent")
fun recent() : ModelAndView{
val vm = ModelAndView("content/blog/viewer")
fun recent() : ResultMV{
val vm = ResultMV("content/blog/viewer")
locationLogService.find20().forEach {
logService.log(Gson().toJson(it))
}
@@ -6,17 +6,16 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kr.lunaticbum.back.lun.configs.GlobalEnvironment
import kr.lunaticbum.back.lun.model.LocationLog
import kr.lunaticbum.back.lun.model.LocationLogService
import kr.lunaticbum.back.lun.model.ResponceResult
import kr.lunaticbum.back.lun.model.*
import kr.lunaticbum.back.lun.utils.LogService
import kr.lunaticbum.back.lun.utils.plainText
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.servlet.ModelAndView
import java.util.*
import java.util.Base64
@RestController
@@ -32,15 +31,15 @@ class BumsPrivate {
lateinit var locationService: LocationLogService
@GetMapping("where")
fun where() : ModelAndView {
val m = ModelAndView("content/private/where")
fun where() : ResultMV {
val m = ResultMV("content/private/where")
locationService.find20().apply {
m.modelMap.put("locations",this.reversed())
forEach {
logService.log(it.timeString.plus(it.mAddressLines.joinToString(",")))
}
// forEach {
// logService.log(it.timeString.plus(it.mAddressLines.joinToString(",")))
// }
}
m.modelMap.put("title","돼지 여기있다요~!!")
m.setTitle("돼지 여기있다요~!!")
return m
}
@@ -49,11 +48,9 @@ class BumsPrivate {
fun login(httpServletRequest: HttpServletRequest, @RequestBody jsonString: String) : ResponseEntity<ResponceResult> {
logService.log("${httpServletRequest.requestURI}")
logService.log(jsonString)
var lResultCode = 0
var lResultMsg = "Suscces"
var location : LocationLog? = null
val decodedBytes: ByteArray = Base64.getMimeDecoder().decode(jsonString)
String(decodedBytes).let {
jsonString.plainText().let {
Gson().fromJson<LocationLog>(it, LocationLog::class.java)?.let { model ->
location = model
logService.log(model.toString())
@@ -61,8 +58,7 @@ class BumsPrivate {
}
}
val responce = ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(ResponceResult().apply {
this.resultCode = lResultCode
this.resultMsg = lResultMsg
})
CoroutineScope(Dispatchers.IO).launch {
location?.let {
@@ -3,13 +3,13 @@ package kr.lunaticbum.back.lun.controllers
import com.google.gson.Gson
import jakarta.servlet.http.HttpServletResponse
import kr.lunaticbum.back.lun.model.PostManageg
import kr.lunaticbum.back.lun.model.ResultMV
import kr.lunaticbum.back.lun.utils.LogService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.servlet.ModelAndView
import java.net.URLDecoder
@RestController
@@ -23,8 +23,8 @@ class Home {
private lateinit var postManageg: PostManageg
@GetMapping("/","/home")
fun home() : ModelAndView {
val vm = ModelAndView("content/home")
fun home() : ResultMV {
val vm = ResultMV("content/home")
vm.modelMap.put("Posts", postManageg.find20(Pageable.ofSize(20)).apply {
this.forEach {
it.title = URLDecoder.decode(it.title)
@@ -41,8 +41,8 @@ class Home {
}
@GetMapping("/licenses")
fun licenses() : ModelAndView {
val vm = ModelAndView("content/licenses")
fun licenses() : ResultMV {
val vm = ResultMV("content/licenses")
return vm
}
@@ -6,18 +6,16 @@ import kr.lunaticbum.back.lun.configs.GlobalEnvironment
import kr.lunaticbum.back.lun.configs.GlobalEnvironment.Companion.ApiKeyWordKey
import kr.lunaticbum.back.lun.configs.GlobalEnvironment.Companion.EncType11
import kr.lunaticbum.back.lun.configs.GlobalEnvironment.Companion.EncTypeKey
import kr.lunaticbum.back.lun.model.RequestModel
import kr.lunaticbum.back.lun.model.ResponceResult
import kr.lunaticbum.back.lun.model.User
import kr.lunaticbum.back.lun.model.UserManager
import kr.lunaticbum.back.lun.model.*
import kr.lunaticbum.back.lun.utils.LogService
import kr.lunaticbum.back.lun.utils.extractModelData
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.web.bind.annotation.*
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.servlet.ModelAndView
import java.io.File
import java.util.*
@@ -37,9 +35,9 @@ class UserController {
lateinit var userManager: UserManager
@GetMapping("join")
fun hello(httpServletRequest: HttpServletRequest): ModelAndView {
fun hello(httpServletRequest: HttpServletRequest): ResultMV {
logService.log("onJoin")
val vm = ModelAndView("content/user/join")
val vm = ResultMV("content/user/join")
// when(System.currentTimeMillis() % 5L) {
// 0L -> vm.modelMap.put(EncTypeKey,"T4")
// 1L -> vm.modelMap.put(EncTypeKey,"T3")
@@ -56,9 +54,9 @@ class UserController {
}
@GetMapping("login")
fun userLogin(httpServletRequest: HttpServletRequest): ModelAndView {
fun userLogin(httpServletRequest: HttpServletRequest): ResultMV {
logService.log("onJoin")
val vm = ModelAndView("content/user/login")
val vm = ResultMV("content/user/login")
// when(System.currentTimeMillis() % 5L) {
// 0L -> vm.modelMap.put(EncTypeKey,"T4")
// 1L -> vm.modelMap.put(EncTypeKey,"T3")
@@ -79,45 +77,48 @@ class UserController {
var lResultCode = 0
var lResultMsg = "Suscces"
var u : UserDetails? = null
val decodedBytes: ByteArray = Base64.getDecoder().decode(jsonString)
String(decodedBytes).let {
Gson().fromJson<RequestModel>(it,RequestModel::class.java)?.let { model ->
logService.log(Gson().toJson(model))
model.data?.let { jsonString ->
try {
val originDataString = model.extractData()
logService.log(originDataString)
val target = Gson().fromJson(originDataString, User::class.java) ?: User()
var user = userManager.findById(target.user_id!!)?.block()
if (user == null && ((target.user_id?.length ?: 0) > 3 == true)) {
user = userManager.findByEmail(target.user_id!!)?.block()
}
if (user != null) {
if(userManager.isCorrectUser(user,target.user_pw!!)){
} else {
lResultMsg = "is wrong infomation id or passord"
lResultCode = 7100
}
} else {
lResultMsg = "not founding user[can't find same id,email.. ]"
lResultCode = 7100
}
} catch (e: Exception) {
e.printStackTrace()
lResultMsg = "unknown exception"
lResultCode = 7999
}
jsonString.extractModelData { exception, originDataString ->
if (exception == null) {
logService.log(originDataString)
val target = Gson().fromJson(originDataString, User::class.java) ?: User()
var user = userManager.findById(target.user_id!!)?.block()
if (user == null && ((target.user_id?.length ?: 0) > 3 == true)) {
user = userManager.findByEmail(target.user_id!!)?.block()
}
if (user != null) {
if(userManager.isCorrectUser(user,target.user_pw!!)){
} else {
lResultMsg = "is wrong infomation id or passord"
lResultCode = 7100
}
} else {
lResultMsg = "not founding user[can't find same id,email.. ]"
lResultCode = 7100
}
} else {
exception.printStackTrace()
lResultMsg = exception.message ?: "unknown exception"
lResultCode = 7999
}
}
val responce = ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(ResponceResult().apply {
val responce = ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).headers {
it.put("S33-DATA", listOf(UUID.randomUUID().toString()))
}.body(ResponceResult().apply {
this.resultCode = lResultCode
this.resultMsg = lResultMsg
})
return responce
}
@ResponseBody
@PostMapping("logout.ajax")
fun logout(httpServletRequest: HttpServletRequest) : ResponseEntity<ResponceResult> {
val responce = ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(ResponceResult().apply {
})
return responce
}
@ResponseBody
@PostMapping("joinUser.ajax")
@@ -127,33 +128,25 @@ class UserController {
var lResultCode = 0
var lResultMsg = "Suscces"
var u : User? = null
val decodedBytes: ByteArray = Base64.getDecoder().decode(jsonString)
String(decodedBytes).let {
Gson().fromJson<RequestModel>(it,RequestModel::class.java)?.let { model ->
logService.log(Gson().toJson(model))
model.data?.let { jsonString ->
try {
val originDataString = model.extractData()
logService.log(originDataString)
val user = Gson().fromJson(originDataString, User::class.java) ?: User()
if (user.checkValid() == false) {
lResultCode = 7009
lResultMsg = "user insert Fail Reason : Not Correct Data"
}else if (userManager.findById(user!!.user_id!!)?.block() != null) {
lResultCode = 7001
lResultMsg = "user insert Fail Reason : already has Same Id"
}else if (userManager.findByEmail(user!!.user_email!!)?.block() != null ) {
lResultCode = 7002
lResultMsg = "user insert Fail Reason : already has Same Email"
} else {
u = userManager.save(user).block()
}
} catch (e: Exception) {
e.printStackTrace()
lResultMsg = "unknown exception"
lResultCode = 7999
}
jsonString.extractModelData { exception, s ->
if (exception == null) {
val user = Gson().fromJson(s, User::class.java) ?: User()
if (user.checkValid() == false) {
lResultCode = 7009
lResultMsg = "user insert Fail Reason : Not Correct Data"
}else if (userManager.findById(user!!.user_id!!)?.block() != null) {
lResultCode = 7001
lResultMsg = "user insert Fail Reason : already has Same Id"
}else if (userManager.findByEmail(user!!.user_email!!)?.block() != null ) {
lResultCode = 7002
lResultMsg = "user insert Fail Reason : already has Same Email"
} else {
u = userManager.save(user).block()
}
} else {
exception.printStackTrace()
lResultMsg = exception.message ?: "unknown exception"
lResultCode = 7999
}
}
val responce = ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(ResponceResult().apply {
@@ -0,0 +1,7 @@
package kr.lunaticbum.back.lun.model
open class BaseResult {
var resultCode = 0
var resultMsg = "Suscces"
var isOk = true
}
@@ -4,9 +4,8 @@ import lombok.Getter
@Getter
open class ResponceResult {
var resultCode: Int = 0
var resultMsg: String? = null
open class ResponceResult : BaseResult() {
}
@@ -0,0 +1,26 @@
package kr.lunaticbum.back.lun.model
import org.springframework.http.HttpStatusCode
import org.springframework.web.servlet.ModelAndView
import org.springframework.web.servlet.View
class ResultMV : ModelAndView {
constructor() : super()
constructor(viewName: String) : super(viewName)
constructor(view: View) : super(view)
constructor(viewName: String, model: MutableMap<String, *>?) : super(viewName, model)
constructor(view: View, model: MutableMap<String, *>?) : super(view, model)
constructor(viewName: String, status: HttpStatusCode) : super(viewName, status)
constructor(viewName: String?, model: MutableMap<String, *>?, status: HttpStatusCode?) : super(viewName, model, status)
constructor(viewName: String, modelName: String, modelObject: Any) : super(viewName, modelName, modelObject)
constructor(view: View, modelName: String, modelObject: Any) : super(view, modelName, modelObject)
init {
modelMap.put("title", "LUNATICBUM")
}
fun setTitle(title : String){
modelMap.put("title", title)
}
}
@@ -1,4 +1,9 @@
//package kr.lunaticbum.back.lun.utils
package kr.lunaticbum.back.lun.utils
import com.google.gson.Gson
import kr.lunaticbum.back.lun.model.RequestModel
import java.util.Base64
//
//
//import javax.crypto.Cipher
@@ -49,4 +54,31 @@
// //Decode Base64
//// byte[] decodeByte = Base64.decodeBase64(encodeText);
// return String(c.doFinal(decodeByte), charset("UTF-8"))
//}
//}
fun String.plainText() = String(Base64.getMimeDecoder().decode(this))
fun String.extractModelData(calback : (Exception?,String)->Unit) {
try {
val decodedBytes: ByteArray = Base64.getDecoder().decode(this)
String(decodedBytes).let { resultString ->
try {
Gson().fromJson<RequestModel>(resultString, RequestModel::class.java).let { model ->
model.data?.let { jsonString ->
try {
calback.invoke(null,model.extractData())
} catch (e: Exception) {
calback.invoke(ExtractDataRequestModelException("Exception on extractData with ${Gson().toJson(model)}", e.cause), jsonString)
}
}
}
} catch (e: Exception) {
calback.invoke(MakeRequestModelException("Exception on make RequestModel with $resultString", e.cause),this@extractModelData)
}
}
} catch (e: Exception) {
calback.invoke(Base64DecodeException("Exception on Base64 decode", e.cause),this@extractModelData)
}
}
class Base64DecodeException(message: String, cause : Throwable? = null) : Exception(message, cause)
class MakeRequestModelException(message: String, cause : Throwable? = null) : Exception(message, cause)
class ExtractDataRequestModelException(message: String, cause : Throwable? = null) : Exception(message, cause)