Change type from User to UserDetails

This commit is contained in:
hou27
2022-05-29 04:50:17 +09:00
parent 9a7ea77820
commit 9037916237
4 changed files with 34 additions and 23 deletions

View File

@@ -37,6 +37,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
// web.ignoring().antMatchers("/?/**"); // web.ignoring().antMatchers("/?/**");
// } // }
/**
* 인증에 대한 지원
*/
@Override @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception { protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder()); auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());

View File

@@ -7,7 +7,12 @@ import demo.api.user.exception.UserNotFoundException;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@@ -15,7 +20,6 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/** /**
* User 관련 HTTP 요청 처리 * User 관련 HTTP 요청 처리
@@ -40,22 +44,24 @@ public class UserController {
@GetMapping("/signIn") @GetMapping("/signIn")
public String signIn(@RequestParam(value = "fail", required = false) String flag, Model model) { public String signIn(@RequestParam(value = "fail", required = false) String flag, Model model) {
if(flag == null || !flag.equals("true")) { model.addAttribute("failed", flag != null);
model.addAttribute("failed", false);
return "user/signIn"; return "user/signIn";
}
else {
model.addAttribute("failed", true);
return "user/signIn";
}
} }
// @Autowired
// private UserDetailsService userDetailsService;
@GetMapping("/profile") @GetMapping("/profile")
public String profile(Model model, @AuthenticationPrincipal User user) { public String profile(Model model, @AuthenticationPrincipal UserDetails userDetails) {
User userDetail = userService.findByEmail(user.getEmail()) // Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
.orElseThrow(() -> new UserNotFoundException()); // System.out.println("principal : " + authentication.getPrincipal());
// System.out.println("Implementing class of UserDetails: " + authentication.getPrincipal().getClass());
// System.out.println("Implementing class of UserDetailsService: " + userDetailsService.getClass());
if (userDetails != null) {
User userDetail = userService.findByEmail(userDetails.getUsername())
.orElseThrow(() -> new UserNotFoundException());
model.addAttribute("userDetail", userDetail); model.addAttribute("userDetail", userDetail);
}
return "user/profile"; return "user/profile";
} }

View File

@@ -5,16 +5,16 @@
<div> <div>
<table> <table>
<thead> <thead>
<tr> <tr>
<th>#</th> <th>#</th>
<th>My Profile</th> <th>My Profile</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr> <tr th:if="${userDetail}">
<td th:text="${userDetail.get().email}"></td> <td th:text="${userDetail.getEmail()}"></td>
<td th:text="${userDetail.get().name}"></td> <td th:text="${userDetail.getName()}"></td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
</div> </div>

View File

@@ -18,6 +18,9 @@ import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@@ -82,7 +85,6 @@ class UserServiceTest {
System.out.println("flag = " + flag); System.out.println("flag = " + flag);
// then // then
} }
@Test @Test