Implement userProfile

This commit is contained in:
hou27
2022-06-11 05:31:51 +09:00
parent fe53bc9ef0
commit 02228ed615
5 changed files with 55 additions and 3 deletions

View File

@@ -11,9 +11,6 @@ import org.springframework.security.config.annotation.authentication.configurati
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

View File

@@ -11,6 +11,7 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@@ -37,6 +38,15 @@ public class UserController {
return "user/profile";
}
@GetMapping("/profile/{username}")
public String userProfile(Model model, @PathVariable String username) {
User user = userService.findByName(username)
.orElseThrow(() -> new UserNotFoundException());
model.addAttribute("userDetail", user);
return "user/profile";
}
@GetMapping("/userList")
public String showUserList(Model model) {
List<User> userList = userService.findAll();

View File

@@ -19,6 +19,19 @@ public interface UserService {
*/
Optional<User> findByEmail(String email);
/**
* 이름을 통해 유저 조회
* @param name
* @return 조회된 유저
*/
Optional<User> findByName(String name);
// /**
// * Security Context에 존재하는 인증 정보를 통해 유저 정보 조회
// * @return 조회된 유저
// */
// Optional<User> getMyInfo();
/**
* 유저 정보 수정
* @param user 수정활 User Entity

View File

@@ -5,8 +5,13 @@ import demo.api.user.repository.UserRepository;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
@Slf4j
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
@@ -22,6 +27,31 @@ public class UserServiceImpl implements UserService {
return userRepository.findByEmail(email);
}
@Override
public Optional<User> findByName(String name) {
return userRepository.findByName(name);
}
// @Override
// public Optional<User> getMyInfo() {
// Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
// String username = null;
//
// if (authentication == null) {
// log.debug("Security Context에 인증 정보가 없습니다.");
// return Optional.empty();
// }
//
// if (authentication.getPrincipal() instanceof UserDetails) {
// UserDetails springSecurityUserInfo = (UserDetails) authentication.getPrincipal();
// username = springSecurityUserInfo.getUsername();
// } else if (authentication.getPrincipal() instanceof String) {
// username = (String) authentication.getPrincipal();
// }
//
// return Optional.ofNullable(userRepository.findByName(username).orElse(null));
// }
@Override
public User updateUser(User user, String newInfo) {
return null;

View File

@@ -10,6 +10,8 @@ public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByEmail(String email);
Optional<User> findByName(String name);
/**
* 이메일 중복 여부를 확인
*