-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from YoungGyo-00/YoungGyo-00
[18기_이영교] Spring Security&JWT 미션 제출합니다.
- Loading branch information
Showing
28 changed files
with
811 additions
and
68 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,4 +40,5 @@ secrets.yml | |
.gradletasknamecache | ||
.sts4-cache | ||
.yml | ||
.idea | ||
.idea | ||
env.yml |
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
37 changes: 37 additions & 0 deletions
37
market/src/main/java/com/ceos18/market/database/RefreshToken.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,37 @@ | ||
package com.ceos18.market.database; | ||
|
||
import com.ceos18.market.database.base.BaseTimeEntity; | ||
import jakarta.persistence.*; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class RefreshToken extends BaseTimeEntity { | ||
public static final String TABLE_NAME = "USER_REFRESH_TOKEN"; | ||
|
||
@Id | ||
@Column(name = "REFRESH_TOKEN_ID") | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long refreshTokenId; | ||
|
||
@Column(name = "USER_ID", nullable = false, length = 100, unique = true) | ||
private String userId; | ||
|
||
@Column(name = "REFRESH_TOKEN", nullable = false) | ||
private String refreshToken; | ||
|
||
@Builder | ||
public RefreshToken(String userId, String refreshToken) { | ||
this.userId = userId; | ||
this.refreshToken = refreshToken; | ||
} | ||
|
||
public RefreshToken updateToken(String token) { | ||
this.refreshToken = token; | ||
return this; | ||
} | ||
} |
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
8 changes: 8 additions & 0 deletions
8
market/src/main/java/com/ceos18/market/database/enums/Role.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,8 @@ | ||
package com.ceos18.market.database.enums; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum Role { | ||
ROLE_USER, ROLE_Manager | ||
} |
35 changes: 35 additions & 0 deletions
35
market/src/main/java/com/ceos18/market/domain/auth/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,35 @@ | ||
package com.ceos18.market.domain.auth; | ||
|
||
import com.ceos18.market.domain.auth.dto.AuthRequestDto; | ||
import com.ceos18.market.domain.auth.dto.SigninRequestDto; | ||
import com.ceos18.market.domain.product.dto.ProductRequest; | ||
import com.ceos18.market.global.jwt.dto.TokenDto; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.h2.command.Token; | ||
import org.springframework.http.ResponseEntity; | ||
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; | ||
|
||
@RestController | ||
@RequestMapping("/api/auth") | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class AuthController { | ||
private final AuthService authService; | ||
|
||
@PostMapping("/signup") | ||
public ResponseEntity<TokenDto> signup(@RequestBody AuthRequestDto authRequestDto) { | ||
log.info("유저 회원가입하기"); | ||
return ResponseEntity.ok(authService.signup(authRequestDto)); | ||
} | ||
|
||
@PostMapping("/signin") | ||
public ResponseEntity<TokenDto> signin(@RequestBody SigninRequestDto signinRequestDto) { | ||
log.info("유저 로그인하기"); | ||
return ResponseEntity.ok(authService.signin(signinRequestDto)); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
market/src/main/java/com/ceos18/market/domain/auth/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,51 @@ | ||
package com.ceos18.market.domain.auth; | ||
|
||
import com.ceos18.market.database.User; | ||
import com.ceos18.market.domain.auth.dto.AuthRequestDto; | ||
import com.ceos18.market.domain.auth.dto.SigninRequestDto; | ||
import com.ceos18.market.global.jwt.TokenProvider; | ||
import com.ceos18.market.global.jwt.dto.TokenDto; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Optional; | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
public class AuthService { | ||
private final AuthenticationManagerBuilder managerBuilder; | ||
private final UserRepository userRepository; | ||
private final TokenProvider tokenProvider; | ||
|
||
@Transactional | ||
public TokenDto signup(AuthRequestDto authRequestDto) { | ||
Optional<User> findUser = userRepository.findByPhoneNumber(authRequestDto.getPhoneNumber()); | ||
if (findUser.isPresent()) { | ||
throw new RuntimeException("이미 가입되어 있는 유저입니다"); | ||
} | ||
|
||
User user = User.of(authRequestDto); | ||
|
||
userRepository.save(user); | ||
|
||
SigninRequestDto signinRequestDto = SigninRequestDto.builder() | ||
.phoneNumber(user.getPhoneNumber()) | ||
.build(); | ||
|
||
return signin(signinRequestDto); | ||
} | ||
|
||
@Transactional | ||
public TokenDto signin(SigninRequestDto signinRequestDto) { | ||
UsernamePasswordAuthenticationToken authenticationToken = signinRequestDto.toAuthentication(); | ||
|
||
Authentication authentication = managerBuilder.getObject().authenticate(authenticationToken); | ||
|
||
return tokenProvider.createAccessToken(authentication); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
market/src/main/java/com/ceos18/market/domain/auth/RefreshTokenRepository.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,12 @@ | ||
package com.ceos18.market.domain.auth; | ||
|
||
import com.ceos18.market.database.RefreshToken; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface RefreshTokenRepository extends JpaRepository<RefreshToken, Long> { | ||
Optional<RefreshToken> findByUserId(String userId); | ||
} |
6 changes: 5 additions & 1 deletion
6
.../ceos18/market/domain/UserRepository.java → ...18/market/domain/auth/UserRepository.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 |
---|---|---|
@@ -1,10 +1,14 @@ | ||
package com.ceos18.market.domain; | ||
package com.ceos18.market.domain.auth; | ||
|
||
import com.ceos18.market.database.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface UserRepository extends JpaRepository<User, String> { | ||
User findByUserNo(String userNo); | ||
Optional<User> findByPhoneNumber(String phoneNumber); | ||
} |
17 changes: 17 additions & 0 deletions
17
market/src/main/java/com/ceos18/market/domain/auth/dto/AuthRequestDto.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,17 @@ | ||
package com.ceos18.market.domain.auth.dto; | ||
|
||
import com.ceos18.market.database.enums.StatusCode; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
public class AuthRequestDto { | ||
private String userNo; | ||
private String nickName; | ||
private String phoneNumber; | ||
private String userImageUrl; | ||
private String address; | ||
private String userAgent; | ||
private StatusCode marketingYN; | ||
} |
20 changes: 20 additions & 0 deletions
20
market/src/main/java/com/ceos18/market/domain/auth/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,20 @@ | ||
package com.ceos18.market.domain.auth.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
||
@Getter | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class SigninRequestDto { | ||
private String phoneNumber; | ||
|
||
public UsernamePasswordAuthenticationToken toAuthentication() { | ||
return new UsernamePasswordAuthenticationToken(phoneNumber, "test"); | ||
} | ||
} |
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
Oops, something went wrong.