From 569346ef31cf11a716b49bfdf6829ba404ff719e Mon Sep 17 00:00:00 2001 From: "sungjin.choi" Date: Wed, 25 Nov 2026 16:47:28 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20IT=20=ED=97=A4=EB=93=9C=EB=9D=BC?= =?UTF-8?q?=EC=9D=B8=20API=20=EC=B6=94=EA=B0=80=20(GET=20/api/headlines,?= =?UTF-8?q?=20GeekNews=20=ED=94=BC=EB=93=9C=2030=EB=B6=84=20=EB=A9=94?= =?UTF-8?q?=EB=AA=A8=EB=A6=AC=20=EC=BA=90=EC=8B=9C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 브라우저에서 직접 읽으면 CORS로 막혀서 백엔드가 대신 가져옴 - 피드 장애 시 직전 목록 유지, 요청마다 재시도하지 않음 - 메인 화면용이라 인증 없이 접근 허용 Co-Authored-By: Claude Opus 5.5 --- .../specialsource/config/SecurityConfig.java | 4 +- .../controller/HeadlineController.java | 59 +++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 src/main/java/company/specialsource/controller/HeadlineController.java diff --git a/src/main/java/company/specialsource/config/SecurityConfig.java b/src/main/java/company/specialsource/config/SecurityConfig.java index 40bcd90..43704f0 100644 --- a/src/main/java/company/specialsource/config/SecurityConfig.java +++ b/src/main/java/company/specialsource/config/SecurityConfig.java @@ -31,8 +31,8 @@ public class SecurityConfig { .cors(Customizer.withDefaults()) // 아래 corsConfigurationSource 빈 사용 .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) .authorizeHttpRequests(auth -> auth - // 로그인 API 및 파일 동기화 업로드 API는 인증 없이 누구나 접근 가능하도록 허용 - .requestMatchers("/api/login", "/api/sync/upload").permitAll() + // 로그인 API, 파일 동기화 업로드 API, 메인 화면 IT 헤드라인은 인증 없이 누구나 접근 가능하도록 허용 + .requestMatchers("/api/login", "/api/sync/upload", "/api/headlines").permitAll() .anyRequest().authenticated() ) .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class); diff --git a/src/main/java/company/specialsource/controller/HeadlineController.java b/src/main/java/company/specialsource/controller/HeadlineController.java new file mode 100644 index 0000000..5d69906 --- /dev/null +++ b/src/main/java/company/specialsource/controller/HeadlineController.java @@ -0,0 +1,59 @@ +package company.specialsource.controller; + +import lombok.extern.slf4j.Slf4j; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Element; +import org.jsoup.parser.Parser; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.time.Duration; +import java.time.Instant; +import java.util.List; + +/** + * 메인 화면 IT 헤드라인: GeekNews Atom 피드를 읽어 최근 글 제목·링크를 내려준다. + * 브라우저에서 직접 읽으면 CORS로 막혀서 백엔드가 대신 가져온다. 30분 동안 메모리에 보관. + */ +@Slf4j +@RestController +public class HeadlineController { + + private static final String FEED_URL = "https://news.hada.io/rss/news"; + private static final Duration CACHE_TTL = Duration.ofMinutes(30); + private static final int LIMIT = 10; + + public record Headline(String title, String link) {} + + private List cached = List.of(); + private Instant fetchedAt = Instant.EPOCH; + + @GetMapping("/api/headlines") + public synchronized List getHeadlines() { + if (Instant.now().isBefore(fetchedAt.plus(CACHE_TTL))) { + return cached; + } + // 실패해도 시각은 갱신 → 피드 장애 때 요청마다 다시 시도하지 않고, 직전 목록을 그대로 보여줌 + fetchedAt = Instant.now(); + try { + cached = Jsoup.connect(FEED_URL) // feedburner로 리다이렉트됨 (Jsoup이 자동으로 따라감) + .userAgent("Mozilla/5.0") + .timeout(10_000) + .parser(Parser.xmlParser()) + .get() + .select("entry") + .stream() + .limit(LIMIT) + .map(e -> new Headline(text(e, "title"), text(e, "id"))) // id = utm 없는 깨끗한 글 주소 + .toList(); + } catch (Exception e) { + log.warn("헤드라인 피드를 가져오지 못했습니다: {}", e.getMessage()); + } + return cached; + } + + private static String text(Element entry, String tag) { + Element el = entry.selectFirst(tag); + return el == null ? "" : el.text(); + } +}