-
Notifications
You must be signed in to change notification settings - Fork 0
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 #18 from 9oormDari/develop
Develop
- Loading branch information
Showing
15 changed files
with
480 additions
and
8 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
43 changes: 43 additions & 0 deletions
43
src/main/java/com/goormdari/domain/user/domain/CustomUserDetails.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,43 @@ | ||
package com.goormdari.domain.user.domain; | ||
|
||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
public class CustomUserDetails implements UserDetails { | ||
|
||
private final User user; | ||
|
||
public CustomUserDetails(User user) { | ||
this.user = user; | ||
} | ||
|
||
// ๊ถํ ๋ฐํ | ||
@Override | ||
public Collection<? extends GrantedAuthority> getAuthorities() { | ||
return Collections.singletonList(new SimpleGrantedAuthority("ROLE_" + user.getRole())); | ||
} | ||
|
||
@Override | ||
public String getPassword() { | ||
return user.getPassword(); | ||
} | ||
|
||
@Override | ||
public String getUsername() { | ||
return user.getUsername(); | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonExpired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonLocked() { | ||
return true; | ||
} | ||
} |
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,5 @@ | ||
package com.goormdari.domain.user.domain; | ||
|
||
public enum Role { | ||
ROLE_USER, ROLE_ADMIN | ||
} |
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
17 changes: 17 additions & 0 deletions
17
src/main/java/com/goormdari/domain/user/domain/dto/AddUserRequest.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.goormdari.domain.user.domain.dto; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.*; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class AddUserRequest { | ||
@NotBlank(message = "Nickname cannot be blank") | ||
private String nickname; | ||
@NotBlank(message = "Username cannot be blank") | ||
private String username; | ||
@NotBlank(message = "Password cannot be blank") | ||
private String password; | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/goormdari/domain/user/domain/dto/JwtResponse.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.goormdari.domain.user.domain.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class JwtResponse { | ||
private String accessToken; | ||
private String tokenType = "Bearer"; | ||
|
||
public JwtResponse(String accessToken) { | ||
this.accessToken = accessToken; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/goormdari/domain/user/domain/dto/LoginRequest.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,19 @@ | ||
package com.goormdari.domain.user.domain.dto; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class LoginRequest { | ||
@NotBlank(message = "Username cannot be blank") | ||
private String username; | ||
|
||
@NotBlank(message = "Password cannot be blank") | ||
private String password; | ||
} |
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
25 changes: 25 additions & 0 deletions
25
src/main/java/com/goormdari/domain/user/domain/service/CustomUserDetailsService.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,25 @@ | ||
package com.goormdari.domain.user.domain.service; | ||
|
||
import com.goormdari.domain.user.domain.CustomUserDetails; | ||
import com.goormdari.domain.user.domain.User; | ||
import com.goormdari.domain.user.domain.repository.UserRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CustomUserDetailsService implements UserDetailsService { | ||
|
||
private final UserRepository userRepository; | ||
|
||
// ์ฌ์ฉ์ ์ด๋ฆ์ผ๋ก ์ฌ์ฉ์์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฉ์๋ | ||
@Override | ||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | ||
User user = userRepository.findByUsername(username) | ||
.orElseThrow(() -> new UsernameNotFoundException("๋ค์ ์ ์ ์ ์ ๋ณด๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค: " + username)); | ||
return new CustomUserDetails(user); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/com/goormdari/domain/user/domain/service/UserService.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,69 @@ | ||
package com.goormdari.domain.user.domain.service; | ||
|
||
import com.goormdari.domain.user.domain.User; | ||
import com.goormdari.domain.user.domain.dto.AddUserRequest; | ||
import com.goormdari.domain.user.domain.dto.JwtResponse; | ||
import com.goormdari.domain.user.domain.dto.LoginRequest; | ||
import com.goormdari.domain.user.domain.repository.UserRepository; | ||
import com.goormdari.global.config.security.jwt.JWTUtil; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class UserService { | ||
|
||
private final UserRepository userRepository; | ||
private final PasswordEncoder passwordEncoder; | ||
private final AuthenticationManager authenticationManager; | ||
private final JWTUtil jwtUtil; | ||
|
||
@Transactional | ||
public Long save(AddUserRequest dto) { | ||
// ์ฌ์ฉ์ ์ด๋ฆ ์ค๋ณต ์ฒดํฌ | ||
if (userRepository.findByUsername(dto.getUsername()).isPresent()) { | ||
throw new IllegalArgumentException("Username is already exists."); | ||
} | ||
|
||
// ์ฌ์ฉ์ ์ ์ฅ | ||
return userRepository.save(User.builder() | ||
.nickname(dto.getNickname()) | ||
.username(dto.getUsername()) | ||
.password(passwordEncoder.encode(dto.getPassword())) | ||
.role("ROLE_USER") | ||
.build()).getId(); | ||
} | ||
|
||
public JwtResponse signupAndLogin(AddUserRequest dto) { | ||
save(dto); | ||
|
||
return loginAndGetToken(new LoginRequest(dto.getUsername(), dto.getPassword())); | ||
} | ||
|
||
public JwtResponse loginAndGetToken(LoginRequest loginRequest) { | ||
// ์ฌ์ฉ์ ์ธ์ฆ | ||
Authentication authentication = authenticationManager.authenticate( | ||
new UsernamePasswordAuthenticationToken( | ||
loginRequest.getUsername(), | ||
loginRequest.getPassword() | ||
) | ||
); | ||
|
||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
|
||
// JWT ์์ฑ | ||
User user = userRepository.findByUsername(loginRequest.getUsername()).orElseThrow(() -> new UsernameNotFoundException("Username not found with: " + loginRequest.getUsername())); | ||
String jwt = jwtUtil.generateToken(user.getUsername(), user.getRole()); | ||
|
||
return new JwtResponse(jwt); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/com/goormdari/domain/user/presentation/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,47 @@ | ||
package com.goormdari.domain.user.presentation; | ||
|
||
import com.goormdari.domain.user.domain.dto.AddUserRequest; | ||
import com.goormdari.domain.user.domain.dto.JwtResponse; | ||
import com.goormdari.domain.user.domain.dto.LoginRequest; | ||
import com.goormdari.domain.user.domain.service.UserService; | ||
|
||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
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("/auth") | ||
@RequiredArgsConstructor | ||
public class AuthController { | ||
|
||
private final UserService userService; | ||
|
||
/** | ||
* ํ์๊ฐ์ ํ JWT ํ ํฐ ๋ฐ๊ธ | ||
* | ||
* @param addUserRequest ํ์๊ฐ์ ์์ฒญ ๋ฐ์ดํฐ | ||
* @return JWT ์๋ต | ||
*/ | ||
@PostMapping("/signup") | ||
public ResponseEntity<JwtResponse> registerUser(@Valid @RequestBody AddUserRequest addUserRequest) { | ||
JwtResponse jwtResponse = userService.signupAndLogin(addUserRequest); | ||
return ResponseEntity.status(HttpStatus.CREATED).body(jwtResponse); | ||
} | ||
|
||
/** | ||
* ๋ก๊ทธ์ธ ํ JWT ํ ํฐ ๋ฐ๊ธ | ||
* | ||
* @param loginRequest ๋ก๊ทธ์ธ ์์ฒญ ๋ฐ์ดํฐ | ||
* @return JWT ์๋ต | ||
*/ | ||
@PostMapping("/login") | ||
public ResponseEntity<JwtResponse> authenticateUser(@Valid @RequestBody LoginRequest loginRequest) { | ||
JwtResponse jwtResponse = userService.loginAndGetToken(loginRequest); | ||
return ResponseEntity.ok(jwtResponse); | ||
} | ||
} |
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.