Skip to content

Commit

Permalink
Merge pull request #30 from yumzen/main
Browse files Browse the repository at this point in the history
Fix: 401 Error
  • Loading branch information
yumzen authored Jul 20, 2024
2 parents e36002b + 631cc71 commit 985de36
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public class CorsMvcConfig implements WebMvcConfigurer {
public void addCorsMappings(CorsRegistry corsRegistry) {

corsRegistry.addMapping("/**")
.allowedOrigins("http://localhost:3000");
.allowedOrigins("http://localhost:3000", "https://kahluaband.com/", "http://kahluaband.com/");
}
}
6 changes: 3 additions & 3 deletions src/main/java/kahlua/KahluaProject/confg/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public class SecurityConfig {

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.cors(Customizer.withDefaults())
.csrf(AbstractHttpConfigurer::disable);
http.cors(Customizer.withDefaults());
http.csrf((csrf) -> csrf.disable());

http.sessionManagement((session) -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS));// Session 미사용
http.httpBasic(AbstractHttpConfigurer::disable)
Expand All @@ -53,7 +53,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

http.authorizeHttpRequests((authorize) ->
authorize
.requestMatchers("/v1/auth/sign-out/**", "v1/auth/recreate","/v1/user/**", "/error/**")
.requestMatchers("/v1/auth/sign-out/**", "v1/auth/recreate/**","/v1/user/**", "/error/**", "/v1/admin/**")
.authenticated()
.anyRequest()
.permitAll());
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/kahlua/KahluaProject/controller/AdminController.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package kahlua.KahluaProject.controller;

import kahlua.KahluaProject.apipayload.ApiResponse;
import kahlua.KahluaProject.apipayload.code.status.ErrorStatus;
import kahlua.KahluaProject.domain.user.UserType;
import kahlua.KahluaProject.dto.response.ApplyGetResponse;
import kahlua.KahluaProject.dto.response.ApplyListResponse;
import kahlua.KahluaProject.exception.GeneralException;
import kahlua.KahluaProject.security.AuthDetails;
import kahlua.KahluaProject.service.ApplyService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -19,13 +24,16 @@ public class AdminController {
private final ApplyService applyService;

@GetMapping("/apply")
public ApiResponse<ApplyListResponse> getApplyList() {
ApplyListResponse applyListResponse = applyService.getApplyList();
public ApiResponse<ApplyListResponse> getApplyList(@AuthenticationPrincipal AuthDetails authDetails) {
ApplyListResponse applyListResponse = applyService.getApplyList(authDetails.user());
return ApiResponse.onSuccess(applyListResponse);
}

@GetMapping("/apply/{applyId}")
public ApiResponse<ApplyGetResponse> getApplyDetail(@PathVariable Long applyId) {
public ApiResponse<ApplyGetResponse> getApplyDetail(@PathVariable Long applyId, @AuthenticationPrincipal AuthDetails authDetails) {
if(authDetails.getUser().getUserType() != UserType.ADMIN){
throw new GeneralException(ErrorStatus.UNAUTHORIZED);
}
ApplyGetResponse applyGetResponse = applyService.getApply(applyId);
return ApiResponse.onSuccess(applyGetResponse);
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/kahlua/KahluaProject/service/ApplyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import kahlua.KahluaProject.apipayload.code.status.ErrorStatus;
import kahlua.KahluaProject.converter.ApplyConverter;
import kahlua.KahluaProject.domain.apply.Apply;
import kahlua.KahluaProject.domain.user.User;
import kahlua.KahluaProject.domain.user.UserType;
import kahlua.KahluaProject.dto.request.ApplyCreateRequest;
import kahlua.KahluaProject.dto.response.ApplyCreateResponse;
import kahlua.KahluaProject.dto.response.ApplyGetResponse;
Expand Down Expand Up @@ -42,7 +44,11 @@ public ApplyGetResponse getApply(Long applyId) {
return applyGetResponse;
}

public ApplyListResponse getApplyList() {
public ApplyListResponse getApplyList(User user) {

if(user.getUserType() != UserType.ADMIN){
throw new GeneralException(ErrorStatus.UNAUTHORIZED);
}

List<Apply> applies = applyRepository.findAll();
List<ApplyItemResponse> applyItemResponses = new ArrayList<>();
Expand Down

0 comments on commit 985de36

Please sign in to comment.