-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
524 additions
and
8 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
19 changes: 19 additions & 0 deletions
19
backend/src/main/java/ddangkong/config/AdminAuthConfig.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,19 @@ | ||
package ddangkong.config; | ||
|
||
import ddangkong.controller.admin.AdminAuthorizationInterceptor; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class AdminAuthConfig implements WebMvcConfigurer { | ||
|
||
private final AdminAuthorizationInterceptor adminAuthorizationInterceptor; | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(adminAuthorizationInterceptor); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
backend/src/main/java/ddangkong/controller/admin/Admin.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,14 @@ | ||
package ddangkong.controller.admin; | ||
|
||
import java.io.Serializable; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class Admin implements Serializable { | ||
|
||
private final String nickname; | ||
|
||
public Admin(String nickname) { | ||
this.nickname = nickname; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
backend/src/main/java/ddangkong/controller/admin/AdminAuth.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,12 @@ | ||
package ddangkong.controller.admin; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.TYPE, ElementType.METHOD}) | ||
public @interface AdminAuth { | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
backend/src/main/java/ddangkong/controller/admin/AdminAuthorizationInterceptor.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,46 @@ | ||
package ddangkong.controller.admin; | ||
|
||
import ddangkong.exception.admin.NotExistAdminSessionException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import jakarta.servlet.http.HttpSession; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.method.HandlerMethod; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; | ||
|
||
@Component | ||
public class AdminAuthorizationInterceptor implements HandlerInterceptor { | ||
|
||
private final String sessionKey; | ||
|
||
public AdminAuthorizationInterceptor(@Value("${admin.session-key}") String sessionKey) { | ||
this.sessionKey = sessionKey; | ||
} | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
if (hasAdminAuthAnnotation(handler)) { | ||
authorizeAdmin(request); | ||
} | ||
return true; | ||
} | ||
|
||
private boolean hasAdminAuthAnnotation(Object handler) { | ||
if (handler instanceof ResourceHttpRequestHandler) { | ||
return false; | ||
} | ||
|
||
HandlerMethod handlerMethod = (HandlerMethod) handler; | ||
return handlerMethod.getMethodAnnotation(AdminAuth.class) != null || | ||
handlerMethod.getBeanType().getAnnotation(AdminAuth.class) != null; | ||
} | ||
|
||
private void authorizeAdmin(HttpServletRequest request) { | ||
HttpSession session = request.getSession(false); | ||
if (session == null || session.getAttribute(sessionKey) == null) { | ||
throw new NotExistAdminSessionException(); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
backend/src/main/java/ddangkong/controller/admin/AdminController.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,48 @@ | ||
package ddangkong.controller.admin; | ||
|
||
import ddangkong.facade.admin.AdminService; | ||
import ddangkong.facade.admin.dto.AdminLoginRequest; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpSession; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequestMapping("/api") | ||
public class AdminController { | ||
|
||
private final AdminService adminService; | ||
|
||
private final String sessionKey; | ||
|
||
public AdminController(AdminService adminService, @Value("${admin.session-key}") String sessionKey) { | ||
this.adminService = adminService; | ||
this.sessionKey = sessionKey; | ||
} | ||
|
||
@PostMapping("/admin/login") | ||
public void login(@RequestBody AdminLoginRequest loginRequest, HttpServletRequest httpRequest) { | ||
adminService.validatePassword(loginRequest); | ||
|
||
Admin admin = new Admin(loginRequest.nickname()); | ||
HttpSession session = httpRequest.getSession(); | ||
session.setAttribute(sessionKey, admin); | ||
log.info("어드민이 로그인 했습니다. nickname = {}, session = {}", loginRequest.nickname(), session.getId()); | ||
} | ||
|
||
@AdminAuth | ||
@PostMapping("/admin/logout") | ||
public void logout(HttpServletRequest request) { | ||
HttpSession session = request.getSession(false); | ||
if (session != null) { | ||
Admin admin = (Admin) session.getAttribute(sessionKey); | ||
session.invalidate(); | ||
log.info("어드민이 로그아웃 했습니다. nickname = {}, session = {}", admin.getNickname(), session.getId()); | ||
} | ||
} | ||
} |
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
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
10 changes: 10 additions & 0 deletions
10
backend/src/main/java/ddangkong/exception/UnauthorizedException.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,10 @@ | ||
package ddangkong.exception; | ||
|
||
public abstract class UnauthorizedException extends RuntimeException { | ||
|
||
public UnauthorizedException(String message) { | ||
super(message); | ||
} | ||
|
||
public abstract String getErrorCode(); | ||
} |
17 changes: 17 additions & 0 deletions
17
backend/src/main/java/ddangkong/exception/admin/NotExistAdminSessionException.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,17 @@ | ||
package ddangkong.exception.admin; | ||
|
||
import static ddangkong.exception.ClientErrorCode.NOT_EXIST_ADMIN_SESSION; | ||
|
||
import ddangkong.exception.UnauthorizedException; | ||
|
||
public class NotExistAdminSessionException extends UnauthorizedException { | ||
|
||
public NotExistAdminSessionException() { | ||
super(NOT_EXIST_ADMIN_SESSION.getMessage()); | ||
} | ||
|
||
@Override | ||
public String getErrorCode() { | ||
return NOT_EXIST_ADMIN_SESSION.name(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
backend/src/main/java/ddangkong/exception/admin/NotMatchAdminPasswordException.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,17 @@ | ||
package ddangkong.exception.admin; | ||
|
||
import static ddangkong.exception.ClientErrorCode.NOT_MATCH_ADMIN_PASSWORD; | ||
|
||
import ddangkong.exception.BadRequestException; | ||
|
||
public class NotMatchAdminPasswordException extends BadRequestException { | ||
|
||
public NotMatchAdminPasswordException() { | ||
super(NOT_MATCH_ADMIN_PASSWORD.getMessage()); | ||
} | ||
|
||
@Override | ||
public String getErrorCode() { | ||
return NOT_MATCH_ADMIN_PASSWORD.name(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
backend/src/main/java/ddangkong/facade/admin/AdminService.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 ddangkong.facade.admin; | ||
|
||
import ddangkong.exception.admin.NotMatchAdminPasswordException; | ||
import ddangkong.facade.admin.dto.AdminLoginRequest; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class AdminService { | ||
|
||
private final String adminPassword; | ||
|
||
public AdminService(@Value("${admin.password}") String adminPassword) { | ||
this.adminPassword = adminPassword; | ||
} | ||
|
||
public void validatePassword(AdminLoginRequest request) { | ||
if (!adminPassword.equals(request.password())) { | ||
throw new NotMatchAdminPasswordException(); | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
backend/src/main/java/ddangkong/facade/admin/dto/AdminLoginRequest.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,4 @@ | ||
package ddangkong.facade.admin.dto; | ||
|
||
public record AdminLoginRequest(String nickname, String password) { | ||
} |
Oops, something went wrong.