Skip to content

Commit

Permalink
feat: 로그인 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
70825 committed Jul 24, 2023
1 parent c9fef9a commit d2433b8
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
14 changes: 14 additions & 0 deletions backend/src/main/java/com/funeat/auth/dto/LoginRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.funeat.auth.dto;

public class LoginRequest {

private final Long id;

public LoginRequest(final Long id) {
this.id = id;
}

public Long getId() {
return id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.funeat.auth.util;

import com.funeat.auth.dto.LoginRequest;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

@Component
public class AuthArgumentResolver implements HandlerMethodArgumentResolver {

private final JwtTokenProvider jwtTokenProvider;

public AuthArgumentResolver(final JwtTokenProvider jwtTokenProvider) {
this.jwtTokenProvider = jwtTokenProvider;
}

@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(AuthenticationPrincipal.class);
}

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {
final HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
final String token = AuthorizationExtractor.extract(Objects.requireNonNull(request));
final String id = jwtTokenProvider.getPayload(token);

return new LoginRequest(Long.valueOf(id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.funeat.auth.util;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface AuthenticationPrincipal {
}
14 changes: 14 additions & 0 deletions backend/src/main/java/com/funeat/common/WebConfig.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
package com.funeat.common;

import com.funeat.auth.util.AuthArgumentResolver;
import java.util.List;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

private final AuthArgumentResolver authArgumentResolver;

public WebConfig(AuthArgumentResolver authArgumentResolver) {
this.authArgumentResolver = authArgumentResolver;
}

@Override
public void addFormatters(final FormatterRegistry registry) {
registry.addConverter(new StringToCategoryTypeConverter());
}

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(authArgumentResolver);
}
}

0 comments on commit d2433b8

Please sign in to comment.