-
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.
Merge remote-tracking branch 'refs/remotes/origin/feature/#18-1' intoโฆ
โฆ feature/#29 # Conflicts: # src/main/java/meltingpot/server/domain/entity/Account.java # src/main/java/meltingpot/server/domain/entity/Comment.java # src/main/java/meltingpot/server/domain/entity/Image.java # src/main/java/meltingpot/server/domain/entity/Post.java # src/main/java/meltingpot/server/domain/entity/Report.java # src/main/java/meltingpot/server/domain/entity/chat/Chat.java # src/main/java/meltingpot/server/domain/entity/chat/ChatUser.java # src/main/java/meltingpot/server/domain/entity/party/PartyParticipant.java # src/main/java/meltingpot/server/domain/entity/party/PartyWishlist.java
- Loading branch information
Showing
37 changed files
with
920 additions
and
17 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
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()); | ||
} | ||
} |
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.