Skip to content

Commit

Permalink
Modify UsernameNotFoundException Error handling (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
inh2613 authored Oct 22, 2023
2 parents d48f87b + d7bbcf1 commit 69128bf
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public OAuthUserInfoDto mapToDto(OAuth oAuth) {
.id(oAuth.getPlatformId())
.email(oAuth.getEmail())
.nickname(oAuth.getNickname())
.Provider(oAuth.getPlatform().toString())
.Provider(oAuth.getPlatform().toString().toLowerCase())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ public boolean isValidatePassword(String password) {
public User read(String username) {
User user = userRepository.findByUsername(username);
if (user == null) {
return null;
throw new BusinessException("존재하지 않는 회원입니다.", StatusEnum.NOT_FOUND);
}
return user;
}

public UserReadResponseDto read(Long id) {
Optional<User> user = userRepository.findById(id);
if (user.isEmpty() || user.get().getDeletedAt() != null) {
if (user.isEmpty() || !user.get().isEnabled()) {
throw new BusinessException("존재하지 않는 회원입니다.", StatusEnum.NOT_FOUND);
}
return UserReadResponseDto.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,54 @@
package org.swmaestro.repl.gifthub.filter;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import java.io.IOException;

import org.springframework.core.annotation.Order;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import org.swmaestro.repl.gifthub.util.ErrorMessage;
import org.swmaestro.repl.gifthub.util.JwtProvider;
import org.swmaestro.repl.gifthub.util.StatusEnum;

import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;

@Order(0)
@Component
@RequiredArgsConstructor
public class JwtAuthenticationFilter extends OncePerRequestFilter {
private final JwtProvider jwtProvider;
private final ObjectMapper objectMapper = new ObjectMapper();

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException,
UsernameNotFoundException {
String token = jwtProvider.resolveToken(request);
if (token != null && jwtProvider.validateToken(token)) {
token = token.substring(7);
Authentication auth = jwtProvider.getAuthentication(token);
SecurityContextHolder.getContext().setAuthentication(auth);
try {
Authentication auth = jwtProvider.getAuthentication(token);
SecurityContextHolder.getContext().setAuthentication(auth);
} catch (UsernameNotFoundException e) {
e.printStackTrace();
response.setStatus(401);
response.setCharacterEncoding("utf-8");
response.setContentType("application/json");
response.getWriter().write(objectMapper.writeValueAsString(
ErrorMessage.builder()
.status(StatusEnum.UNAUTHORIZED.statusCode)
.path(request.getRequestURI())
.error("탈퇴한 회원입니다.")
.build()));
return;
}
}
filterChain.doFilter(request, response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.swmaestro.repl.gifthub.auth.repository.RefreshTokenRepository;
import org.swmaestro.repl.gifthub.auth.service.UserService;
Expand Down Expand Up @@ -96,6 +97,9 @@ public String resolveToken(HttpServletRequest request) {
*/
public Authentication getAuthentication(String token) {
UserDetails userDetails = userService.loadUserByUsername(this.getUsername(token));
if (userDetails == null) {
throw new UsernameNotFoundException("존재하지 않는 회원입니다.");
}
return new UsernamePasswordAuthenticationToken(userDetails, "", userDetails.getAuthorities());
}

Expand Down

0 comments on commit 69128bf

Please sign in to comment.