From e17a5dc4096096d264d240ada1c46f5b54457981 Mon Sep 17 00:00:00 2001 From: Woo Joo Chae Date: Fri, 8 Mar 2024 20:58:11 +0900 Subject: [PATCH] [BE] FEAT: anonymous login --- .../auth/service/AuthFacadeService.java | 33 ++++++++++++++----- .../controller/LoginController.java | 30 +++++++++++++++++ 2 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 backend/src/main/java/org/ftclub/cabinet/openpublic/controller/LoginController.java diff --git a/backend/src/main/java/org/ftclub/cabinet/auth/service/AuthFacadeService.java b/backend/src/main/java/org/ftclub/cabinet/auth/service/AuthFacadeService.java index f65483994..bd4dce194 100644 --- a/backend/src/main/java/org/ftclub/cabinet/auth/service/AuthFacadeService.java +++ b/backend/src/main/java/org/ftclub/cabinet/auth/service/AuthFacadeService.java @@ -1,5 +1,11 @@ package org.ftclub.cabinet.auth.service; +import java.io.IOException; +import java.time.LocalDateTime; +import java.util.concurrent.ExecutionException; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import org.ftclub.cabinet.admin.admin.domain.Admin; import org.ftclub.cabinet.admin.admin.service.AdminCommandService; @@ -14,13 +20,6 @@ import org.ftclub.cabinet.user.service.UserQueryService; import org.springframework.stereotype.Service; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.time.LocalDateTime; -import java.util.concurrent.ExecutionException; - /** * 인증 관련 비즈니스 로직을 처리하는 서비스입니다. */ @@ -28,6 +27,7 @@ @RequiredArgsConstructor public class AuthFacadeService { + private static final String REDIRECT_COOKIE_NAME = "redirect"; private final UserQueryService userQueryService; private final UserCommandService userCommandService; private final AdminQueryService adminQueryService; @@ -35,10 +35,8 @@ public class AuthFacadeService { private final UserOauthService userOauthService; private final AdminOauthService adminOauthService; private final AuthPolicyService authPolicyService; - private final TokenProvider tokenProvider; private final CookieManager cookieManager; - private static final String REDIRECT_COOKIE_NAME = "redirect"; /** * 유저 로그인 페이지로 리다이렉트합니다. @@ -95,6 +93,23 @@ public void handleUserLogin(HttpServletRequest req, HttpServletResponse res, Str res.sendRedirect(authPolicyService.getMainHomeUrl()); } + public void handlePublicLogin(HttpServletRequest req, HttpServletResponse res, String name) + throws IOException { + + User user = userQueryService.findUser(name).orElseThrow( + ExceptionStatus.NOT_FOUND_USER::asServiceException); + String token = tokenProvider.createUserToken(user, LocalDateTime.now()); + Cookie cookie = cookieManager.cookieOf(TokenProvider.USER_TOKEN_NAME, token); + cookieManager.setCookieToClient(res, cookie, "/", req.getServerName()); + if (cookieManager.getCookieValue(req, REDIRECT_COOKIE_NAME) != null) { + String redirect = cookieManager.getCookieValue(req, REDIRECT_COOKIE_NAME); + cookieManager.deleteCookie(res, REDIRECT_COOKIE_NAME); + res.sendRedirect(redirect); + return; + } + res.sendRedirect(authPolicyService.getMainHomeUrl()); + } + /** * 관리자 로그인 콜백으로 받은 authorization_code로 관리자 프로필 정보를 가져오고, 반환합니다. *

diff --git a/backend/src/main/java/org/ftclub/cabinet/openpublic/controller/LoginController.java b/backend/src/main/java/org/ftclub/cabinet/openpublic/controller/LoginController.java new file mode 100644 index 000000000..0e52032d6 --- /dev/null +++ b/backend/src/main/java/org/ftclub/cabinet/openpublic/controller/LoginController.java @@ -0,0 +1,30 @@ +package org.ftclub.cabinet.openpublic.controller; + +import java.io.IOException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import lombok.RequiredArgsConstructor; +import org.ftclub.cabinet.alarm.config.AlarmProperties; +import org.ftclub.cabinet.auth.service.AuthFacadeService; +import org.ftclub.cabinet.log.Logging; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Logging +@RequiredArgsConstructor +@RequestMapping("/public") +@RestController +public class LoginController { + + private final AuthFacadeService authFacadeService; + private final AlarmProperties alarmProperties; + + @GetMapping("/login") + public void login(HttpServletRequest request, HttpServletResponse response) throws IOException { + final String username = "anonymous"; + if (!alarmProperties.getIsProduction()) { + authFacadeService.handlePublicLogin(request, response, username); + } + } +}