Skip to content

Commit

Permalink
Merge pull request #35 from LikeLion-12th-SKHU/develop
Browse files Browse the repository at this point in the history
๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ
  • Loading branch information
AWESOMEGUY5 authored Jul 25, 2024
2 parents 9fe5547 + 9582afd commit 1827d63
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti
.csrf(AbstractHttpConfigurer::disable)
/*.authorizeHttpRequests(authorize -> authorize
.requestMatchers("/sign-up/**", "/sign-in/**", "/login/oauth2/**").permitAll()
.requestMatchers("/user/**").hasRole("USER")
.requestMatchers("/user/**").hasRole("ROLE_USER")
)*/
.authorizeHttpRequests(authorizeRequests -> authorizeRequests
.anyRequest().permitAll()) // ์Šค์›จ๊ฑฐ ์•ˆ ์—ด๋ฆผ ์ด์Šˆ๋กœ ๋‹ค ํ—ˆ์šฉ์œผ๋กœ ๋ฐ”๊ฟˆ
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public GoogleToken signUpOrSignIn(String googleAccessToken) {
userRepository.save(User.builder()
.email(userInfo.getEmail())
.name(userInfo.getName())
.role(Role.USER)
.role(Role.ROLE_USER)
.build())
);
return googleTokenProvider.generateToken(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public GoogleToken generateToken(User user) {
.setSubject(user.getEmail())
.claim(AUTHORITIES_KEY, user.getRole().name())
.setExpiration(tokenExpiredTime)
.signWith(key, SignatureAlgorithm.HS512)
.signWith(key, SignatureAlgorithm.HS256)
.compact();

return GoogleToken.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,30 @@ public String generateToken(String email) {

Date expireDate = new Date(date.getTime() + Long.parseLong(tokenExpireTime));

return Jwts.builder()
String accessToken = Jwts.builder()
.setSubject(email)
.setIssuedAt(date)
.setExpiration(expireDate)
.signWith(key, SignatureAlgorithm.HS256)
.compact();

return accessToken;
}

// public String refreshToken(String email) {
// Date date = new Date();
//
// Date expireDate = new Date(date.getTime() + Long.parseLong(tokenExpireTime) * 24);
//
// return Jwts.builder()
// .setSubject(email)
// .setIssuedAt(date)
// .setExpiration(expireDate)
// .signWith(key, SignatureAlgorithm.HS256)
// .compact();
// }
public String refreshToken(String email) {
Date date = new Date();

Date expireDate = new Date(date.getTime() + Long.parseLong(tokenExpireTime) * 24 * 7);

String refreshToken = Jwts.builder()
.setSubject(email)
.setIssuedAt(date)
.setExpiration(expireDate)
.signWith(key, SignatureAlgorithm.HS256)
.compact();

return refreshToken;
}

public boolean validateToken(String token) {
// ํ† ํฐ์„ ๊ฒ€์ฆํ•˜๋Š” ๋ถ€๋ถ„
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.likelion.likelion_12th_team05.global.auth.jwt;

import lombok.Builder;

@Builder
public record TokenDto(
String grantType,

String accessToken,

String refreshToken
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.likelion.likelion_12th_team05.config.ApiResponseTemplate;
import org.likelion.likelion_12th_team05.global.auth.googleAuth.AuthLoginService;
import org.likelion.likelion_12th_team05.global.auth.googleAuth.GoogleToken;
import org.likelion.likelion_12th_team05.global.auth.jwt.TokenDto;
import org.likelion.likelion_12th_team05.user.api.dto.request.UserSignInReqDto;
import org.likelion.likelion_12th_team05.user.api.dto.request.UserSignUpReqDto;
import org.likelion.likelion_12th_team05.user.api.dto.response.UserSignInResDto;
Expand All @@ -31,9 +32,9 @@ public UserController(UserService userService, AuthLoginService authLoginService
@ApiResponse(responseCode = "401", description = "์ธ์ฆ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.")
})
@PostMapping("/sign-up")
public ApiResponseTemplate<SuccessCode> userSignUp(@RequestBody @Valid UserSignUpReqDto userSignUpReqDto) {
userService.userSignUp(userSignUpReqDto);
return ApiResponseTemplate.successWithNoContent(SuccessCode.USER_SIGNUP_SUCCESS);
public ApiResponseTemplate<String> userSignUp(@RequestBody @Valid UserSignUpReqDto userSignUpReqDto, TokenDto tokenDto) {
userService.userSignUp(userSignUpReqDto, tokenDto);
return ApiResponseTemplate.successResponse(userSignUpReqDto.refreshToken(), SuccessCode.USER_SIGNUP_SUCCESS);
}

@Operation(summary = "๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ", description = "๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public record UserSignInReqDto(

@NotBlank(message = "๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ํ•„์ˆ˜๋กœ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.")
@Size(min = 8, message = "8์ž ์ด์ƒ ์ž…๋ ฅํ•˜์„ธ์š”.")
String password
String password,

String refreshToken
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public record UserSignUpReqDto(
@Size(min = 8, message = "8์ž ์ด์ƒ ์ž…๋ ฅํ•˜์„ธ์š”.")
String password,

String refreshToken,

Role role
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import org.likelion.likelion_12th_team05.user.domain.User;

@Builder
public record UserSignInResDto(String name, String email, String token) {
public static UserSignInResDto of(User user, String token) {
public record UserSignInResDto(String name, String email, String accessToken, String refreshToken) {
public static UserSignInResDto of(User user, String accessToken, String refreshToken) {
return UserSignInResDto.builder()
.name(user.getName())
.email(user.getEmail())
.token(token).build();
.accessToken(accessToken)
.refreshToken(refreshToken).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.extern.slf4j.Slf4j;
import org.likelion.likelion_12th_team05.global.auth.jwt.JwtTokenProvider;
import org.likelion.likelion_12th_team05.global.auth.jwt.TokenDto;
import org.likelion.likelion_12th_team05.user.api.dto.request.UserSignInReqDto;
import org.likelion.likelion_12th_team05.user.api.dto.request.UserSignUpReqDto;
import org.likelion.likelion_12th_team05.user.api.dto.response.UserSignInResDto;
Expand All @@ -28,15 +29,18 @@ public UserService(UserRepository userRepository, PasswordEncoder passwordEncode

// ํšŒ์›๊ฐ€์ž…
@Transactional
public void userSignUp(UserSignUpReqDto userSignUpReqDto) {
public void userSignUp(UserSignUpReqDto userSignUpReqDto, TokenDto tokenDto) {
if (userRepository.existsByEmail(userSignUpReqDto.email())) {
throw new IllegalArgumentException("์ด๋ฏธ ์กด์žฌํ•˜๋Š” ์ด๋ฉ”์ผ์ž…๋‹ˆ๋‹ค.");
}

User user = User.builder()
.name(userSignUpReqDto.name())
.email(userSignUpReqDto.email())
.password(passwordEncoder.encode(userSignUpReqDto.password()))
.role(Role.USER)
.accessToken(tokenDto.accessToken())
.refreshToken(tokenDto.refreshToken())
.role(Role.ROLE_USER)
.build();

userRepository.save(user);
Expand All @@ -45,13 +49,13 @@ public void userSignUp(UserSignUpReqDto userSignUpReqDto) {
public UserSignInResDto userSignIn(UserSignInReqDto userSignUpReqDto) {
User user = userRepository.findByEmail(userSignUpReqDto.email())
.orElseThrow(() -> new IllegalArgumentException("์ด๋ฉ”์ผ์ด๋‚˜ ํŒจ์Šค์›Œ๋“œ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."));
String token = tokenProvider.generateToken(user.getEmail());
// String refreshToken = tokenProvider.refreshToken(user.getEmail());
String accessToken = tokenProvider.generateToken(user.getEmail());
String refreshToken = tokenProvider.refreshToken(user.getEmail());

if (!passwordEncoder.matches(userSignUpReqDto.password(), user.getPassword())) {
throw new IllegalArgumentException("์ด๋ฉ”์ผ์ด๋‚˜ ํŒจ์Šค์›Œ๋“œ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
}

return UserSignInResDto.of(user, token);
return UserSignInResDto.of(user, accessToken, refreshToken);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.likelion.likelion_12th_team05.user.domain;

public enum Role {
USER
ROLE_USER
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ public class User {
private String name;
private String email;
private String password;

private String accessToken;
private String refreshToken;

@Enumerated(value = EnumType.STRING)
private Role role;
// @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
// private List<Curation> curations = new ArrayList<>();

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Curation> curations = new ArrayList<>();
Expand All @@ -38,10 +41,12 @@ public class User {
private List<Like> likes = new ArrayList<>();

@Builder
private User(String name, String email, String password, Role role){
private User(String name, String email, String password, String accessToken, String refreshToken, Role role){
this.name = name;
this.email = email;
this.password = password;
this.accessToken = accessToken;
this.refreshToken = refreshToken;
this.role = role;
}
}

0 comments on commit 1827d63

Please sign in to comment.