Skip to content

Commit

Permalink
feat: LoginService와 로그인 성공, 실패 핸들러 추가 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyeong-hyeok committed Aug 3, 2023
1 parent ac32aa5 commit 6310426
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
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());
}
}
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();
}
}
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();
}
}

0 comments on commit 6310426

Please sign in to comment.