-
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 #164 from Gamegoo-repo/feat/154
[Feat/154] Security μ€μ λ° Jwt @AuthMember μ°λ
- Loading branch information
Showing
15 changed files
with
492 additions
and
111 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
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
5 changes: 5 additions & 0 deletions
5
src/main/java/com/gamegoo/gamegoo_v2/account/auth/domain/Role.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,5 @@ | ||
package com.gamegoo.gamegoo_v2.account.auth.domain; | ||
|
||
public enum Role { | ||
MEMBER, ADMIN | ||
} |
66 changes: 0 additions & 66 deletions
66
src/main/java/com/gamegoo/gamegoo_v2/account/auth/jwt/JwtInterceptor.java
This file was deleted.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
src/main/java/com/gamegoo/gamegoo_v2/account/auth/security/CustomAccessDeniedHandler.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,50 @@ | ||
package com.gamegoo.gamegoo_v2.account.auth.security; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.gamegoo.gamegoo_v2.core.common.ApiResponse; | ||
import com.gamegoo.gamegoo_v2.core.exception.common.ErrorCode; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.web.access.AccessDeniedHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
|
||
@Slf4j | ||
@Component | ||
public class CustomAccessDeniedHandler implements AccessDeniedHandler { | ||
|
||
@Override | ||
public void handle(HttpServletRequest request, HttpServletResponse response, | ||
AccessDeniedException accessDeniedException) throws IOException { | ||
response.setContentType("application/json; charset=UTF-8"); | ||
response.setStatus(403); | ||
PrintWriter writer = response.getWriter(); | ||
|
||
// μ€ν¨ μλ΅ | ||
ApiResponse<Object> apiResponse = | ||
ApiResponse.builder() | ||
.code(ErrorCode._FORBIDDEN.getCode()) | ||
.message(ErrorCode._FORBIDDEN.getMessage()) | ||
.data(null) | ||
.status(ErrorCode._FORBIDDEN.getStatus()) | ||
.build(); | ||
|
||
try { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
String jsonResponse = objectMapper.writeValueAsString(apiResponse); | ||
writer.write(jsonResponse); | ||
} catch (NullPointerException e) { | ||
log.error("μλ΅ λ©μμ§ μμ± μλ¬", e); | ||
} finally { | ||
if (writer != null) { | ||
writer.flush(); | ||
writer.close(); | ||
} | ||
} | ||
} | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/com/gamegoo/gamegoo_v2/account/auth/security/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,69 @@ | ||
package com.gamegoo.gamegoo_v2.account.auth.security; | ||
|
||
import com.gamegoo.gamegoo_v2.account.auth.domain.Role; | ||
import lombok.Getter; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Getter | ||
public class CustomUserDetails implements UserDetails { | ||
|
||
Long memberId; | ||
Role role; // Member, Admin | ||
|
||
public CustomUserDetails(Long memberId, Role role) { | ||
this.memberId = memberId; | ||
this.role = role; | ||
} | ||
|
||
|
||
@Override | ||
public Collection<? extends GrantedAuthority> getAuthorities() { | ||
List<String> roles = new ArrayList<>(); | ||
roles.add("ROLE_" + role); | ||
|
||
return roles.stream() | ||
.map(SimpleGrantedAuthority::new) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public String getPassword() { | ||
return ""; | ||
} | ||
|
||
// getUserId λ©μλ λ체 | ||
@Override | ||
public String getUsername() { | ||
return memberId.toString(); | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonExpired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isAccountNonLocked() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isCredentialsNonExpired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return true; | ||
} | ||
|
||
|
||
} | ||
|
40 changes: 40 additions & 0 deletions
40
src/main/java/com/gamegoo/gamegoo_v2/account/auth/security/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,40 @@ | ||
package com.gamegoo.gamegoo_v2.account.auth.security; | ||
|
||
import com.gamegoo.gamegoo_v2.account.auth.domain.Role; | ||
import com.gamegoo.gamegoo_v2.account.member.repository.MemberRepository; | ||
import com.gamegoo.gamegoo_v2.account.member.domain.Member; | ||
import com.gamegoo.gamegoo_v2.core.exception.JwtAuthException; | ||
import com.gamegoo.gamegoo_v2.core.exception.common.ErrorCode; | ||
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; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
public class CustomUserDetailsService implements UserDetailsService { | ||
|
||
private final MemberRepository memberRepository; | ||
|
||
public UserDetails loadUserByMemberId(Long memberId) throws UsernameNotFoundException { | ||
Member member = memberRepository.findById(memberId) | ||
.orElseThrow(() -> new JwtAuthException(ErrorCode.MEMBER_NOT_FOUND)); | ||
|
||
// νν΄ν νμμΈ κ²½μ° μλ¬ λ°μ | ||
if (member.isBlind()) { | ||
throw new JwtAuthException(ErrorCode.INACTIVE_MEMBER); | ||
} | ||
|
||
return new CustomUserDetails(member.getId(), Role.MEMBER); | ||
} | ||
|
||
@Override | ||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | ||
throw new UnsupportedOperationException("loadUserByUsername(String username) is not supported. Use " + | ||
"loadUserByMemberIdAndSocialId(Long socialId, String role) instead."); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
...main/java/com/gamegoo/gamegoo_v2/account/auth/security/EntryPointUnauthorizedHandler.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,51 @@ | ||
package com.gamegoo.gamegoo_v2.account.auth.security; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.gamegoo.gamegoo_v2.core.common.ApiResponse; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
|
||
import static com.gamegoo.gamegoo_v2.core.exception.common.ErrorCode._UNAUTHORIZED; | ||
|
||
@Slf4j | ||
@Component | ||
public class EntryPointUnauthorizedHandler implements AuthenticationEntryPoint { | ||
|
||
@Override | ||
public void commence(HttpServletRequest request, HttpServletResponse response, | ||
AuthenticationException authException) throws IOException { | ||
response.setContentType("application/json; charset=UTF-8"); | ||
response.setStatus(401); | ||
PrintWriter writer = response.getWriter(); | ||
|
||
// μ€ν¨ μλ΅ | ||
ApiResponse<Object> apiResponse = | ||
ApiResponse.builder() | ||
.code(_UNAUTHORIZED.getCode()) | ||
.message(_UNAUTHORIZED.getMessage()) | ||
.data(null) | ||
.status(_UNAUTHORIZED.getStatus()) | ||
.build(); | ||
try { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
String jsonResponse = objectMapper.writeValueAsString(apiResponse); | ||
writer.write(jsonResponse); | ||
} catch (NullPointerException e) { | ||
log.error("μλ΅ λ©μμ§ μμ± μλ¬", e); | ||
} finally { | ||
if (writer != null) { | ||
writer.flush(); | ||
writer.close(); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
Oops, something went wrong.