This repository was archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
스프링 시큐리티를 이용한 api 사용 인증 #68
Open
kmmin78
wants to merge
3
commits into
develop
Choose a base branch
from
feature/kms/LoginWithSpringSecurityAndJWT/005
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...ain/src/main/kotlin/kr/flab/wiki/app/components/authentication/JwsAuthenticationFilter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package kr.flab.wiki.app.components.authentication | ||
|
||
import io.jsonwebtoken.Claims | ||
import io.jsonwebtoken.Jws | ||
import kr.flab.wiki.app.utils.JwtUtils | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken | ||
import org.springframework.security.core.context.SecurityContextHolder | ||
import org.springframework.web.filter.OncePerRequestFilter | ||
import javax.servlet.FilterChain | ||
import javax.servlet.http.HttpServletRequest | ||
import javax.servlet.http.HttpServletResponse | ||
|
||
class JwsAuthenticationFilter : OncePerRequestFilter() { | ||
override fun doFilterInternal( | ||
request: HttpServletRequest, | ||
response: HttpServletResponse, | ||
filterChain: FilterChain | ||
) { | ||
val jws: String? = JwtUtils.parseJws(request.getHeader("Authorization") ?: "") | ||
var result: Jws<Claims>? | ||
if (jws != null) { | ||
result = JwtUtils.validateJws(jws) | ||
if (result != null) { | ||
val email = JwtUtils.getEmailFromJws(jws) | ||
SecurityContextHolder.getContext().authentication = UsernamePasswordAuthenticationToken(email, null) | ||
} | ||
} | ||
filterChain.doFilter(request, response) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...main/src/main/kotlin/kr/flab/wiki/app/components/authentication/UnauthorizedEntryPoint.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package kr.flab.wiki.app.components.authentication | ||
|
||
import org.springframework.security.core.AuthenticationException | ||
import org.springframework.security.web.AuthenticationEntryPoint | ||
import org.springframework.stereotype.Component | ||
import javax.servlet.http.HttpServletRequest | ||
import javax.servlet.http.HttpServletResponse | ||
|
||
@Component | ||
class UnauthorizedEntryPoint : AuthenticationEntryPoint { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 요거 네이밍 좋습니다. |
||
override fun commence( | ||
request: HttpServletRequest?, | ||
response: HttpServletResponse?, | ||
authException: AuthenticationException? | ||
) { | ||
response?.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Access Denied") | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
app-main/src/test/kotlin/kr/flab/wiki/app/testlib/LoginTestHelper.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package kr.flab.wiki.app.testlib | ||
|
||
import io.restassured.RestAssured | ||
import io.restassured.specification.RequestSpecification | ||
import kr.flab.wiki.app.api.user.request.LoginRequest | ||
import kr.flab.wiki.app.api.user.response.LoginResponse | ||
|
||
object LoginTestHelper { | ||
|
||
fun makeLoginResponse(requestSpecification: RequestSpecification, email: String, password: String): LoginResponse { | ||
return RestAssured | ||
.given() | ||
.spec(requestSpecification) | ||
.body(LoginRequest(email, password)) | ||
.`when`() | ||
.post("/login") | ||
.then() | ||
.extract() | ||
.`as`(LoginResponse::class.java) | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
서버 실행할때 jackson-kotlin 어쩌고 경고 뜨진 않던가요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
서버 시작을 아직 안해봤는데, 확인해보고 말씀드리겠습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
음.. 로컬에 wsl, docker, mysql 세팅하고 서버실행해봤는데, 말씀하신 경고는 뜨지 않고 있습니다.