feat : HttpServletRequest 의 Header 중 Authorization 값을 검사하는 AuthorizationFilter 추가
This commit is contained in:
@@ -1,31 +0,0 @@
|
|||||||
package com.banjjoknim.playground.config.filter
|
|
||||||
|
|
||||||
import javax.servlet.Filter
|
|
||||||
import javax.servlet.FilterChain
|
|
||||||
import javax.servlet.ServletRequest
|
|
||||||
import javax.servlet.ServletResponse
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @see javax.servlet.Filter
|
|
||||||
* @see com.banjjoknim.playground.config.security.JwtSecurityConfiguration
|
|
||||||
*/
|
|
||||||
class CustomFilter1 : Filter {
|
|
||||||
override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) {
|
|
||||||
println("필터1")
|
|
||||||
chain.doFilter(request, response) // 필터체인에 request, response 를 등록해주어야 한다. 그렇지 않으면 현재 필터가 진행되고 이 이후의 필터들은 더이상 동작하지 않게 된다.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class CustomFilter2 : Filter {
|
|
||||||
override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) {
|
|
||||||
println("필터2")
|
|
||||||
chain.doFilter(request, response) // 필터체인에 request, response 를 등록해주어야 한다. 그렇지 않으면 현재 필터가 진행되고 이 이후의 필터들은 더이상 동작하지 않게 된다.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class CustomFilter3 : Filter {
|
|
||||||
override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) {
|
|
||||||
println("필터3")
|
|
||||||
chain.doFilter(request, response) // 필터체인에 request, response 를 등록해주어야 한다. 그렇지 않으면 현재 필터가 진행되고 이 이후의 필터들은 더이상 동작하지 않게 된다.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package com.banjjoknim.playground.config.filter
|
||||||
|
|
||||||
|
import org.springframework.http.HttpMethod
|
||||||
|
import javax.servlet.Filter
|
||||||
|
import javax.servlet.FilterChain
|
||||||
|
import javax.servlet.ServletRequest
|
||||||
|
import javax.servlet.ServletResponse
|
||||||
|
import javax.servlet.http.HttpServletRequest
|
||||||
|
import javax.servlet.http.HttpServletResponse
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see javax.servlet.Filter
|
||||||
|
* @see com.banjjoknim.playground.config.security.JwtSecurityConfiguration
|
||||||
|
*/
|
||||||
|
class CustomFilter1 : Filter {
|
||||||
|
override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) {
|
||||||
|
println("필터1")
|
||||||
|
chain.doFilter(request, response) // request, response 와 함께 doFilter 를 호출해주어야 한다. 그렇지 않으면 현재 필터가 진행되고나서 이 이후의 필터들은 더이상 동작하지 않게 된다.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class CustomFilter2 : Filter {
|
||||||
|
override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) {
|
||||||
|
println("필터2")
|
||||||
|
chain.doFilter(request, response) // request, response 와 함께 doFilter 를 호출해주어야 한다. 그렇지 않으면 현재 필터가 진행되고나서 이 이후의 필터들은 더이상 동작하지 않게 된다.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class CustomFilter3 : Filter {
|
||||||
|
override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) {
|
||||||
|
println("필터3")
|
||||||
|
chain.doFilter(request, response) // request, response 와 함께 doFilter 를 호출해주어야 한다. 그렇지 않으면 현재 필터가 진행되고나서 이 이후의 필터들은 더이상 동작하지 않게 된다.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class AuthorizationFilter : Filter {
|
||||||
|
override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) {
|
||||||
|
val httpServletRequest = request as HttpServletRequest
|
||||||
|
val httpServletResponse = response as HttpServletResponse
|
||||||
|
|
||||||
|
if (httpServletRequest.method == HttpMethod.POST.name) {
|
||||||
|
println("POST 요청됨")
|
||||||
|
val headerAuthorization = httpServletRequest.getHeader("Authorization")
|
||||||
|
println(headerAuthorization)
|
||||||
|
|
||||||
|
// banjjoknim 이 정상적인 토큰이라고 가정한다.
|
||||||
|
// 따라서 banjjoknim 이라는 토큰을 id, password 가 정상적으로 입력되었을 때 토큰을 만들어주고 응답에 실어보낸다.
|
||||||
|
// 클라이언트는 요청할 때마다 해당 토큰을 Header 중 Authorization 의 값으로 포함하여 요청한다.
|
||||||
|
// 따라서 클라이언트가 요청할 때 Authorization 의 값으로 포함되어 온 토큰이 우리 서버에서 만든 토큰이 맞는지 검증만 하면 된다(RSA, HS256).
|
||||||
|
if (headerAuthorization == "banjjoknim") {
|
||||||
|
chain.doFilter(httpServletRequest, httpServletResponse) // request, response 와 함께 doFilter 를 호출해주어야 한다. 그렇지 않으면 현재 필터가 진행되고나서 이 이후의 필터들은 더이상 동작하지 않게 된다.
|
||||||
|
} else {
|
||||||
|
val printWriter = httpServletResponse.writer
|
||||||
|
printWriter.println("인증안됨") // 응답에 '인증안됨' 을 쓴다.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.banjjoknim.playground.config.security
|
package com.banjjoknim.playground.config.security
|
||||||
|
|
||||||
|
import com.banjjoknim.playground.config.filter.AuthorizationFilter
|
||||||
import com.banjjoknim.playground.config.filter.CustomFilter3
|
import com.banjjoknim.playground.config.filter.CustomFilter3
|
||||||
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
|
||||||
@@ -31,6 +32,7 @@ class JwtSecurityConfiguration(
|
|||||||
|
|
||||||
// 우리가 원하는 위치에 Filter 를 등록한다. 만약 Spring Security Filter 보다도 먼저 실행되게 하고 싶다면 SecurityContextPersistenceFilter 보다 먼저 실행되도록 아래처럼 등록해주면 된다.
|
// 우리가 원하는 위치에 Filter 를 등록한다. 만약 Spring Security Filter 보다도 먼저 실행되게 하고 싶다면 SecurityContextPersistenceFilter 보다 먼저 실행되도록 아래처럼 등록해주면 된다.
|
||||||
http.addFilterBefore(CustomFilter3(), SecurityContextPersistenceFilter::class.java)
|
http.addFilterBefore(CustomFilter3(), SecurityContextPersistenceFilter::class.java)
|
||||||
|
http.addFilterBefore(AuthorizationFilter(), SecurityContextPersistenceFilter::class.java)
|
||||||
|
|
||||||
http.csrf().disable()
|
http.csrf().disable()
|
||||||
// 기본적으로 웹은 STATELESS 인데, STATEFUL 처럼 쓰기 위해서 세션과 쿠키를 만든다. 이때, 그걸(세션과 쿠키) 사용하지 않도록 설정하는 것이다.
|
// 기본적으로 웹은 STATELESS 인데, STATEFUL 처럼 쓰기 위해서 세션과 쿠키를 만든다. 이때, 그걸(세션과 쿠키) 사용하지 않도록 설정하는 것이다.
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.banjjoknim.playground.domain.home
|
package com.banjjoknim.playground.domain.home
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping
|
import org.springframework.web.bind.annotation.GetMapping
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping
|
||||||
import org.springframework.web.bind.annotation.RestController
|
import org.springframework.web.bind.annotation.RestController
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -18,4 +19,9 @@ class HomeApiController {
|
|||||||
fun home(): String {
|
fun home(): String {
|
||||||
return "<h1>home</h1>"
|
return "<h1>home</h1>"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/token")
|
||||||
|
fun token(): String {
|
||||||
|
return "<h1>token</h1>"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user