Skip to content

Commit

Permalink
Merge pull request #49 from Sketcher-s/refactor/48
Browse files Browse the repository at this point in the history
  • Loading branch information
gyehwan24 authored Sep 9, 2024
2 parents 31c61ba + 4910a51 commit 78c626a
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 16 deletions.
13 changes: 6 additions & 7 deletions src/main/java/shop/catchmind/auth/application/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import shop.catchmind.auth.dto.request.SignUpRequest;
import shop.catchmind.auth.dto.response.IsExistedEmailResponse;
import shop.catchmind.auth.provider.JwtProvider;
import shop.catchmind.auth.provider.RedisProvider;
import shop.catchmind.member.domain.Member;
import shop.catchmind.member.repository.MemberRepository;

import java.util.concurrent.TimeUnit;

import static shop.catchmind.auth.constant.JwtConstant.*;

@Service
Expand All @@ -22,7 +24,7 @@ public class AuthService {

private final MemberRepository memberRepository;
private final PasswordEncoder passwordEncoder;
private final RedisTemplate redisTemplate;
private final RedisProvider redisProvider;
private final JwtProvider jwtProvider;

@Transactional
Expand All @@ -38,11 +40,8 @@ public IsExistedEmailResponse isExistedEmail(final String email) {
}

public void logout(final String bearerToken) {
String accessToken = bearerToken.substring(7);
ValueOperations<String, String> stringValueOperations = redisTemplate.opsForValue();

long remainingExpirationTime = jwtProvider.getRemainingExpirationTime(accessToken);

stringValueOperations.set(REDIS_LOGOUT_KEY, accessToken, remainingExpirationTime, TimeUnit.SECONDS);
String accessToken = bearerToken.substring(BEARER_CLAIM.length());
Long remainingExpirationTime = jwtProvider.getRemainingExpirationTime(accessToken);
redisProvider.registerBlackList(bearerToken.substring(BEARER_CLAIM.length()), remainingExpirationTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import shop.catchmind.auth.Role;
import shop.catchmind.auth.dto.AuthenticationDto;
import shop.catchmind.auth.provider.JwtProvider;
import shop.catchmind.auth.provider.RedisProvider;
import shop.catchmind.member.domain.Member;
import shop.catchmind.member.repository.MemberRepository;

Expand All @@ -26,6 +27,7 @@
public class JwtAuthProcessingFilter extends OncePerRequestFilter {

private final JwtProvider jwtProvider;
private final RedisProvider redisProvider;
private final MemberRepository memberRepository;

private final GrantedAuthoritiesMapper authoritiesMapper = new NullAuthoritiesMapper();
Expand All @@ -38,6 +40,7 @@ protected void doFilterInternal(final HttpServletRequest request, final HttpServ
private void checkAccessTokenAndAuthentication(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain) throws ServletException, IOException {
jwtProvider.extractAccessToken(request)
.filter(jwtProvider::isTokenValid)
.filter(redisProvider::isTokenBlacklisted)
.flatMap(accessToken -> jwtProvider.extractId(accessToken)
.flatMap(memberRepository::findById)).ifPresent(this::saveAuthentication);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public boolean isTokenValid(final String token) {
}
}

public long getRemainingExpirationTime(String accessToken) {
public Long getRemainingExpirationTime(final String accessToken) {
Algorithm algorithm = Algorithm.HMAC512(secretKey);

JWTVerifier verifier = JWT.require(algorithm)
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/shop/catchmind/auth/provider/RedisProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package shop.catchmind.auth.provider;

import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

import static shop.catchmind.auth.constant.JwtConstant.REDIS_LOGOUT_KEY;

@Component
@RequiredArgsConstructor
public class RedisProvider {

private final RedisTemplate<String, String> redisTemplate;

public void registerBlackList(final String accessToken, final Long remainingExpirationTime) {
redisTemplate.opsForValue().set(REDIS_LOGOUT_KEY, accessToken, remainingExpirationTime, TimeUnit.SECONDS);
}

public boolean isTokenBlacklisted(final String accessToken) {
return Boolean.TRUE.equals(redisTemplate.hasKey(REDIS_LOGOUT_KEY + accessToken));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import shop.catchmind.auth.handler.LoginFailureHandler;
import shop.catchmind.auth.handler.LoginSuccessHandler;
import shop.catchmind.auth.provider.JwtProvider;
import shop.catchmind.auth.provider.RedisProvider;
import shop.catchmind.member.repository.MemberRepository;

import java.util.Arrays;
Expand All @@ -41,7 +42,8 @@
public class SecurityConfig {

private final LoginService loginService;
private final JwtProvider jwtDriver;
private final RedisProvider redisProvider;
private final JwtProvider jwtProvider;
private final MemberRepository memberRepository;
private final ObjectMapper objectMapper;

Expand Down Expand Up @@ -94,7 +96,7 @@ public AuthenticationManager authenticationManager() {

@Bean
public LoginSuccessHandler loginSuccessHandler() {
return new LoginSuccessHandler(jwtDriver, memberRepository);
return new LoginSuccessHandler(jwtProvider, memberRepository);
}

@Bean
Expand All @@ -113,7 +115,7 @@ public CustomJsonAuthenticationFilter customJsonAuthenticationFilter() {

@Bean
public JwtAuthProcessingFilter jwtAuthProcessingFilter() {
return new JwtAuthProcessingFilter(jwtDriver, memberRepository);
return new JwtAuthProcessingFilter(jwtProvider, redisProvider, memberRepository);
}

@Bean
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/shop/catchmind/global/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package shop.catchmind.global.config;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import shop.catchmind.global.converter.OctetStreamReadMsgConverter;

import java.util.List;

@Configuration
@RequiredArgsConstructor
public class WebConfig implements WebMvcConfigurer {

private final OctetStreamReadMsgConverter octetStreamReadMsgConverter;

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(octetStreamReadMsgConverter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package shop.catchmind.global.converter;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
import org.springframework.stereotype.Component;

import java.lang.reflect.Type;

@Component
public class OctetStreamReadMsgConverter extends AbstractJackson2HttpMessageConverter {

@Autowired
public OctetStreamReadMsgConverter(ObjectMapper objectMapper) {
super(objectMapper, MediaType.APPLICATION_OCTET_STREAM);
}

@Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
return false;
}

@Override
public boolean canWrite(Type type, Class<?> clazz, MediaType mediaType) {
return false;
}

@Override
protected boolean canWrite(MediaType mediaType) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private NaturalLanguageDto serveToFlask(final MultipartFile file, final Recogniz

try {
body.add(FILE_REQUEST, new MultipartInputStreamFileResource(file.getInputStream(), file.getOriginalFilename()));
body.add(MODEL_TYPE_REQUEST, request.pictureType());
body.add(MODEL_TYPE_REQUEST, request.pictureType().getType());
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/shop/catchmind/picture/domain/PictureType.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package shop.catchmind.picture.domain;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum PictureType {
HOUSE,
TREE,
MALE,
FEMALE
HOUSE("house"),
TREE("tree"),
MALE("male"),
FEMALE("female");

private final String type;
}

0 comments on commit 78c626a

Please sign in to comment.