티스토리 뷰
3편은 Provider에서 인증 성공 / 실패 판단을하고 그 후 처리를 어떻게할지 Custom한
Success , Failuer Handler에 대해 작성하려한다.
1) SuccessHandler
2편에서 작성한 Provider을보면
if(user?.userpw.equals(userpw)) {
val authorities = listOf(SimpleGrantedAuthority("ROLE_USER"))
return UsernamePasswordAuthenticationToken(userid,userpw,authorities)
}
해당 ID의 PW값과 입력한PW값을 비교해 일치하면
UsernamePasswordAuthenticationToken을 반환하고, 인증이 성공한것으로 간주되어
SuccessHandler가 실행된다.
@Component
class EkeprlSuccessHandler : AuthenticationSuccessHandler{
override fun onAuthenticationSuccess(
request: HttpServletRequest?,
response: HttpServletResponse?,
authentication: Authentication?
) {
val flashMap = FlashMap()
val redirectUrl = "/main"
val logger = LoggerFactory.getLogger(this::class.java)
logger.info("CustomSuccessHandler")
flashMap["MESSAGE"] = "성공적으로 로그인 되었습니다."
val flashMapManager = SessionFlashMapManager()
if (request != null && response != null) {
flashMapManager.saveOutputFlashMap(flashMap, request, response)
}
response?.sendRedirect(redirectUrl)
}
}
**블로그의 다른글에서 FlashMap["MESSAGE"]에 관해 다룬것이있어 참고바랍니다.**
인증에 성공할 경우 redirectUrl을 이용해 반환값을 넣어준다.
2) Failuer Handler
@Component
class EkeprlFailuerHandler : AuthenticationFailureHandler{
override fun onAuthenticationFailure(
request: HttpServletRequest,
response: HttpServletResponse,
exception: AuthenticationException
) {
val flashMap = FlashMap()
var redirectUrl: String?
val logger = LoggerFactory.getLogger(this::class.java)
when(exception) {
is BadCredentialsException -> {
flashMap["MESSAGE"] = "아이디와 비밀번호를 확인해주세요"
logger.info("FlashMap MESSAGE: " + flashMap.get("MESSAGE"));
redirectUrl= "/login"
}else -> {
flashMap["MESSAGE"] = "오류가 발생했습니다. 문의해주세요"
redirectUrl= "/login"
}
}
val flashMapManager = SessionFlashMapManager()
flashMapManager.saveOutputFlashMap(flashMap, request, response)
response.sendRedirect(redirectUrl)
}
}
전편에서 입력한 pw와 db상의 pw를 비교하여 일치하지 않을 시
BadCredentialException을 발생시키고
발생한 후 처리를 FailuerHandler에서 처리하도록했다.
When절이 예외 발생 시 처리하는 부분이다.
비밀번호가 올바르지 않을때 BadCredentialException이 발생하기 때문에 flashMap 메시지를 설정했고,
/login페이지로 리다이렉트하도록 작성했다.
또한 pw불일치 뿐아니라 다른 예외가 발생할 수 있어
"else ->" 부분을 작성했다.
우선은 PW의 값을 비교해 BadCredentialException만을 대상으로 Handler를 작성했다.
추후에 다른 오류까지 같이 처리하도록 수정할 예정이다.
반응형
'Spring > Security(session)' 카테고리의 다른 글
| Spring Security (6) : BCryptPasswordEncoder 로그인 (2) | 2025.03.05 |
|---|---|
| Spring Security (5) : 비밀번호 암호화 저장 (0) | 2025.03.05 |
| Spring Security (4) : session 값 저장 (0) | 2025.01.16 |
| Spring Security (2) (0) | 2024.10.14 |
| Spring Security (1) (5) | 2024.10.14 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- kotlin
- 특일정보
- null
- GitHub
- 명령줄이 너무 깁니다
- session저장
- CRUD
- Insert오류
- JAR매니패스트
- 게시판
- securityconfig
- ubuntu
- spring ai
- Git
- Powershell
- dependencies
- springsecurity
- 의존성
- insert
- Postman
- 백엔드
- 서버호스트
- dbeaver
- AI
- Column count doesn't match value count at row 1
- Spring Security
- InetAddress
- MariaDB
- gradle
- jwt
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 |
글 보관함
