-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: LoginService와 로그인 성공, 실패 핸들러 추가 (#4)
- Loading branch information
1 parent
ac32aa5
commit 6310426
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/main/java/com/project/mapdagu/domain/auth/handler/LoginFailureHandler.java
This file contains 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,23 @@ | ||
package com.project.mapdagu.domain.auth.handler; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler; | ||
|
||
import java.io.IOException; | ||
|
||
@Slf4j | ||
public class LoginFailureHandler extends SimpleUrlAuthenticationFailureHandler { | ||
|
||
@Override | ||
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, | ||
AuthenticationException exception) throws IOException { | ||
response.setStatus(HttpServletResponse.SC_BAD_REQUEST); | ||
response.setCharacterEncoding("UTF-8"); | ||
response.setContentType("text/plain;charset=UTF-8"); | ||
response.getWriter().write("로그인 실패! 이메일이나 비밀번호를 확인해주세요."); | ||
log.info("로그인에 실패했습니다. 메시지 : {}", exception.getMessage()); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/project/mapdagu/domain/auth/handler/LoginSuccessHandler.java
This file contains 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,43 @@ | ||
package com.project.mapdagu.domain.auth.handler; | ||
|
||
import com.project.mapdagu.domain.member.repository.MemberRepository; | ||
import com.project.mapdagu.jwt.service.JwtService; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class LoginSuccessHandler extends SimpleUrlAuthenticationSuccessHandler { | ||
|
||
private final JwtService jwtService; | ||
private final MemberRepository memberRepository; | ||
|
||
@Value("${jwt.access.expiration}") | ||
private String accessTokenExpiration; | ||
|
||
@Override | ||
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, | ||
Authentication authentication) { | ||
String email = extractUsername(authentication); | ||
String accessToken = jwtService.createAccessToken(email); | ||
String refreshToken = jwtService.createRefreshToken(email); | ||
|
||
jwtService.sendAccessAndRefreshToken(response, accessToken, refreshToken); | ||
jwtService.updateRefreshToken(email, refreshToken); | ||
log.info("로그인에 성공하였습니다. 이메일 : {}", email); | ||
|
||
log.info("로그인에 성공하였습니다. AccessToken : {}", accessToken); | ||
log.info("발급된 AccessToken 만료 기간 : {}", accessTokenExpiration); | ||
} | ||
|
||
private String extractUsername(Authentication authentication) { | ||
UserDetails userDetails = (UserDetails) authentication.getPrincipal(); | ||
return userDetails.getUsername(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/project/mapdagu/domain/auth/service/LoginService.java
This file contains 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,31 @@ | ||
package com.project.mapdagu.domain.auth.service; | ||
|
||
import com.project.mapdagu.domain.member.entity.Member; | ||
import com.project.mapdagu.domain.member.repository.MemberRepository; | ||
import com.project.mapdagu.error.ErrorCode; | ||
import com.project.mapdagu.error.exception.custom.BusinessException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.stereotype.Service; | ||
|
||
import static com.project.mapdagu.error.ErrorCode.*; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class LoginService implements UserDetailsService { | ||
|
||
private final MemberRepository memberRepository; | ||
|
||
@Override | ||
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException { | ||
Member member = memberRepository.findByEmail(email).orElseThrow(() -> new BusinessException(MEMBER_NOT_FOUND)); | ||
|
||
return org.springframework.security.core.userdetails.User.builder() | ||
.username(member.getEmail()) | ||
.password(member.getPassword()) | ||
.roles(member.getRole().name()) | ||
.build(); | ||
} | ||
} |