|
| 1 | +package io.f1.backend.global.filter; |
| 2 | + |
| 3 | +import io.f1.backend.domain.admin.dto.AdminPrincipal; |
| 4 | +import io.f1.backend.domain.admin.entity.Admin; |
| 5 | +import io.f1.backend.domain.user.dto.UserPrincipal; |
| 6 | +import io.f1.backend.domain.user.entity.User; |
| 7 | +import jakarta.servlet.FilterChain; |
| 8 | +import jakarta.servlet.ServletException; |
| 9 | +import jakarta.servlet.http.HttpServletRequest; |
| 10 | +import jakarta.servlet.http.HttpServletResponse; |
| 11 | +import java.io.IOException; |
| 12 | +import java.time.LocalDateTime; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 16 | +import org.springframework.security.core.Authentication; |
| 17 | +import org.springframework.security.core.GrantedAuthority; |
| 18 | +import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| 19 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 20 | +import org.springframework.stereotype.Component; |
| 21 | +import org.springframework.web.filter.OncePerRequestFilter; |
| 22 | + |
| 23 | +@Component |
| 24 | +public class DevTokenAuthFilter extends OncePerRequestFilter { |
| 25 | + |
| 26 | + private static final String DEV_TOKEN = "dev-secret-token-1234"; |
| 27 | + private static final String ADMIN_TOKEN = "admin-secret-token-1234"; |
| 28 | + |
| 29 | + @Override |
| 30 | + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, |
| 31 | + FilterChain filterChain) throws ServletException, IOException { |
| 32 | + |
| 33 | + User fakeUser = User.builder() |
| 34 | + .provider("kakao") |
| 35 | + .providerId("dev") |
| 36 | + .lastLogin(LocalDateTime.now()) |
| 37 | + .build(); |
| 38 | + |
| 39 | + fakeUser.setId(1L); |
| 40 | + fakeUser.updateNickname("user"); |
| 41 | + |
| 42 | + UserPrincipal principal = new UserPrincipal(fakeUser, Map.of()); |
| 43 | + |
| 44 | + Admin fakeAdmin = Admin.builder() |
| 45 | + .id(1L) |
| 46 | + .username("admin") |
| 47 | + .password("admin") |
| 48 | + .lastLogin(LocalDateTime.now()) |
| 49 | + .build(); |
| 50 | + |
| 51 | + AdminPrincipal adminPrincipal = new AdminPrincipal(fakeAdmin); |
| 52 | + |
| 53 | + String authHeader = request.getHeader("Authorization"); |
| 54 | + |
| 55 | + if (authHeader != null && authHeader.equals("Bearer " + DEV_TOKEN)) { |
| 56 | + List<GrantedAuthority> authorities = List.of(new SimpleGrantedAuthority("ROLE_USER")); |
| 57 | + |
| 58 | + Authentication auth = new UsernamePasswordAuthenticationToken(principal, null, |
| 59 | + authorities); |
| 60 | + SecurityContextHolder.getContext().setAuthentication(auth); |
| 61 | + } else if (authHeader != null && authHeader.equals("Bearer " + ADMIN_TOKEN)) { |
| 62 | + List<GrantedAuthority> authorities = List.of(new SimpleGrantedAuthority("ROLE_ADMIN")); |
| 63 | + |
| 64 | + Authentication auth = new UsernamePasswordAuthenticationToken(adminPrincipal, null, |
| 65 | + authorities); |
| 66 | + SecurityContextHolder.getContext().setAuthentication(auth); |
| 67 | + } |
| 68 | + filterChain.doFilter(request, response); |
| 69 | + } |
| 70 | +} |
0 commit comments