Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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 @@ -29,7 +29,13 @@ protected void doFilterInternal(HttpServletRequest req,
String email = jwtProvider.getEmail(token);
var authentication = new JwtAuthentication(email);
SecurityContextHolder.getContext().setAuthentication(authentication);
} else {
// 토큰이 유효하지 않으면 Context 클리어
SecurityContextHolder.clearContext();
}
} else {
// Authorization 헤더가 아예 없을 때도 Context 클리어
SecurityContextHolder.clearContext();
}

chain.doFilter(req, res);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
@RequiredArgsConstructor
public class JwtTokenProvider {
private final JwtProperties jwtProperties;
private Key signingKey;

private Key getSigningKey() {
return Keys.hmacShaKeyFor(jwtProperties.getSecret().getBytes());
if (signingKey == null) {
signingKey = Keys.hmacShaKeyFor(jwtProperties.getSecret().getBytes());
}
return signingKey;
}

public String generateToken(String email, String role) {
Expand All @@ -38,7 +42,10 @@ public boolean validateToken(String token) {
}

public String getEmail(String token) {
if (!validateToken(token)) {
throw new IllegalArgumentException("Invalid token");
}
return Jwts.parserBuilder().setSigningKey(getSigningKey()).build()
.parseClaimsJws(token).getBody().getSubject();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.wayble.server.user.dto;

import com.wayble.server.user.entity.LoginType;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;

public record UserLoginRequestDto(
@NotBlank(message = "이름 또는 닉네임은 필수입니다")
Expand All @@ -12,5 +14,8 @@ public record UserLoginRequestDto(
String email,

@NotBlank(message = "비밀번호는 필수입니다")
String password
String password,

@NotNull(message = "로그인 타입은 필수입니다")
LoginType loginType
) {}
7 changes: 5 additions & 2 deletions src/main/java/com/wayble/server/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@SQLDelete(sql = "UPDATE user SET deleted_at = now() WHERE id = ?")
@SQLRestriction("deleted_at IS NULL")
@Table(name = "user")
@Table(
name = "user",
uniqueConstraints = @UniqueConstraint(columnNames = {"email", "login_type"})
)
public class User extends BaseEntity {

@Id
Expand All @@ -30,7 +33,7 @@ public class User extends BaseEntity {

private String username;

@Column(nullable = false, unique = true)
@Column(nullable = false)
private String email;

// TODO: 비밀번호 암호화 필요
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.wayble.server.user.repository;

import com.wayble.server.user.entity.LoginType;
import com.wayble.server.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface UserRepository extends JpaRepository<User, Long> {

Optional<User> findByEmail(String email);

boolean existsByEmail(String email);
boolean existsByEmailAndLoginType(String email, LoginType loginType);
Optional<User> findByEmailAndLoginType(String email, LoginType loginType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class UserService {

// 회원가입
public void signup(UserRegisterRequestDto req) {
if (userRepository.existsByEmail(req.email())) {
if (userRepository.existsByEmailAndLoginType(req.email(), req.loginType())) {
throw new ApplicationException(UserErrorCase.USER_ALREADY_EXISTS);
}
User user = User.createUser(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class AuthService {
private final JwtTokenProvider jwtProvider;

public TokenResponseDto login(UserLoginRequestDto req) {
User user = userRepository.findByEmail(req.email())
User user = userRepository.findByEmailAndLoginType(req.email(), req.loginType())
.orElseThrow(() -> new ApplicationException(UserErrorCase.INVALID_CREDENTIALS));
if (!encoder.matches(req.password(), user.getPassword())) {
throw new ApplicationException(UserErrorCase.INVALID_CREDENTIALS);
Expand Down
Loading