......
This commit is contained in:
@@ -4,6 +4,7 @@ import com.google.gson.Gson
|
||||
import jakarta.servlet.http.Cookie
|
||||
import jakarta.servlet.http.HttpServletRequest
|
||||
import jakarta.servlet.http.HttpServletResponse
|
||||
import kr.lunaticbum.back.lun.model.UserManager
|
||||
import kr.lunaticbum.back.lun.service.JwtService
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.lang.Nullable
|
||||
@@ -19,14 +20,19 @@ class BumsInterceptor : HandlerInterceptor {
|
||||
lateinit var jwtService : JwtService
|
||||
@Autowired
|
||||
lateinit var globalEvv : GlobalEnvironment
|
||||
@Autowired
|
||||
lateinit var userManager: UserManager
|
||||
|
||||
val WRITE_PERMISSION_KEY = "PERMISSION"
|
||||
|
||||
@Throws(Exception::class)
|
||||
override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean {
|
||||
println("===============================================")
|
||||
println("==================== BEGIN ====================")
|
||||
println("Request URL ===> " + request.requestURL)
|
||||
var skippResourcesExtension = arrayListOf(".js",".css").filter { request.requestURI.contains(it) }.size > 0
|
||||
if (!skippResourcesExtension) {
|
||||
println("===============================================")
|
||||
println("==================== BEGIN ====================")
|
||||
println("Request URL ===> " + request.requestURL)
|
||||
}
|
||||
return super.preHandle(request, response, handler)
|
||||
}
|
||||
|
||||
@@ -38,52 +44,75 @@ class BumsInterceptor : HandlerInterceptor {
|
||||
handler: Any,
|
||||
@Nullable modelAndView: ModelAndView?
|
||||
) {
|
||||
if (request.requestURI.contains("logout") == false && !request.cookies.isNullOrEmpty() && request.cookies.filter { it.name.equals("access") && it.value.length > 0 }.size > 0) {
|
||||
var correctUserCheck = -1;
|
||||
var access : Cookie?= null
|
||||
var refresh : Cookie?= null
|
||||
request.cookies.forEach {
|
||||
if (it.name.equals("access", true) && jwtService.validateAccessToken(it.value)){
|
||||
access = it
|
||||
correctUserCheck += 1
|
||||
println("Response access correctUserCheck ===> ${correctUserCheck}")
|
||||
var skippResourcesExtension = arrayListOf(".js",".css").filter { request.requestURI.contains(it) }.size > 0
|
||||
if (!skippResourcesExtension) {
|
||||
if (request.requestURI.contains("logout") == false && !request.cookies.isNullOrEmpty() && request.cookies.filter {
|
||||
it.name.equals(
|
||||
"access"
|
||||
) && it.value.length > 0
|
||||
}.size > 0) {
|
||||
var refreshOk = false;
|
||||
var accessOk = false;
|
||||
var access: Cookie? = null
|
||||
var refresh: Cookie? = null
|
||||
request.cookies.forEach {
|
||||
if (it.name.equals("access", true) && jwtService.validateAccessToken(it.value)) {
|
||||
access = it
|
||||
accessOk = true
|
||||
println("==================== accessOk ${accessOk} ======================")
|
||||
}
|
||||
}
|
||||
}
|
||||
request.cookies.forEach {
|
||||
if (it.name.equals("refresh", true) && jwtService.validateRefreshToken(access?.value,it.value)){
|
||||
refresh = it
|
||||
correctUserCheck += 1
|
||||
println("Response refresh correctUserCheck ===> ${correctUserCheck}")
|
||||
request.cookies.forEach {
|
||||
if (it.name.equals("refresh", true) && jwtService.validateRefreshToken(access?.value, it.value)) {
|
||||
refresh = it
|
||||
refreshOk = true
|
||||
println("==================== refreshOk ${refreshOk} ======================")
|
||||
}
|
||||
}
|
||||
}
|
||||
if (correctUserCheck > 0) {
|
||||
println("Response correctUserCheck ===> ${correctUserCheck}")
|
||||
response.addCookie(cookieUpdate(refresh))
|
||||
response.addCookie(cookieUpdate(access))
|
||||
modelAndView?.modelMap?.put(WRITE_PERMISSION_KEY,"OK")
|
||||
modelAndView?.modelMap?.put("user_id", jwtService.getUserIdFromToken(access?.value ?: ""))
|
||||
} else {
|
||||
println("Response correctUserCheck ===> ${correctUserCheck}")
|
||||
response.addCookie(Cookie("access","").apply { maxAge = -1 })
|
||||
response.addCookie(Cookie("refresh","").apply { maxAge = -1 })
|
||||
modelAndView?.modelMap?.put(WRITE_PERMISSION_KEY,"NO")
|
||||
if (refreshOk || accessOk) {
|
||||
if (refreshOk) {
|
||||
if (!accessOk) {
|
||||
refresh?.let { refresh ->
|
||||
jwtService.getUserIdFromRefresh(refresh.value)?.let { userId ->
|
||||
userManager.findById(userId)?.block()?.let { user ->
|
||||
jwtService.generate(user)?.let { token ->
|
||||
response.addCookie(cookieUpdate(Cookie("access", token.tokenKey)))
|
||||
response.addCookie(cookieUpdate(Cookie("refresh", token.refreshToken)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
response.addCookie(cookieUpdate(access))
|
||||
}
|
||||
} else if (accessOk) {
|
||||
response.addCookie(cookieUpdate(access))
|
||||
}
|
||||
modelAndView?.modelMap?.put(WRITE_PERMISSION_KEY, "OK")
|
||||
modelAndView?.modelMap?.put("user_id", jwtService.getUserIdFromToken(access?.value ?: ""))
|
||||
} else {
|
||||
println("==================== accessOk ${accessOk} && refreshOk ${refreshOk} ======================")
|
||||
response.addCookie(Cookie("access", "").apply { maxAge = -1 })
|
||||
response.addCookie(Cookie("refresh", "").apply { maxAge = -1 })
|
||||
modelAndView?.modelMap?.put(WRITE_PERMISSION_KEY, "NO")
|
||||
modelAndView?.modelMap?.put("user_id", "")
|
||||
}
|
||||
println("Response modelMap ===> ${Gson().toJson(modelAndView?.modelMap)}")
|
||||
} else if (request.requestURI.contains("logout")) {
|
||||
modelAndView?.modelMap?.put(WRITE_PERMISSION_KEY, "NO")
|
||||
modelAndView?.modelMap?.put("user_id", "")
|
||||
}
|
||||
println("Response modelMap ===> ${Gson().toJson(modelAndView?.modelMap)}")
|
||||
} else if (request.requestURI.contains("logout")) {
|
||||
modelAndView?.modelMap?.put(WRITE_PERMISSION_KEY,"NO")
|
||||
modelAndView?.modelMap?.put("user_id", "")
|
||||
|
||||
println("==================== END ======================")
|
||||
println("===============================================")
|
||||
}
|
||||
|
||||
println("==================== END ======================")
|
||||
println("===============================================")
|
||||
|
||||
super.postHandle(request, response, handler, modelAndView)
|
||||
}
|
||||
fun cookieUpdate(cookie: Cookie?) : Cookie? {
|
||||
cookie?.maxAge = (globalEvv.ACCESS_EXPIRATION / 1000).toInt()
|
||||
cookie?.domain = "lunaticbum.kr"
|
||||
cookie?.secure = true
|
||||
cookie?.path = "/"
|
||||
return cookie
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,7 @@ class GlobalEnvironment : EnvironmentAware {
|
||||
// @Value("jwt.access-expiration")
|
||||
var ACCESS_EXPIRATION: Long = 60 * 5 * 1000L
|
||||
// @Value("jwt.refresh-expiration")
|
||||
var REFRESH_EXPIRATION: Long = 60 * 5 * 1000L
|
||||
var REFRESH_EXPIRATION: Long = 60 * 30 * 1000L
|
||||
|
||||
override fun setEnvironment(environment: Environment) {
|
||||
println ("telegramBotKey $telegramBotKey")
|
||||
|
||||
@@ -37,6 +37,7 @@ class JwtGenerator {
|
||||
.compact()
|
||||
}
|
||||
|
||||
|
||||
private fun createHeader(): Map<String, Any> {
|
||||
val header: MutableMap<String, Any> = HashMap()
|
||||
header["typ"] = "JWT"
|
||||
|
||||
@@ -10,6 +10,7 @@ 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.*
|
||||
import kr.lunaticbum.back.lun.service.JwtService
|
||||
import kr.lunaticbum.back.lun.utils.LogService
|
||||
import kr.lunaticbum.back.lun.utils.getFileExtension
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
@@ -135,22 +136,20 @@ class BlogController() {
|
||||
return vm
|
||||
}
|
||||
|
||||
@Autowired
|
||||
lateinit var jwtService : JwtService
|
||||
|
||||
@GetMapping("modify")
|
||||
fun modify(httpServletRequest: HttpServletRequest,@RequestParam("token") token : String?) : ResultMV{
|
||||
fun modify(httpServletRequest: HttpServletRequest, @RequestParam("token") token : String?) : ResultMV{
|
||||
logService.log("incoming modify")
|
||||
val vm = ResultMV("content/blog/modify")
|
||||
var s33Key : String? = null
|
||||
if (!httpServletRequest.cookies.isNullOrEmpty()) {
|
||||
httpServletRequest.cookies.forEach { if (it.name.equals("S33-DATA")){
|
||||
s33Key = it.value
|
||||
} }
|
||||
}
|
||||
if (TEMPTOKEN.equals(token)|| s33Key?.length ?: 0 > 5) {
|
||||
if(jwtService.hasPerrmission(httpServletRequest)) {
|
||||
postManageg.find20()?.apply {
|
||||
forEach {
|
||||
it.title = URLDecoder.decode(it.title)
|
||||
val content = URLDecoder.decode(it.content)
|
||||
it.content = if (content.length > 50) content.substring(0,50) else content
|
||||
it.content = if (content.length > 50) content.substring(0,150) else content
|
||||
}
|
||||
vm.modelMap.put("chunkedPosts", this.chunked(3))
|
||||
}
|
||||
|
||||
@@ -27,10 +27,9 @@ class TokenData {
|
||||
var refreshToken : String? = null
|
||||
|
||||
constructor(tokenKey: String?, refreshToken: String?) {
|
||||
this.expireAt = LocalDateTime.now().plusSeconds(300)
|
||||
this.expireAt = LocalDateTime.now().plusSeconds(500)
|
||||
this.tokenKey = tokenKey
|
||||
this.refreshToken = refreshToken
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package kr.lunaticbum.back.lun.service
|
||||
import io.jsonwebtoken.Claims
|
||||
import io.jsonwebtoken.Jws
|
||||
import io.jsonwebtoken.Jwts
|
||||
import jakarta.servlet.http.Cookie
|
||||
import jakarta.servlet.http.HttpServletRequest
|
||||
import jakarta.servlet.http.HttpServletResponse
|
||||
import kr.lunaticbum.back.lun.configs.GlobalEnvironment
|
||||
@@ -68,7 +69,6 @@ class JwtService {
|
||||
}
|
||||
|
||||
|
||||
|
||||
private fun setTokenToCookie(tokenPrefix: String, token: String, maxAgeSeconds: Long): ResponseCookie {
|
||||
return ResponseCookie.from(tokenPrefix, token)
|
||||
.path("/")
|
||||
@@ -139,4 +139,42 @@ class JwtService {
|
||||
return null
|
||||
}
|
||||
}
|
||||
fun getUserIdFromRefresh(token: String?): String? {
|
||||
try {
|
||||
return jwtUtil.extractToken(token,jwtUtil.getSigningKey(globalEvv.REFRESH_SECRET_KEY))?.body?.get("Identifier")
|
||||
.toString()
|
||||
} catch (e: Exception) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
fun hasPerrmission(request: HttpServletRequest): Boolean {
|
||||
var correctUserCheck = -1;
|
||||
if (request.requestURI.contains("logout") == false && !request.cookies.isNullOrEmpty() && request.cookies.filter { it.name.equals("access") && it.value.length > 0 }.size > 0) {
|
||||
var access : Cookie?= null
|
||||
var refresh : Cookie?= null
|
||||
request.cookies.forEach {
|
||||
if (it.name.equals("access", true) && validateAccessToken(it.value)){
|
||||
access = it
|
||||
correctUserCheck += 1
|
||||
}
|
||||
}
|
||||
request.cookies.forEach {
|
||||
if (it.name.equals("refresh", true) && validateRefreshToken(access?.value,it.value)){
|
||||
refresh = it
|
||||
correctUserCheck += 1
|
||||
}
|
||||
}
|
||||
if (correctUserCheck > 0) {
|
||||
println("Response correctUserCheck ===> ${correctUserCheck}")
|
||||
} else {
|
||||
println("Response correctUserCheck ===> ${correctUserCheck}")
|
||||
}
|
||||
} else if (request.requestURI.contains("logout")) {
|
||||
|
||||
}
|
||||
return correctUserCheck > 0
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user