Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/#67 admin jwt #68

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import JGS.CasperEvent.domain.event.dto.RequestDto.AdminRequestDto;
import JGS.CasperEvent.domain.event.service.AdminService.AdminService;
import JGS.CasperEvent.global.response.ResponseDto;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
Expand All @@ -19,7 +20,7 @@ public AdminController(AdminService adminService) {
}

@PostMapping("/join")
public ResponseEntity<String> postAdmin(@RequestBody @Valid AdminRequestDto adminRequestDto){
public ResponseEntity<ResponseDto> postAdmin(@RequestBody @Valid AdminRequestDto adminRequestDto){
return ResponseEntity
.status(HttpStatus.CREATED)
.body(adminService.postAdmin(adminRequestDto));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface AdminRepository extends JpaRepository<Admin, String> {
Optional<Admin> findByIdAndPassword(String adminId, String password);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import JGS.CasperEvent.global.enums.Role;
import JGS.CasperEvent.global.error.exception.CustomException;
import JGS.CasperEvent.global.jwt.dto.AdminLoginDto;
import JGS.CasperEvent.global.response.ResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -18,12 +19,10 @@ public class AdminService {
private final AdminRepository adminRepository;

public Admin verifyAdmin(AdminLoginDto adminLoginDto) {
return adminRepository.findById(adminLoginDto.getId()).orElseThrow(NoSuchElementException::new);
return adminRepository.findByIdAndPassword(adminLoginDto.getAdminId(), adminLoginDto.getPassword()).orElseThrow(NoSuchElementException::new);
}

public String postAdmin(AdminRequestDto adminRequestDto) {


public ResponseDto postAdmin(AdminRequestDto adminRequestDto) {
String adminId = adminRequestDto.getAdminId();
//Todo: 비밀번호 암호화 필요
String password = adminRequestDto.getPassword();
Expand All @@ -33,6 +32,6 @@ public String postAdmin(AdminRequestDto adminRequestDto) {
if (admin != null) throw new CustomException("이미 등록된 ID입니다.", CustomErrorCode.CONFLICT);
adminRepository.save(new Admin(adminId, password, Role.ADMIN));

return "admin Created";
return ResponseDto.of("관리자 생성 성공");
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package JGS.CasperEvent.global.config;

import JGS.CasperEvent.domain.event.service.AdminService.AdminService;
import JGS.CasperEvent.global.jwt.filter.JwtAuthorizationFilter;
import JGS.CasperEvent.global.jwt.filter.JwtUserFilter;
import JGS.CasperEvent.global.jwt.filter.VerifyAdminFilter;
import JGS.CasperEvent.global.jwt.filter.VerifyUserFilter;
import JGS.CasperEvent.global.jwt.filter.*;
import JGS.CasperEvent.global.jwt.service.UserService;
import JGS.CasperEvent.global.jwt.util.JwtProvider;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -87,7 +84,7 @@ public FilterRegistrationBean verifyAdminFilter(ObjectMapper mapper, AdminServic
public FilterRegistrationBean jwtAdminFilter(JwtProvider provider, ObjectMapper mapper) {
FilterRegistrationBean<Filter> filterRegistrationBean = new
FilterRegistrationBean<>();
filterRegistrationBean.setFilter(new JwtUserFilter(provider, mapper));
filterRegistrationBean.setFilter(new JwtAdminFilter(provider, mapper));
filterRegistrationBean.setOrder(2);
filterRegistrationBean.addUrlPatterns("/admin/auth");
return filterRegistrationBean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

@Getter
public class AdminLoginDto {
private String id;
private String adminId;
private String password;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.http.HttpStatus;

import java.io.IOException;
import java.util.NoSuchElementException;

@Slf4j
@RequiredArgsConstructor
Expand All @@ -28,12 +29,14 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
try {
AdminLoginDto adminLoginDto = objectMapper.readValue(request.getReader(), AdminLoginDto.class);
Admin admin = adminService.verifyAdmin(adminLoginDto);

request.setAttribute(AUTHENTICATE_ADMIN, admin);

chain.doFilter(request, response);
} catch(NoSuchElementException e){
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
httpServletResponse.sendError(HttpStatus.BAD_REQUEST.value(), "아이디 혹은 비밀번호가 잘못되었습니다.");
} catch (Exception e) {
log.error("Fail User Verify");
log.error("Fail Admin Verify");
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
httpServletResponse.sendError(HttpStatus.BAD_REQUEST.value(), e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class AdminControllerTest {
private MockMvc mockMvc;

@Nested
@DisplayName("캐스퍼 봇 생성 테스트")
@DisplayName("어드민 테스트")
class AdminTest{
@Test
@DisplayName("어드민 생성 성공 테스트")
Expand All @@ -48,7 +48,7 @@ void createAdminSuccessTest() throws Exception {
//then
perform
.andExpect(status().isCreated())
.andExpect(jsonPath("$.message").value("관리자 생성 완료"))
.andExpect(jsonPath("$.message").value("관리자 생성 성공"))
.andDo(print());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void createCasperBotSuccessTest() throws Exception {
""";

//when
ResultActions perform = mockMvc.perform(post("/event/lottery")
ResultActions perform = mockMvc.perform(post("/event/lottery/casperBot")
.contentType(MediaType.APPLICATION_JSON)
.content(casperBotRequest)
.header("Authorization", accessToken));
Expand Down Expand Up @@ -105,7 +105,7 @@ void createCasperBotFailureTest_RequiredFieldNotExist() throws Exception {


//when
ResultActions perform = mockMvc.perform(post("/event/lottery")
ResultActions perform = mockMvc.perform(post("/event/lottery/casperBot")
.contentType(MediaType.APPLICATION_JSON)
.content(casperBotRequest)
.header("Authorization", accessToken));
Expand Down Expand Up @@ -134,7 +134,7 @@ void createCasperBotSuccessTest_WrongValue() throws Exception {
}""";

//when
ResultActions perform = mockMvc.perform(post("/event/lottery")
ResultActions perform = mockMvc.perform(post("/event/lottery/casperBot")
.contentType(MediaType.APPLICATION_JSON)
.content(casperBotRequest)
.header("Authorization", accessToken));
Expand Down Expand Up @@ -162,7 +162,7 @@ void createCasperBotSuccessTest_CookieNotPresent() throws Exception {
""";

//when
ResultActions perform = mockMvc.perform(post("/event/lottery")
ResultActions perform = mockMvc.perform(post("/event/lottery/casperBot")
.contentType(MediaType.APPLICATION_JSON)
.content(casperBotRequest));

Expand Down Expand Up @@ -193,7 +193,7 @@ void userHasAppliedCasperBotSuccessTest_PresentUser() throws Exception {
}
""";
//when
mockMvc.perform(post("/event/lottery")
mockMvc.perform(post("/event/lottery/casperBot")
.contentType(MediaType.APPLICATION_JSON)
.content(casperBotRequest)
.header("Authorization", accessToken));
Expand Down
Loading