-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: security 의존성 추가, cors 설정 (#21)
* Chore: security 의존성 추가 * Feat: 토큰 타입을 상수로 정의 * Feat: swagger security 설정 추가 * Feat: auth 관련 error type 추가 * Chore: cors origin 환경 변수 추가 * Feat: security 관련 웹 url 설정 추가 * Test: allow origin 테스트용 추가
- Loading branch information
Showing
14 changed files
with
261 additions
and
5 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
22 changes: 22 additions & 0 deletions
22
src/main/java/com/dnd/runus/auth/exception/AuthException.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,22 @@ | ||
package com.dnd.runus.auth.exception; | ||
|
||
import com.dnd.runus.global.exception.type.ErrorType; | ||
import lombok.Getter; | ||
import org.springframework.security.core.AuthenticationException; | ||
|
||
@Getter | ||
public class AuthException extends AuthenticationException { | ||
private final ErrorType type; | ||
private final String message; | ||
|
||
public AuthException(ErrorType type, String message) { | ||
super(message); | ||
this.type = type; | ||
this.message = message; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "AUTH 에러 타입: " + type + ", 사유: " + message; | ||
} | ||
} |
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/dnd/runus/global/constant/AuthConstant.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.dnd.runus.global.constant; | ||
|
||
public final class AuthConstant { | ||
public static final String TOKEN_TYPE = "Bearer "; | ||
} |
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
21 changes: 21 additions & 0 deletions
21
src/main/java/com/dnd/runus/presentation/config/SecurityFilterConfig.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,21 @@ | ||
package com.dnd.runus.presentation.config; | ||
|
||
import com.dnd.runus.presentation.filter.AuthenticationCheckFilter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.filter.ForwardedHeaderFilter; | ||
|
||
@RequiredArgsConstructor | ||
@Configuration | ||
public class SecurityFilterConfig { | ||
@Bean | ||
AuthenticationCheckFilter authenticationCheckFilter() { | ||
return new AuthenticationCheckFilter(); | ||
} | ||
|
||
@Bean | ||
ForwardedHeaderFilter forwardedHeaderFilter() { | ||
return new ForwardedHeaderFilter(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/dnd/runus/presentation/config/SecurityHttpConfigurer.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.dnd.runus.presentation.config; | ||
|
||
import com.dnd.runus.presentation.filter.AuthenticationCheckFilter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.config.annotation.SecurityConfigurer; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.web.DefaultSecurityFilterChain; | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
import org.springframework.stereotype.Component; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class SecurityHttpConfigurer implements SecurityConfigurer<DefaultSecurityFilterChain, HttpSecurity> { | ||
private final AuthenticationCheckFilter authenticationCheckFilter; | ||
|
||
@Override | ||
public void init(HttpSecurity httpSecurity) {} | ||
|
||
@Override | ||
public void configure(HttpSecurity httpSecurity) { | ||
httpSecurity.addFilterBefore(authenticationCheckFilter, UsernamePasswordAuthenticationFilter.class); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/main/java/com/dnd/runus/presentation/config/SecurityWebConfig.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,83 @@ | ||
package com.dnd.runus.presentation.config; | ||
|
||
import com.dnd.runus.presentation.handler.UnauthorizedHandler; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
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.util.matcher.AntPathRequestMatcher; | ||
import org.springframework.web.cors.CorsConfiguration; | ||
import org.springframework.web.cors.CorsConfigurationSource; | ||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; | ||
|
||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import static org.springframework.http.HttpHeaders.AUTHORIZATION; | ||
import static org.springframework.http.HttpHeaders.SET_COOKIE; | ||
|
||
@EnableMethodSecurity(securedEnabled = true) | ||
@RequiredArgsConstructor | ||
@Configuration | ||
public class SecurityWebConfig { | ||
private final UnauthorizedHandler unauthorizedHandler; | ||
private final SecurityHttpConfigurer securityHttpConfigurer; | ||
|
||
@Value("${app.api.allow-origins}") | ||
private List<String> corsOrigins; | ||
|
||
private static final String[] PUBLIC_ENDPOINTS = { | ||
"/api/v1/auth/**", | ||
}; | ||
|
||
private static final String[] OPEN_API_ENDPOINTS = { | ||
"/v3/api-docs/**", "/swagger-ui/**", "/swagger-resources/**", | ||
}; | ||
|
||
private static final String[] READ_ONLY_ENDPOINTS = { | ||
"/api/v1/examples/**", // TODO: Remove test endpoints | ||
}; | ||
|
||
@Bean | ||
SecurityFilterChain defaultSecurityFilterChain(HttpSecurity httpSecurity) throws Exception { | ||
AntPathRequestMatcher[] readOnlyEndpoints = Stream.of(READ_ONLY_ENDPOINTS) | ||
.map(path -> new AntPathRequestMatcher(path, HttpMethod.GET.name())) | ||
.toArray(AntPathRequestMatcher[]::new); | ||
|
||
httpSecurity | ||
.csrf(AbstractHttpConfigurer::disable) | ||
.cors(httpSecurityCorsConfigurer -> corsConfigurationSource()) | ||
.authorizeHttpRequests(auth -> { | ||
auth.requestMatchers(PUBLIC_ENDPOINTS).permitAll(); | ||
auth.requestMatchers(OPEN_API_ENDPOINTS).permitAll(); | ||
auth.requestMatchers(readOnlyEndpoints).permitAll(); | ||
auth.anyRequest().authenticated(); | ||
}) | ||
.sessionManagement(s -> s.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) | ||
.exceptionHandling(auth -> auth.authenticationEntryPoint(unauthorizedHandler)); | ||
|
||
httpSecurity.apply(securityHttpConfigurer); | ||
|
||
return httpSecurity.build(); | ||
} | ||
|
||
@Bean | ||
CorsConfigurationSource corsConfigurationSource() { | ||
CorsConfiguration configuration = new CorsConfiguration(); | ||
configuration.setAllowedOrigins(corsOrigins); | ||
configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "PATCH", "DELETE")); | ||
configuration.setAllowedHeaders(List.of("*")); | ||
configuration.setMaxAge(3600L); // Cache preflight | ||
configuration.setExposedHeaders(List.of(SET_COOKIE, AUTHORIZATION)); | ||
configuration.setAllowCredentials(true); | ||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); | ||
source.registerCorsConfiguration("/**", configuration); | ||
return source; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/dnd/runus/presentation/filter/AuthenticationCheckFilter.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,26 @@ | ||
package com.dnd.runus.presentation.filter; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class AuthenticationCheckFilter extends OncePerRequestFilter { | ||
@Override | ||
protected void doFilterInternal( | ||
@NonNull HttpServletRequest request, | ||
@NonNull HttpServletResponse response, | ||
@NonNull FilterChain filterChain) | ||
throws ServletException, IOException { | ||
// TODO: Implement authentication check logic | ||
filterChain.doFilter(request, response); | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
src/main/java/com/dnd/runus/presentation/handler/UnauthorizedHandler.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,24 @@ | ||
package com.dnd.runus.presentation.handler; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.AuthenticationEntryPoint; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.HandlerExceptionResolver; | ||
|
||
@Component | ||
public class UnauthorizedHandler implements AuthenticationEntryPoint { | ||
private final HandlerExceptionResolver resolver; | ||
|
||
public UnauthorizedHandler(@Qualifier("handlerExceptionResolver") HandlerExceptionResolver resolver) { | ||
this.resolver = resolver; | ||
} | ||
|
||
@Override | ||
public void commence( | ||
HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) { | ||
resolver.resolveException(request, response, null, authException); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -3,3 +3,5 @@ DATABASE_PORT= | |
DATABASE_NAME= | ||
DATABASE_USER= | ||
DATABASE_PASSWORD= | ||
|
||
ALLOW_ORIGINS= |
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