-
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 #13 from Team-INSERT/Feat/Security
Feat/security
- Loading branch information
Showing
46 changed files
with
1,229 additions
and
35 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
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,24 @@ | ||
package com.insert.ogbsm.domain.auth; | ||
|
||
import lombok.*; | ||
import org.springframework.data.redis.core.RedisHash; | ||
import org.springframework.data.redis.core.TimeToLive; | ||
import org.springframework.data.redis.core.index.Indexed; | ||
|
||
import javax.persistence.Id; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
@Builder | ||
@RedisHash | ||
public class AuthId { | ||
@Id | ||
String id; | ||
|
||
@Indexed | ||
String authId; | ||
|
||
@TimeToLive | ||
private long ttl; | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/insert/ogbsm/domain/auth/RefreshToken.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,34 @@ | ||
package com.insert.ogbsm.domain.auth; | ||
|
||
import lombok.*; | ||
import org.springframework.data.redis.core.RedisHash; | ||
import org.springframework.data.redis.core.TimeToLive; | ||
import org.springframework.data.redis.core.index.Indexed; | ||
|
||
import javax.persistence.Id; | ||
import java.time.ZonedDateTime; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
@Builder | ||
@RedisHash | ||
public class RefreshToken { | ||
@Id | ||
private String id; | ||
@Indexed | ||
private String refreshToken; | ||
|
||
private String role; | ||
|
||
@TimeToLive | ||
private long ttl; | ||
|
||
private ZonedDateTime expiredAt; | ||
|
||
public RefreshToken update(final String refreshToken, final long ttl) { | ||
this.refreshToken = refreshToken; | ||
this.ttl = ttl; | ||
return this; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/insert/ogbsm/domain/auth/exception/InvalidClientException.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,13 @@ | ||
package com.insert.ogbsm.domain.auth.exception; | ||
|
||
import com.insert.ogbsm.domain.user.exception.UserNotFoundException; | ||
import com.insert.ogbsm.global.error.exception.BsmException; | ||
import com.insert.ogbsm.global.error.exception.ErrorCode; | ||
|
||
public class InvalidClientException extends BsmException { | ||
public static final UserNotFoundException EXCEPTION = new UserNotFoundException(ErrorCode.BSM_AUTH_INVALID_CLIENT); | ||
|
||
public InvalidClientException(ErrorCode errorCode) { | ||
super(errorCode); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/insert/ogbsm/domain/auth/repo/AuthIdRepo.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,11 @@ | ||
package com.insert.ogbsm.domain.auth.repo; | ||
|
||
import com.insert.ogbsm.domain.auth.AuthId; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface AuthIdRepo extends CrudRepository<AuthId, String> { | ||
Optional<AuthId> findByAuthId(String authId); | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/insert/ogbsm/domain/auth/repo/RefreshTokenRepo.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,11 @@ | ||
package com.insert.ogbsm.domain.auth.repo; | ||
|
||
import com.insert.ogbsm.domain.auth.RefreshToken; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface RefreshTokenRepo extends CrudRepository<RefreshToken, String> { | ||
Optional<RefreshToken> findById(String authId); | ||
Optional<RefreshToken> findByRefreshToken(String refreshToken); | ||
} |
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
13 changes: 13 additions & 0 deletions
13
src/main/java/com/insert/ogbsm/domain/user/exception/UserNotFoundException.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,13 @@ | ||
package com.insert.ogbsm.domain.user.exception; | ||
|
||
import com.insert.ogbsm.global.error.exception.BsmException; | ||
import com.insert.ogbsm.global.error.exception.ErrorCode; | ||
|
||
public class UserNotFoundException extends BsmException { | ||
|
||
public static final UserNotFoundException EXCEPTION = new UserNotFoundException(ErrorCode.USER_NOT_FOUND); | ||
|
||
public UserNotFoundException(ErrorCode errorCode) { | ||
super(errorCode); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/insert/ogbsm/domain/user/exception/UserNotLoginException.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,13 @@ | ||
package com.insert.ogbsm.domain.user.exception; | ||
|
||
import com.insert.ogbsm.global.error.exception.BsmException; | ||
import com.insert.ogbsm.global.error.exception.ErrorCode; | ||
|
||
public class UserNotLoginException extends BsmException { | ||
|
||
public static final UserNotFoundException EXCEPTION = new UserNotFoundException(ErrorCode.USER_NOT_LOGIN); | ||
|
||
public UserNotLoginException(ErrorCode errorCode) { | ||
super(errorCode); | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
src/main/java/com/insert/ogbsm/global/error/CustomAuthenticationEntryPoint.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,30 @@ | ||
package com.insert.ogbsm.global.error; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import com.insert.ogbsm.global.error.exception.ErrorCode; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
|
||
import java.io.IOException; | ||
|
||
@RequiredArgsConstructor | ||
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
@Override | ||
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException { | ||
ErrorCode errorCode = ErrorCode.FORBIDDEN; | ||
String errorResponseJson = objectMapper.writeValueAsString( | ||
new ErrorResponse(errorCode.getStatus(), errorCode.getCode(), errorCode.getMessage())); | ||
|
||
response.setStatus(errorCode.getStatus()); | ||
response.setContentType(MediaType.APPLICATION_JSON_VALUE); | ||
response.getWriter().write(errorResponseJson); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/insert/ogbsm/global/error/ErrorResponse.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,23 @@ | ||
package com.insert.ogbsm.global.error; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class ErrorResponse { | ||
private int status; | ||
private String code; | ||
private String message; | ||
|
||
public String toString() { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
try { | ||
return objectMapper.writeValueAsString(this); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/insert/ogbsm/global/error/ExceptionFilter.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,42 @@ | ||
package com.insert.ogbsm.global.error; | ||
|
||
import com.insert.ogbsm.global.error.exception.BsmException; | ||
import com.insert.ogbsm.global.error.exception.ErrorCode; | ||
import io.jsonwebtoken.ExpiredJwtException; | ||
import io.jsonwebtoken.JwtException; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
|
||
public class ExceptionFilter extends OncePerRequestFilter { | ||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException { | ||
try { | ||
filterChain.doFilter(request, response); | ||
} catch (BsmException e) { | ||
writeErrorCode(response, e.getErrorCode()); | ||
} catch (ExpiredJwtException e) { | ||
writeErrorCode(response, ErrorCode.EXPIRED_JWT); | ||
} catch (JwtException e) { | ||
writeErrorCode(response, ErrorCode.INVALID_TOKEN); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
writeErrorCode(response, ErrorCode.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
|
||
private void writeErrorCode(HttpServletResponse response, ErrorCode errorCode) throws IOException { | ||
ErrorResponse errorResponse = new ErrorResponse( | ||
errorCode.getStatus(), errorCode.getCode(), errorCode.getMessage() | ||
); | ||
|
||
response.setStatus(errorResponse.getStatus()); | ||
response.setCharacterEncoding("UTF-8"); | ||
response.setContentType(MediaType.APPLICATION_JSON_VALUE); | ||
response.getWriter().write(errorResponse.toString()); | ||
} | ||
} |
Oops, something went wrong.