spring boot 3.1 security entryPoint 관련 질문입니다
import com.tripcoach.app_api.api.member.repository.MemberRepository; import com.tripcoach.app_api.security.token.CustomAuthenticationToken; import com.tripcoach.core.domain.member.entity.Member; import com.tripcoach.core.domain.member.enums.OsType; import com.tripcoach.core.domain.member.enums.SnsType; import com.tripcoach.core.common.exception.exception.BusinessException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.transaction.annotation.Transactional; import static com.tripcoach.core.common.apiresult.comcode.ComCode.LOGIN_FAIL; public class CustomAuthenticationManager implements AuthenticationManager { @Autowired private UserDetailsService userDetailsService; @Autowired private MemberRepository memberRepository; @Override @Transactional public Authentication authenticate(Authentication authentication) throws AuthenticationException { CustomAuthenticationToken customAuthenticationToken = (CustomAuthenticationToken) authentication; System.out.println("customAuthenticationToken.getSnsType() = " + customAuthenticationToken.getSnsType()); String snsId = customAuthenticationToken.getSnsId(); SnsType snsType = SnsType.valueOf(customAuthenticationToken.getSnsType()); String fcmToken = customAuthenticationToken.getFcmToken(); OsType osType = OsType.valueOf(customAuthenticationToken.getOsType()); Member member = memberRepository.findBySnsIdAndSnsType(snsId, snsType) .orElseThrow(() -> new BadCredentialsException(LOGIN_FAIL)); member.updateFcmToken(fcmToken); member.updateOsType(osType); UserDetails userDetails = userDetailsService.loadUserByUsername(String.valueOf(member.getIdx())); if (userDetails == null) { throw new BusinessException("로그인에 실패 했습니다."); } return new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()); } } import com.tripcoach.core.common.exception.exception.BusinessException; import com.tripcoach.core.domain.member.entity.Member; import io.jsonwebtoken.*; import jakarta.servlet.http.HttpServletRequest; import lombok.extern.slf4j.Slf4j; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.stereotype.Component; import java.io.Serializable; import java.util.ArrayList; import java.util.Date; import static com.tripcoach.app_api.security.config.SecurityCode.*; import static java.util.stream.Collectors.toList; @Slf4j @Component public class JwtTokenProvider implements Serializable { private static final String SECRET_KEY = ""; private static final long EXPIRATION_TIME = 1000 * 1800; // 30분초 // private static final long EXPIRATION_TIME = 30 * 24 * 60 * 60 * 1000L; // 1달 // private static final long EXPIRATION_TIME = 300 * 600 * 100L; //300분 /** * Jwt 생성 */ public String generateToken(Member member) { ArrayList roles = new ArrayList(); roles.add(new SimpleGrantedAuthority(member.getGrade().label())); Date now = new Date(); Date expiration = new Date(now.getTime() + EXPIRATION_TIME); return Jwts.builder() .setSubject(member.getIdx().toString()) .claim("authorities", roles.stream().map(GrantedAuthority::getAuthority).collect(toList())) .claim("nickname", member.getNickname()) // 이 부분을 추가하여 nickname 클레임을 설정합니다 .setIssuedAt(now) .setExpiration(expiration) .signWith(SignatureAlgorithm.HS512, SECRET_KEY) .compact(); } // /** // * JWT 회원 정보 추출. // */ public Claims getUsernameFromToken(String token) { try { return Jwts.parserBuilder() .setSigningKey(SECRET_KEY) .build() .parseClaimsJws(token) .getBody(); } catch (ExpiredJwtException e) { throw new ExpiredJwtException(null, null, "토큰이 만료 되었습니다"); } catch (MalformedJwtException e) { throw new BusinessException("잘못된 토큰입니다."); // 토큰이 잘못된 경우 처리할 내용 } } public Claims getUsernameFromToken(String token, HttpServletRequest request) { Claims claims = null; try { claims = Jwts.parserBuilder() .setSigningKey(SECRET_KEY) .build() .parseClaimsJws(token) .getBody(); } catch (ExpiredJwtException e) { request.setAttribute(JWT_EXCEPTION.label(), EXPIRED_JWT_EXCEPTION); } catch (MalformedJwtException e) { request.setAttribute(JWT_EXCEPTION.label(), MALFORMED_JWT_EXCEPTION); } catch (UnsupportedJwtException e) { request.setAttribute(JWT_EXCEPTION.label(), UNSUPPORTED_JWT_EXCEPTION); } catch (SignatureException e) { request.setAttribute(JWT_EXCEPTION.label(), SIGNATURE_EXCEPTION); } catch (IllegalArgumentException e) { request.setAttribute(JWT_EXCEPTION.label(), ILLEGAL_ARGUMENT_EXCEPTION); }catch (NullPointerException e) { request.setAttribute(JWT_EXCEPTION.label(), NULL_POINT_EXCEPTION); } return claims; } } import com.tripcoach.app_api.api.member.repository.MemberRepository; import com.tripcoach.core.domain.member.entity.Member; import lombok.RequiredArgsConstructor; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; 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 java.util.ArrayList; @RequiredArgsConstructor @Service("userDetailService") public class CustomUserDetailsServiceImpl implements UserDetailsService { private final MemberRepository memberRepository; @Override public UserDetails loadUserByUsername(String idx) throws UsernameNotFoundException { Member member = memberRepository.findById(Long.valueOf(idx)) .orElseThrow(() -> new UsernameNotFoundException("User not found with idx: " + idx)); ArrayList roles = new ArrayList(); roles.add(new SimpleGrantedAuthority(member.getGrade().label())); return new MemberContext(member, roles); } } import com.tripcoach.core.domain.member.entity.Member; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.userdetails.User; import java.util.Collection; public class MemberContext extends User { private final Member member; public MemberContext(Member member, Collection authorities) { super(String.valueOf(member.getIdx()), "beeb.tripcoach", authorities); this.member = member; } public Member getMember() { return member; } public Long getId() { return member.getIdx(); } }import lombok.Getter; import org.springframework.security.authentication.AbstractAuthenticationToken; @Getter public class CustomAuthenticationToken extends AbstractAuthenticationToken { private final String snsId; private final String snsType; private final String fcmToken; private final String osType; public CustomAuthenticationToken(String snsId, String snsType, String fcmToken, String osType) { super(null); this.snsId = snsId; this.snsType = snsType; this.fcmToken = fcmToken; this.osType = osType; } @Override public Object getCredentials() { return null; } @Override public Object getPrincipal() { return snsId; } }