Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify UsernameNotFoundException Error handling #134

Merged
merged 5 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -74,14 +74,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