Skip to content

Commit

Permalink
✨ Feat: 로그인 후 Security 관련 세팅 추가"
Browse files Browse the repository at this point in the history
  • Loading branch information
HyoBN committed May 2, 2024
1 parent 4f9313d commit f5e68b1
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ public Optional<User> findByPhoneNumber(String phone){
public User findById(Long id){
return userRepository.findById(id).orElseThrow(()->new UserException(GlobalErrorCode.MEMBER_NOT_FOUND));
}

public Optional<User> optionalUserFindById(Long id){
return userRepository.findById(id);
}
}
27 changes: 27 additions & 0 deletions src/main/java/treehouse/server/global/config/GlobalWebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package treehouse.server.global.config;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import treehouse.server.global.security.handler.annotation.resolver.AuthMemberArgumentResolver;

import java.util.List;

@Configuration
@RequiredArgsConstructor
public class GlobalWebConfig implements WebMvcConfigurer {

private final AuthMemberArgumentResolver authMemberArgumentResolver;

/**
* 컨트롤러 메서드의 특정 파라미터를 지원하는 커스텀한 ArgumentResolver를 추가
* @param resolverList
*/
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolverList) {
resolverList.add(authMemberArgumentResolver);
}
}
18 changes: 17 additions & 1 deletion src/main/java/treehouse/server/global/config/SwaggerConfig.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package treehouse.server.global.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -15,8 +18,21 @@ public OpenAPI SpringCodeBaseAPI() {
.description("Treehouse API 명세서")
.version("1.0.0");

String jwtSchemeName = "JWT TOKEN";
// API 요청헤더에 인증정보 포함
SecurityRequirement securityRequirement = new SecurityRequirement().addList(jwtSchemeName);
// SecuritySchemes 등록
Components components = new Components()
.addSecuritySchemes(jwtSchemeName, new SecurityScheme()
.name(jwtSchemeName)
.type(SecurityScheme.Type.HTTP)
.scheme("bearer")
.bearerFormat("JWT"));

return new OpenAPI()
.addServersItem(new Server().url("/"))
.info(info);
.info(info)
.addSecurityItem(securityRequirement)
.components(components);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
package treehouse.server.global.security.handler.annotation;

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.PARAMETER) // 어노테이션을 파라미터에만 적용
public @interface AuthMember {
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public boolean supportsParameter(MethodParameter parameter) {
* resolveArgument
* 실제로 파라미터의 값을 해석해주는 메서드
* 파라미터에 전달할 객체를 반환
* - SecurityContextHolder에서 인증 객체를 가져와서 Member 객체로 변환하여 반환
* - SecurityContextHolder에서 인증 객체를 가져와서 User 객체로 변환하여 반환
*/
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
Expand Down

0 comments on commit f5e68b1

Please sign in to comment.