티스토리 뷰
@GetMapping("/posts/{id}")
public String showPost(Model model, @PathVariable Long id, @AuthenticationPrincipal UserDetailsImpl userDetails){
Post post = postRepository.findById(id).orElseThrow(
() -> new IllegalArgumentException("게시글을 찾을 수 없습니다.")
);
model.addAttribute("username", userDetails.getUsername());
model.addAttribute("id", id);
model.addAttribute("title", post.getTitle());
model.addAttribute("writer", post.getWriter());
model.addAttribute("modifiedAt", post.getModifiedAt());
model.addAttribute("contents", post.getContents());
return "post";
}
사용자가 토큰을 가지고 로그인을 했음에도 불구하고 계속 @Authentication에서 null이 반환되는 문제가 발생한다...
$(document).ready(function () {
if ($.cookie('token')) {
$.ajaxSetup({
headers: {
'Authorization': $.cookie('token')
}
})
}
$.ajax({
type: "POST",
url: "/user/userinfo",
contentType: "application/json",
success: function (response) {
const username = response.username;
$('#username').text(username);
},
error: function() {
window.location.href = '/';
}
})
})
$(docuemt).ready가 작동할 때마다 userinfo를 받아와주니 굳이 매범 @AuthenicationPrincipal을 받아올 필요는 없다.
매니저님이랑 상담했는데 너무 프론트엔드적인 요소까지 같이 생각하려니 너무 복잡한 것 같다고 한소리 들었다..ㅎㅎ ARC를 사용하면서 데이터가 잘 들어가는지, 작동이 되는지를 생각해봐야하는데 데이터가 잘 들어가는 지의 문제가 아니라 홈화면에서 끙끙 앓고 있으니 답답하셨나보다.
ArgsConstructor
@NoArgsConstructor
@RequiredArgsConstructor
@AllArgsConstructor
User user1 = new User(); // @NoArgsConstructor
User user2 = new User("user2", "1234"); // @RequiredArgsConstructor
User user3 = new User(1L, "user3", "1234", null); // @AllArgsConstructor
JQuery 선택자를 선택할 경우
$('#getWriter' + commentId)과 같은 변수를 포함한 선택자는 에러가 나지만
let str = 'getWriter' + commentId를 선언한 후
$('#' + str)은 가능하다.
'TIL(Today I Learn)' 카테고리의 다른 글
TIL 220603 (0) | 2022.06.03 |
---|---|
TIL 220603 (0) | 2022.06.03 |
TIL 220531 (0) | 2022.06.01 |
WIL 3주차(Week I Learn) (0) | 2022.05.30 |
TIL 220527 (0) | 2022.05.27 |
댓글