-
Notifications
You must be signed in to change notification settings - Fork 2
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 #8 from J2KB-3rd-Season/changmink
토큰 기반 로그인 적용
- Loading branch information
Showing
17 changed files
with
304 additions
and
97 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
26 changes: 26 additions & 0 deletions
26
src/main/java/com/devin/dev/controller/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,26 @@ | ||
package com.devin.dev.controller.Auth; | ||
|
||
import com.devin.dev.dto.user.UserLoginRequestDto; | ||
import com.devin.dev.dto.user.UserSimpleDto; | ||
import com.devin.dev.model.DefaultResponse; | ||
import com.devin.dev.service.UserService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class AuthController { | ||
private final UserService userService; | ||
|
||
@PostMapping("/user/login") | ||
public DefaultResponse<?> login(@RequestBody UserLoginRequestDto userLoginRequestDto) { | ||
return userService.login(userLoginRequestDto); | ||
} | ||
|
||
@PostMapping("/user/join") | ||
public DefaultResponse<?> join(@RequestBody UserSimpleDto userSimpleDto) { | ||
return userService.join(userSimpleDto); | ||
} | ||
} |
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
9 changes: 9 additions & 0 deletions
9
src/main/java/com/devin/dev/dto/user/UserLoginRequestDto.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,9 @@ | ||
package com.devin.dev.dto.user; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class UserLoginRequestDto { | ||
String userEmail; | ||
String userPassword; | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/devin/dev/dto/user/UserLoginResponseDto.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.devin.dev.dto.user; | ||
|
||
import com.devin.dev.entity.user.User; | ||
import com.devin.dev.security.JwtAuthToken; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class UserLoginResponseDto { | ||
private Long userId; | ||
private String userProfile; | ||
private String userName; | ||
private String token; | ||
|
||
public UserLoginResponseDto(User user, JwtAuthToken token) { | ||
this.userId = user.getId(); | ||
this.userProfile = user.getProfile(); | ||
this.userName = user.getName(); | ||
this.token = token.getToken(); | ||
} | ||
} |
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,26 +1,24 @@ | ||
package com.devin.dev.dto.user; | ||
|
||
import com.devin.dev.entity.user.UserStatus; | ||
import com.querydsl.core.annotations.QueryProjection; | ||
import lombok.Data; | ||
|
||
import javax.validation.constraints.NotEmpty; | ||
|
||
@Data | ||
public class UserSimpleDto { | ||
|
||
private String name; | ||
private String userName; | ||
@NotEmpty(message = "email은 필수 입니다") | ||
private String email; | ||
private String userEmail; | ||
@NotEmpty(message = "password는 필수 입니다") | ||
private String password; | ||
private String phone_number; | ||
private String userPassword; | ||
private String userPhoneNumber; | ||
|
||
@QueryProjection | ||
public UserSimpleDto(String name, String email, String password, String phone_number) { | ||
this.name = name; | ||
this.email = email; | ||
this.password = password; | ||
this.phone_number = phone_number; | ||
public UserSimpleDto(String userName, String userEmail, String userPassword, String userPhoneNumber) { | ||
this.userName = userName; | ||
this.userEmail = userEmail; | ||
this.userPassword = userPassword; | ||
this.userPhoneNumber = userPhoneNumber; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.devin.dev.security; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Builder | ||
@Getter | ||
public class JwtAuthToken { | ||
private String token; | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/com/devin/dev/security/JwtAuthTokenProvider.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,66 @@ | ||
package com.devin.dev.security; | ||
|
||
import io.jsonwebtoken.*; | ||
import io.jsonwebtoken.io.Decoders; | ||
import io.jsonwebtoken.security.Keys; | ||
import io.jsonwebtoken.security.SignatureException; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.crypto.SecretKey; | ||
import javax.servlet.http.HttpServletRequest; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.Date; | ||
|
||
@Component | ||
public class JwtAuthTokenProvider { | ||
private static final String SECRET_KEY = "J2KBJ2KBJ2KBJ2KBJ2KBJ2KBJ2KBJ2KBJ2KBJ2KBJ2KBJ2KBJ2KBJ2KB"; | ||
private static final long EXPIRATION_MS = 1000 * 60 * 60 * 24; | ||
|
||
public JwtAuthToken publishToken(Long userId) { | ||
return JwtAuthToken.builder().token(generateToken(userId)).build(); | ||
} | ||
|
||
private String generateToken(Long userId) { | ||
LocalDateTime now = LocalDateTime.now(); | ||
LocalDateTime expiredAt = now.plus(EXPIRATION_MS, ChronoUnit.MILLIS); | ||
SecretKey key = Keys.hmacShaKeyFor(Decoders.BASE64.decode(SECRET_KEY)); | ||
return Jwts.builder() | ||
.setSubject(String.valueOf(userId)) | ||
.setIssuedAt(Date.from(now.atZone(ZoneId.systemDefault()).toInstant())) | ||
.setExpiration(Date.from(expiredAt.atZone(ZoneId.systemDefault()).toInstant())) | ||
.signWith(key) | ||
.compact(); | ||
} | ||
|
||
public String parseToken(HttpServletRequest request) { | ||
String bearerToken = request.getHeader("Authorization"); | ||
if (bearerToken != null && bearerToken.startsWith("Bearer ")) { | ||
return bearerToken.substring(7); | ||
} | ||
return null; | ||
} | ||
|
||
public Long getUserId(String token) { | ||
Claims claims = Jwts.parser() | ||
.setSigningKey(SECRET_KEY) | ||
.parseClaimsJws(token) | ||
.getBody(); | ||
return Long.parseLong(claims.getSubject()); | ||
} | ||
|
||
public boolean validateToken(String token) { | ||
if (token != null && !token.equals("")) { | ||
try { | ||
Jwts.parser().setSigningKey(SECRET_KEY).parseClaimsJws(token); | ||
return true; | ||
} catch (Exception e) { | ||
throw new RuntimeException(); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
|
||
} |
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
Oops, something went wrong.