-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[Feature] νν° λ΄μ© μ‘°ν κΈ°λ₯ ꡬν
- Loading branch information
Showing
49 changed files
with
1,151 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/meltingpot/server/auth/controller/AuthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package meltingpot.server.auth.controller; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import jakarta.validation.Valid; | ||
import meltingpot.server.auth.controller.dto.SigninRequestDto; | ||
import meltingpot.server.auth.controller.dto.AccountResponseDto; | ||
import meltingpot.server.util.ResponseCode; | ||
import meltingpot.server.util.ResponseData; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import lombok.RequiredArgsConstructor; | ||
import meltingpot.server.auth.service.AuthService; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Validated | ||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("auth") | ||
public class AuthController { | ||
|
||
private final AuthService authService; | ||
private final Logger logger = LoggerFactory.getLogger(this.getClass()); | ||
|
||
// νμ κ°μ | ||
|
||
|
||
// λ‘κ·ΈμΈ | ||
@PostMapping("signin") | ||
@Operation(summary="λ‘κ·ΈμΈ", description="λ‘κ·ΈμΈ API μ λλ€.") | ||
public ResponseEntity<ResponseData<AccountResponseDto>> signin( | ||
@RequestBody @Valid SigninRequestDto request | ||
){ | ||
AccountResponseDto data = authService.signin(request.toServiceDto()); | ||
logger.info("SIGNIN_SUCCESS (200 OK) :: userId = {}, userEmail = {}", | ||
data.getId(), data.getEmail()); | ||
return ResponseData.toResponseEntity(ResponseCode.SIGNIN_SUCCESS, data); | ||
} | ||
|
||
|
||
// λ‘κ·Έμμ | ||
|
||
// μ΄λ©μΌ μΈμ¦ | ||
|
||
// λΉλ°λ²νΈ μ¬μ€μ | ||
|
||
// ν ν° μ¬λ°κΈ | ||
|
||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/meltingpot/server/auth/controller/dto/AccountResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package meltingpot.server.auth.controller.dto; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import meltingpot.server.domain.entity.Account; | ||
import meltingpot.server.util.TokenDto; | ||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
public class AccountResponseDto { | ||
private final Long id; | ||
private final String email; | ||
private final String name; | ||
private TokenDto tokenDto; | ||
|
||
public static AccountResponseDto of(Account account) { | ||
return AccountResponseDto.builder() | ||
.id(account.getId()) | ||
.email(account.getUsername()) | ||
.name(account.getName()) | ||
.build(); | ||
} | ||
|
||
|
||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/meltingpot/server/auth/controller/dto/SigninRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package meltingpot.server.auth.controller.dto; | ||
|
||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.Size; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import meltingpot.server.auth.service.dto.SigninServiceDto; | ||
|
||
@Getter | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class SigninRequestDto { | ||
|
||
@NotBlank(message = "email is required") | ||
private String email; | ||
|
||
@NotBlank(message = "password is required") | ||
private String password; | ||
|
||
public SigninServiceDto toServiceDto() { | ||
return SigninServiceDto.builder() | ||
.username(getEmail()) | ||
.password(getPassword()) | ||
.build(); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
src/main/java/meltingpot/server/auth/service/AuthService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package meltingpot.server.auth.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import meltingpot.server.exception.ResourceNotFoundException; | ||
import meltingpot.server.config.TokenProvider; | ||
import meltingpot.server.domain.entity.RefreshToken; | ||
import meltingpot.server.domain.entity.Account; | ||
import meltingpot.server.domain.repository.RefreshTokenRepository; | ||
import meltingpot.server.domain.repository.AccountRepository; | ||
import meltingpot.server.auth.controller.dto.AccountResponseDto; | ||
import meltingpot.server.auth.service.dto.SigninServiceDto; | ||
import meltingpot.server.util.AccountUser; | ||
import meltingpot.server.util.ResponseCode; | ||
import meltingpot.server.util.SecurityUtil; | ||
import meltingpot.server.util.TokenDto; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Service | ||
@EnableWebSecurity | ||
public class AuthService implements UserDetailsService { | ||
private final AccountRepository accountRepository; | ||
private final AuthenticationManagerBuilder authenticationManagerBuilder; | ||
private final TokenProvider tokenProvider; | ||
private final RefreshTokenRepository refreshTokenRepository; | ||
//private final PasswordEncoder passwordEncoder; | ||
private static final String BEARER_HEADER = "Bearer "; | ||
|
||
// λ‘κ·ΈμΈ μ μ μ 보 λ°ν to @CurrentUser | ||
@Transactional(readOnly = true) | ||
public Account getUserInfo(){ | ||
return accountRepository.findByUsernameAndDeletedIsNull(SecurityUtil.getCurrentUserName()) | ||
.orElseThrow(() -> new ResourceNotFoundException(ResponseCode.ACCOUNT_NOT_FOUND)); | ||
} | ||
|
||
// λ‘κ·ΈμΈμ μ μ μ 보 μ‘°ννλ λ©μλ override | ||
@Override | ||
@Transactional(readOnly = true) | ||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | ||
Account account = accountRepository.findByUsername(username) | ||
.orElseThrow(() -> new UsernameNotFoundException(username)); | ||
return new AccountUser(account); | ||
} | ||
|
||
// λ‘κ·ΈμΈ | ||
@Transactional(rollbackFor = Exception.class) | ||
public AccountResponseDto signin(SigninServiceDto serviceDto){ | ||
|
||
// 1. Login ID/PW λ₯Ό κΈ°λ°μΌλ‘ AuthenticationToken μμ± (λ―ΈμΈμ¦ ν ν°) | ||
UsernamePasswordAuthenticationToken authenticationToken = serviceDto.toAuthentication(); | ||
|
||
// 2. κ²μ¦ (μ¬μ©μ λΉλ°λ²νΈ 체ν¬) μ΄ μ΄λ£¨μ΄μ§λ λΆλΆ | ||
// authenticate λ©μλκ° μ€νμ΄ λ λ loadUserByUsername λ©μλκ° μ€νλ¨ | ||
Authentication authentication = authenticationManagerBuilder.getObject() | ||
.authenticate(authenticationToken); | ||
|
||
// 3. μΈμ¦ μ 보λ₯Ό κΈ°λ°μΌλ‘ JWT ν ν° μμ± | ||
TokenDto tokenDto = tokenProvider.generateTokenDto(authentication); | ||
|
||
// 4. RefreshToken μ μ₯ | ||
Account account = accountRepository.findByUsername(authentication.getName()) | ||
.orElseThrow(() -> new ResourceNotFoundException(ResponseCode.ACCOUNT_NOT_FOUND)); | ||
RefreshToken refreshToken = RefreshToken.builder() | ||
.account(account) | ||
.tokenValue(tokenDto.getRefreshToken()) | ||
.build(); | ||
|
||
refreshTokenRepository.save(refreshToken); | ||
|
||
//μΈμ¦λ Authenticationλ₯Ό SecurityContextμ μ μ₯ | ||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
|
||
// 5. ν ν° ν¬ν¨ νμ¬ μ μ μ 보 λ°ν | ||
AccountResponseDto accountResponseDto = AccountResponseDto.of(getUserInfo()); | ||
accountResponseDto.setTokenDto(tokenDto); | ||
|
||
return accountResponseDto; | ||
|
||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/meltingpot/server/auth/service/dto/SigninServiceDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package meltingpot.server.auth.service.dto; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
|
||
@Getter | ||
@Builder | ||
public class SigninServiceDto { | ||
|
||
private final String username; | ||
private final String password; | ||
|
||
// λ―ΈμΈμ¦ ν ν° μμ± | ||
public UsernamePasswordAuthenticationToken toAuthentication() { | ||
return new UsernamePasswordAuthenticationToken(getUsername(), getPassword()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/meltingpot/server/config/HttpLogoutSuccessHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package meltingpot.server.config; | ||
|
||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
|
||
@Component | ||
public class HttpLogoutSuccessHandler implements LogoutSuccessHandler { | ||
|
||
@Override | ||
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, | ||
Authentication authentication) throws IOException, ServletException { | ||
if (authentication == null) { | ||
response.setStatus(HttpServletResponse.SC_BAD_REQUEST); | ||
} else { | ||
response.setStatus(HttpServletResponse.SC_OK); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/meltingpot/server/config/JwtAccessDeniedHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package meltingpot.server.config; | ||
|
||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.web.access.AccessDeniedHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
|
||
@Component | ||
public class JwtAccessDeniedHandler implements AccessDeniedHandler { | ||
|
||
@Override | ||
public void handle(HttpServletRequest request, HttpServletResponse response, | ||
AccessDeniedException accessDeniedException) throws IOException, ServletException { | ||
// νμν κΆνμ΄ μμ΄ μ κ·Όνλ € ν λ 401 | ||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/meltingpot/server/config/JwtAuthenticationEntryPoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package meltingpot.server.config; | ||
|
||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import meltingpot.server.exception.UnknownAuthenticationException; | ||
import org.springframework.security.authentication.BadCredentialsException; | ||
import org.springframework.security.authentication.CredentialsExpiredException; | ||
import org.springframework.security.authentication.DisabledException; | ||
import org.springframework.security.authentication.InternalAuthenticationServiceException; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
|
||
@Component | ||
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint { | ||
|
||
@Override | ||
public void commence(HttpServletRequest request, HttpServletResponse response, | ||
AuthenticationException authException) throws IOException, ServletException { | ||
// μ ν¨ν μ격μ¦λͺ μ μ 곡νμ§ μκ³ μ κ·Όνλ € ν λ 401 | ||
if (authException instanceof BadCredentialsException | ||
|| authException instanceof InternalAuthenticationServiceException) { | ||
throw new BadCredentialsException("μ΄λ©μΌμ΄λ λΉλ°λ²νΈκ° λ§μ§ μμ΅λλ€"); | ||
} else if (authException instanceof DisabledException) { | ||
throw new DisabledException("κ³μ μ΄ λΉνμ±ν λμμ΅λλ€"); | ||
} else if (authException instanceof CredentialsExpiredException) { | ||
throw new CredentialsExpiredException("λΉλ°λ²νΈ μ ν¨κΈ°κ°μ΄ λ§λ£λμμ΅λλ€"); | ||
} else { | ||
throw new UnknownAuthenticationException("μ μ μλ μ΄μ λ‘ λ‘κ·ΈμΈμ μ€ν¨νμ΅λλ€"); | ||
} | ||
} | ||
} |
Oops, something went wrong.