first commit
This commit is contained in:
21
src/main/java/com/krt/newssak/config/QueryDslConfig.java
Normal file
21
src/main/java/com/krt/newssak/config/QueryDslConfig.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package com.krt.newssak.config;
|
||||
|
||||
import com.querydsl.jpa.impl.JPAQueryFactory;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
||||
|
||||
@EnableJpaAuditing
|
||||
@Configuration
|
||||
public class QueryDslConfig {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Bean
|
||||
public JPAQueryFactory jpaQueryFactory() {
|
||||
return new JPAQueryFactory(this.entityManager);
|
||||
}
|
||||
}
|
||||
70
src/main/java/com/krt/newssak/config/SecurityConfig.java
Normal file
70
src/main/java/com/krt/newssak/config/SecurityConfig.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package com.krt.newssak.config;
|
||||
|
||||
import com.krt.newssak.jwt.JwtAuthenticationFilter;
|
||||
import com.krt.newssak.jwt.JwtTokenProvider;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.Customizer;
|
||||
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.configuration.WebSecurityCustomizer;
|
||||
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
||||
import org.springframework.security.config.annotation.web.configurers.HttpBasicConfigurer;
|
||||
import org.springframework.security.config.http.SessionCreationPolicy;
|
||||
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
@RequiredArgsConstructor
|
||||
public class SecurityConfig {
|
||||
|
||||
private final JwtTokenProvider jwtTokenProvider;
|
||||
|
||||
@Bean
|
||||
public WebSecurityCustomizer webSecurityCustomizer() {
|
||||
return (web) -> web.ignoring().requestMatchers("/v2/api-docs", "/v3/api-docs/**", "/configuration/ui",
|
||||
"/swagger-resources/**", "/configuration/security",
|
||||
"/swagger-ui/**", "/webjars/**","/swagger/**");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PasswordEncoder passwordEncoder() {
|
||||
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
|
||||
}
|
||||
|
||||
|
||||
//Security 6 이상은 꼭 커스텀해야함. 이전에 쓰던 코드들은 스프링 시큐리티 지원 중단됨.
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http
|
||||
.httpBasic(HttpBasicConfigurer::disable)
|
||||
.csrf(AbstractHttpConfigurer::disable)
|
||||
.cors(Customizer.withDefaults())
|
||||
.sessionManagement((sessionManagement) ->
|
||||
sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
|
||||
)
|
||||
.authorizeHttpRequests((authorizeRequests) ->
|
||||
authorizeRequests.requestMatchers("api/v1/member/login","files/**","ws/api/**","api/v1/list/downloadList","api/v1/kiwi/**").permitAll()
|
||||
.anyRequest().authenticated()
|
||||
)
|
||||
.addFilterBefore(new JwtAuthenticationFilter(jwtTokenProvider), UsernamePasswordAuthenticationFilter.class);
|
||||
return http.build();
|
||||
}
|
||||
|
||||
// @Bean
|
||||
// public CorsConfigurationSource corsConfigurationSource() {
|
||||
// CorsConfiguration configuration = new CorsConfiguration();
|
||||
// configuration.setAllowedOrigins(Arrays.asList("*"));
|
||||
// configuration.setAllowedMethods(Arrays.asList("*"));
|
||||
// configuration.setAllowedHeaders(Arrays.asList("Authorization", "Cache-Control", "Content-Type"));
|
||||
// configuration.setAllowCredentials(true);
|
||||
// UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||
// source.registerCorsConfiguration("/**", configuration);
|
||||
// return source;
|
||||
// }
|
||||
|
||||
}
|
||||
47
src/main/java/com/krt/newssak/config/SwaggerConfig.java
Normal file
47
src/main/java/com/krt/newssak/config/SwaggerConfig.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package com.krt.newssak.config;
|
||||
|
||||
import io.swagger.v3.oas.models.Components;
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.info.Info;
|
||||
import io.swagger.v3.oas.models.security.SecurityRequirement;
|
||||
import io.swagger.v3.oas.models.security.SecurityScheme;
|
||||
import io.swagger.v3.oas.models.servers.Server;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.core.context.SecurityContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
//http://localhost:7000/swagger-ui/index.html
|
||||
@Configuration
|
||||
public class SwaggerConfig {
|
||||
|
||||
|
||||
@Bean
|
||||
public OpenAPI newSsakOpenAPI() {
|
||||
Info info = new Info()
|
||||
.version("v1.0.0")
|
||||
.title("새싹 씨앗 API")
|
||||
.description("새싹 구동을 위한 세팅과 관련된 API");
|
||||
|
||||
// SecuritySecheme명
|
||||
String jwtSchemeName = "jwtAuth";
|
||||
// API 요청헤더에 인증정보 포함
|
||||
SecurityRequirement securityRequirement = new SecurityRequirement().addList(jwtSchemeName);
|
||||
// SecuritySchemes 등록
|
||||
Components components = new Components()
|
||||
.addSecuritySchemes(jwtSchemeName, new SecurityScheme()
|
||||
.name(jwtSchemeName)
|
||||
.type(SecurityScheme.Type.HTTP) // HTTP 방식
|
||||
.scheme("bearer")
|
||||
.bearerFormat("JWT")); // 토큰 형식을 지정하는 임의의 문자(Optional)
|
||||
|
||||
return new OpenAPI()
|
||||
.info(info)
|
||||
.addServersItem(new Server().url("/"))
|
||||
.addSecurityItem(securityRequirement)
|
||||
.components(components);
|
||||
}
|
||||
}
|
||||
35
src/main/java/com/krt/newssak/config/WebConfig.java
Normal file
35
src/main/java/com/krt/newssak/config/WebConfig.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package com.krt.newssak.config;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import com.krt.newssak.Interceptor.IpAccessControlInterceptor;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@RequiredArgsConstructor
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
private final IpAccessControlInterceptor ipAccessControlInterceptor;
|
||||
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/**")
|
||||
.allowedOriginPatterns("*")
|
||||
.allowedMethods("*")
|
||||
.allowedHeaders("*")
|
||||
.allowCredentials(true)
|
||||
.maxAge(3000);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
|
||||
registry.addInterceptor(ipAccessControlInterceptor)
|
||||
.order(1)
|
||||
.addPathPatterns("/swagger-ui/**", "/v3/api-docs/**");
|
||||
}
|
||||
}
|
||||
44
src/main/java/com/krt/newssak/config/WebSocketConfig.java
Normal file
44
src/main/java/com/krt/newssak/config/WebSocketConfig.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package com.krt.newssak.config;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
|
||||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSocketMessageBroker
|
||||
@RequiredArgsConstructor
|
||||
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer{
|
||||
@Override
|
||||
public void registerStompEndpoints(StompEndpointRegistry registry) {
|
||||
//웹소켓 연결 URL
|
||||
registry.addEndpoint("/ws/api")
|
||||
.setAllowedOriginPatterns("*")
|
||||
.withSockJS();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configureMessageBroker(MessageBrokerRegistry registry) {
|
||||
//구독자들에게 보낼 URL
|
||||
registry.setApplicationDestinationPrefixes("/pub")
|
||||
//서버로 보낼 URL
|
||||
.enableSimpleBroker("/sub")
|
||||
//.setHeartbeatValue(new long[]{10000, 20000})
|
||||
//.setTaskScheduler(heartBeatScheduler())
|
||||
;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TaskScheduler heartBeatScheduler(){
|
||||
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
|
||||
scheduler.setPoolSize(1);
|
||||
scheduler.setThreadNamePrefix("websocket-heartbeat-thread-");
|
||||
scheduler.initialize();
|
||||
return scheduler;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user