Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
776fa2d
commit bef4466
Showing
4 changed files
with
65 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.ll.gramgram.base.rq; | ||
|
||
import com.ll.gramgram.boundedContext.member.entity.Member; | ||
import com.ll.gramgram.boundedContext.member.service.MemberService; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import jakarta.servlet.http.HttpSession; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.userdetails.User; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.context.annotation.RequestScope; | ||
|
||
@Component | ||
@RequestScope | ||
public class Rq { | ||
private final MemberService memberService; | ||
private final HttpServletRequest req; | ||
private final HttpServletResponse resp; | ||
private final HttpSession session; | ||
private final User user; | ||
private Member member = null; // 레이지 로딩, 처음부터 넣지 않고, 요청이 들어올 때 넣는다. | ||
|
||
public Rq(MemberService memberService, HttpServletRequest req, HttpServletResponse resp, HttpSession session) { | ||
this.memberService = memberService; | ||
this.req = req; | ||
this.resp = resp; | ||
this.session = session; | ||
|
||
// 현재 로그인한 회원의 인증정보를 가져옴 | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
|
||
if (authentication.getPrincipal() instanceof User) { | ||
this.user = (User) authentication.getPrincipal(); | ||
} else { | ||
this.user = null; | ||
} | ||
} | ||
|
||
// 로그인 되어 있는지 체크 | ||
public boolean isLogin() { | ||
return user != null; | ||
} | ||
|
||
// 로그아웃 되어 있는지 체크 | ||
public boolean isLogout() { | ||
return !isLogin(); | ||
} | ||
|
||
// 로그인 된 회원의 객체 | ||
public Member getMember() { | ||
if ( isLogout() ) return null; | ||
|
||
// 데이터가 없는지 체크 | ||
if ( member == null ) { | ||
member = memberService.findByUsername(user.getUsername()).orElseThrow(); | ||
} | ||
|
||
return member; | ||
} | ||
} |
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
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