Skip to content
Merged
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 @@ -8,6 +8,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;

Expand All @@ -20,6 +21,7 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
private final JwtProvider jwtProvider;
private static final String GRANT_TYPE = "Bearer ";


@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public JwtToken generateToken(User user, Role roles) {
.setIssuer(jwtIssuer)
.setSubject(String.valueOf(user.getUserId()))//userid로 할경우 jwt는 사양상 String 타입을 요구함 따라서string변환
.claim("auth", authorities)// 권한 설정
.setExpiration(new Date(now+accessTokenExpirationMillis))
.setExpiration(new Date(now+1000 * 60))
.setIssuedAt(Calendar.getInstance().getTime())
.signWith(key, SignatureAlgorithm.HS256)
.compact();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,17 @@ public boolean validateToken(String token) {
return true;
} catch (SecurityException | MalformedJwtException e) {
log.warn("Invalid JWT Token", e);
throw new GeneralException(UserError._JWT_INVALID_SIGNATURE);
throw new JwtAuthenticationException(UserError._JWT_INVALID_SIGNATURE);
} catch (ExpiredJwtException e) {
log.warn("Expired JWT Token", e);
throw new GeneralException(UserError._JWT_EXPIRED_ACCESS_TOKEN);
throw new JwtAuthenticationException(UserError._JWT_EXPIRED_ACCESS_TOKEN);
} catch (UnsupportedJwtException e) {
log.warn("Unsupported JWT Token", e);
throw new GeneralException(UserError._JWT_UNSUPPORTED_TOKEN);
throw new JwtAuthenticationException(UserError._JWT_UNSUPPORTED_TOKEN);
} catch (IllegalArgumentException e) {
log.warn("JWT claims string is empty.", e);
throw new GeneralException(UserError._JWT_INVALID_TOKEN);
throw new JwtAuthenticationException(UserError._JWT_INVALID_TOKEN);
}
// return false;
}
public void validateRefreshToken(String refreshToken) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,6 @@ public List<HomeListResponse> getRandomPersonalized(Long userId, PersonalizedReq
Set<Long> favoriteIds = getFavoriteIds(userId);
setFavorites(results, favoriteIds);

adjustLocationFields(
results,
request.getIsDomestic(),
request.getRegion(),
request.getCountry()
);

return results;
}

Expand All @@ -139,13 +132,6 @@ public List<HomeListResponse> getRandomSchedule(ScheduleRandomRequest request, L
Set<Long> favoriteIds = getFavoriteIds(userId);
setFavorites(results, favoriteIds);

adjustLocationFields(
results,
request.getIsDomestic(),
request.getRegion(),
request.getCountry()
);

return results;
}

Expand All @@ -163,13 +149,6 @@ public List<HomeListResponse> getRandomGenre(GenreRandomRequest request, Long us
Set<Long> favoriteIds = getFavoriteIds(userId);
setFavorites(results, favoriteIds);

adjustLocationFields(
results,
request.getIsDomestic(),
request.getRegion(),
request.getCountry()
);

return results;
}

Expand All @@ -187,32 +166,9 @@ public List<HomeListResponse> getRandomToday(TodayRandomRequest request, Long us
Set<Long> favoriteIds = getFavoriteIds(userId);
setFavorites(results, favoriteIds);

adjustLocationFields(
results,
request.getIsDomestic(),
request.getRegion(),
request.getCountry()
);

return results;
}
private void adjustLocationFields(List<HomeListResponse> results, boolean isDomestic, String region, String country) {

boolean isWhole = ("전체".equals(region) || region == null) && ("전체".equals(country) || country == null);

if (!isWhole) {
results.forEach(r -> {
r.setRegionName(null);
r.setCountryName(null);
});
return;
}

if (isDomestic) {
results.forEach(r -> r.setCountryName(null));
} else {
results.forEach(r -> r.setRegionName(null));
}
}

}
14 changes: 11 additions & 3 deletions src/main/java/org/atdev/artrip/global/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,20 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
)
.successHandler(oAuth2LoginSuccessHandler)
)
.addFilterBefore(new JwtAuthenticationFilter(jwtProvider),
UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(new JwtExceptionFilter(objectMapper), JwtAuthenticationFilter.class);
.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(jwtExceptionFilter(), JwtAuthenticationFilter.class);

return http.build();
}
@Bean
public JwtAuthenticationFilter jwtAuthenticationFilter() {
return new JwtAuthenticationFilter(jwtProvider);
}

@Bean
public JwtExceptionFilter jwtExceptionFilter() {
return new JwtExceptionFilter(objectMapper);
}

@Bean
public CorsConfigurationSource corsConfigurationSource() {
Expand Down
Loading