-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BE] setting: signaling server base entity, jwt 세팅 (#30)
Signed-off-by: EunJiJung <[email protected]>
- Loading branch information
Showing
25 changed files
with
736 additions
and
17 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
36 changes: 36 additions & 0 deletions
36
...ignaling-server/src/main/java/com/asyncgate/signaling_server/config/BaseEntityConfig.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,36 @@ | ||
package com.asyncgate.signaling_server.config; | ||
|
||
import com.asyncgate.signaling_server.security.info.CustomUserPrincipal; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.domain.AuditorAware; | ||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
|
||
import java.util.Optional; | ||
|
||
@EnableJpaAuditing | ||
@Configuration | ||
public class BaseEntityConfig { | ||
|
||
@Bean("user-auditorProvider") | ||
public AuditorAware<String> auditorProvider() { | ||
return () -> { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
|
||
if (authentication == null || !authentication.isAuthenticated()) { | ||
return Optional.of("AnonymousNULL"); | ||
} | ||
|
||
Object principal = authentication.getPrincipal(); | ||
|
||
if (principal instanceof CustomUserPrincipal) { | ||
return Optional.of(((CustomUserPrincipal) principal).getId()); | ||
} | ||
|
||
return Optional.of("AnonymousNOT_TYPE"); | ||
}; | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
...kend/signaling-server/src/main/java/com/asyncgate/signaling_server/config/CorsConfig.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,45 @@ | ||
package com.asyncgate.signaling_server.config; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.web.cors.CorsConfiguration; | ||
import org.springframework.web.cors.CorsConfigurationSource; | ||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class CorsConfig { | ||
|
||
public static CorsConfigurationSource corsConfigurationSource() { | ||
CorsConfiguration configuration = new CorsConfiguration(); | ||
|
||
//리소스를 허용 | ||
ArrayList<String> allowedOriginPatterns = new ArrayList<>(); | ||
allowedOriginPatterns.add("http://localhost:5173"); // vite | ||
allowedOriginPatterns.add("http://127.0.0.1:5173"); | ||
configuration.setAllowedOrigins(allowedOriginPatterns); | ||
|
||
//허용하는 HTTP METHOD | ||
ArrayList<String> allowedHttpMethods = new ArrayList<>(); | ||
allowedHttpMethods.add("GET"); | ||
allowedHttpMethods.add("POST"); | ||
allowedHttpMethods.add("PUT"); | ||
allowedHttpMethods.add("PATCH"); | ||
allowedHttpMethods.add("DELETE"); | ||
allowedHttpMethods.add("OPTIONS"); | ||
configuration.setAllowedMethods(allowedHttpMethods); | ||
|
||
configuration.setAllowedHeaders(Collections.singletonList("*")); | ||
// configuration.setAllowedHeaders(List.of(HttpHeaders.AUTHORIZATION, HttpHeaders.CONTENT_TYPE)); | ||
|
||
//인증, 인가를 위한 credentials 를 TRUE로 설정 | ||
configuration.setAllowCredentials(true); | ||
|
||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); | ||
source.registerCorsConfiguration("/**", configuration); | ||
|
||
return source; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...nd/signaling-server/src/main/java/com/asyncgate/signaling_server/domain/Identifiable.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.asyncgate.signaling_server.domain; | ||
|
||
public interface Identifiable { | ||
String getId(); | ||
} |
35 changes: 35 additions & 0 deletions
35
...gnaling-server/src/main/java/com/asyncgate/signaling_server/entity/common/BaseEntity.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,35 @@ | ||
package com.asyncgate.signaling_server.entity.common; | ||
|
||
import com.asyncgate.user_server.entity.common.BaseTimeEntity; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.EntityListeners; | ||
import jakarta.persistence.MappedSuperclass; | ||
import lombok.Getter; | ||
import org.springframework.data.annotation.CreatedBy; | ||
import org.springframework.data.annotation.LastModifiedBy; | ||
import org.springframework.data.jpa.domain.support.AuditingEntityListener; | ||
|
||
@Getter | ||
@MappedSuperclass | ||
@EntityListeners(AuditingEntityListener.class) | ||
public abstract class BaseEntity extends BaseTimeEntity { | ||
|
||
@CreatedBy | ||
@Column(updatable = false) | ||
private String createdBy; | ||
|
||
@LastModifiedBy | ||
private String lastModifiedBy; | ||
|
||
private boolean deleted; | ||
|
||
// 재활성화 - soft delete | ||
public void activate() { | ||
this.deleted = false; | ||
} | ||
|
||
// 비활성화 - soft delete | ||
public void deactivate() { | ||
this.deleted = true; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...ing-server/src/main/java/com/asyncgate/signaling_server/entity/common/BaseTimeEntity.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,25 @@ | ||
package com.asyncgate.signaling_server.entity.common; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.EntityListeners; | ||
import jakarta.persistence.MappedSuperclass; | ||
import lombok.Getter; | ||
import org.springframework.data.annotation.CreatedDate; | ||
import org.springframework.data.annotation.LastModifiedDate; | ||
import org.springframework.data.jpa.domain.support.AuditingEntityListener; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@MappedSuperclass | ||
@EntityListeners(AuditingEntityListener.class) | ||
public abstract class BaseTimeEntity { | ||
|
||
@CreatedDate | ||
@Column(updatable = false) | ||
private LocalDateTime createdDate; | ||
|
||
@LastModifiedDate | ||
private LocalDateTime lastModifiedDate; | ||
|
||
} |
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
9 changes: 9 additions & 0 deletions
9
...ing-server/src/main/java/com/asyncgate/signaling_server/security/annotation/MemberID.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,9 @@ | ||
package com.asyncgate.signaling_server.security.annotation; | ||
|
||
import java.lang.annotation.*; | ||
|
||
@Documented | ||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface MemberID { | ||
} |
57 changes: 57 additions & 0 deletions
57
...g-server/src/main/java/com/asyncgate/signaling_server/security/config/SecurityConfig.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,57 @@ | ||
package com.asyncgate.signaling_server.security.config; | ||
|
||
import com.asyncgate.signaling_server.config.CorsConfig; | ||
import com.asyncgate.signaling_server.security.constant.Constants; | ||
import com.asyncgate.signaling_server.security.filter.JsonWebTokenAuthenticationFilter; | ||
import com.asyncgate.signaling_server.security.usecase.AuthenticateJsonWebTokenUseCase; | ||
import com.asyncgate.signaling_server.security.utility.JsonWebTokenUtil; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; | ||
import org.springframework.security.config.http.SessionCreationPolicy; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.authentication.logout.LogoutFilter; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
@RequiredArgsConstructor | ||
public class SecurityConfig { | ||
|
||
private final AuthenticateJsonWebTokenUseCase authenticateJsonWebTokenUseCase; | ||
|
||
private final JsonWebTokenUtil jsonWebTokenUtil; | ||
|
||
@Bean | ||
protected SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception { | ||
return httpSecurity | ||
.cors(cors -> cors | ||
.configurationSource(CorsConfig.corsConfigurationSource()) | ||
) | ||
.csrf(AbstractHttpConfigurer::disable) | ||
|
||
.httpBasic(AbstractHttpConfigurer::disable) | ||
|
||
.sessionManagement(configurer -> configurer | ||
.sessionCreationPolicy(SessionCreationPolicy.STATELESS) | ||
) | ||
|
||
.authorizeHttpRequests(configurer -> configurer | ||
.requestMatchers(Constants.NO_NEED_AUTH_URLS.toArray(String[]::new)).permitAll() | ||
.anyRequest().authenticated() | ||
) | ||
|
||
// 빈 주입 | ||
.addFilterBefore( | ||
new JsonWebTokenAuthenticationFilter( | ||
authenticateJsonWebTokenUseCase, | ||
jsonWebTokenUtil | ||
), | ||
LogoutFilter.class | ||
) | ||
|
||
.getOrBuild(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...naling-server/src/main/java/com/asyncgate/signaling_server/security/config/WebConfig.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.asyncgate.signaling_server.security.config; | ||
|
||
import com.asyncgate.signaling_server.security.resolver.HttpMemberIDArgumentResolver; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
import java.util.List; | ||
|
||
@Configuration | ||
public class WebConfig implements WebMvcConfigurer { | ||
|
||
private final HttpMemberIDArgumentResolver memberIDArgumentResolver; | ||
|
||
public WebConfig(HttpMemberIDArgumentResolver memberIDArgumentResolver) { | ||
this.memberIDArgumentResolver = memberIDArgumentResolver; | ||
} | ||
|
||
@Override | ||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) { | ||
resolvers.add(memberIDArgumentResolver); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...ling-server/src/main/java/com/asyncgate/signaling_server/security/constant/Constants.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,41 @@ | ||
package com.asyncgate.signaling_server.security.constant; | ||
|
||
import java.util.List; | ||
|
||
public class Constants { | ||
|
||
// JWT | ||
public static String MEMBER_ID_ATTRIBUTE_NAME = "MEMBER_ID"; | ||
public static String MEMBER_ID_CLAIM_NAME = "mid"; | ||
|
||
// HEADER | ||
public static String BEARER_PREFIX = "Bearer "; | ||
public static String AUTHORIZATION_HEADER = "Authorization"; | ||
|
||
|
||
/** | ||
* 인증이 필요 없는 URL | ||
*/ | ||
public static List<String> NO_NEED_AUTH_URLS = List.of( | ||
// Authentication/Authorization | ||
"/", // root | ||
"/actuator/info", | ||
"/health", | ||
|
||
// Swagger | ||
"/api-docs.html", | ||
"/api-docs/**", | ||
"/swagger-ui/**", | ||
"/v3/**" | ||
); | ||
|
||
/** | ||
* Swagger 에서 사용하는 URL | ||
*/ | ||
public static List<String> SWAGGER_URLS = List.of( | ||
"/api-docs.html", | ||
"/api-docs", | ||
"/swagger-ui", | ||
"/v3" | ||
); | ||
} |
16 changes: 16 additions & 0 deletions
16
...rver/src/main/java/com/asyncgate/signaling_server/security/exception/CommonException.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,16 @@ | ||
package com.asyncgate.signaling_server.security.exception; | ||
|
||
// 각 application에 맞는 failType으로 정의해주세요 ! | ||
import com.asyncgate.signaling_server.exception.FailType; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class CommonException extends RuntimeException { | ||
|
||
private final FailType failType; | ||
|
||
public CommonException(FailType failType) { | ||
super(failType.getMessage()); | ||
this.failType = failType; | ||
} | ||
} |
Oops, something went wrong.