Skip to content

Commit 9d3b2ff

Browse files
authored
Merge pull request #204 from ProjectArTrip/ART-203
[FIX] 랜덤 조회 api 응답 통일, 엑세스토큰 에러 응답 처리
2 parents 804e7b1 + 15af974 commit 9d3b2ff

File tree

5 files changed

+18
-53
lines changed

5 files changed

+18
-53
lines changed

src/main/java/org/atdev/artrip/domain/auth/jwt/JwtAuthenticationFilter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import lombok.extern.slf4j.Slf4j;
99
import org.springframework.security.core.Authentication;
1010
import org.springframework.security.core.context.SecurityContextHolder;
11+
import org.springframework.stereotype.Component;
1112
import org.springframework.util.StringUtils;
1213
import org.springframework.web.filter.OncePerRequestFilter;
1314

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

24+
2325
@Override
2426
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
2527
throws ServletException, IOException {

src/main/java/org/atdev/artrip/domain/auth/jwt/JwtGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public JwtToken generateToken(User user, Role roles) {
3939
.setIssuer(jwtIssuer)
4040
.setSubject(String.valueOf(user.getUserId()))//userid로 할경우 jwt는 사양상 String 타입을 요구함 따라서string변환
4141
.claim("auth", authorities)// 권한 설정
42-
.setExpiration(new Date(now+accessTokenExpirationMillis))
42+
.setExpiration(new Date(now+1000 * 60))
4343
.setIssuedAt(Calendar.getInstance().getTime())
4444
.signWith(key, SignatureAlgorithm.HS256)
4545
.compact();

src/main/java/org/atdev/artrip/domain/auth/jwt/JwtProvider.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,17 @@ public boolean validateToken(String token) {
5959
return true;
6060
} catch (SecurityException | MalformedJwtException e) {
6161
log.warn("Invalid JWT Token", e);
62-
throw new GeneralException(UserError._JWT_INVALID_SIGNATURE);
62+
throw new JwtAuthenticationException(UserError._JWT_INVALID_SIGNATURE);
6363
} catch (ExpiredJwtException e) {
6464
log.warn("Expired JWT Token", e);
65-
throw new GeneralException(UserError._JWT_EXPIRED_ACCESS_TOKEN);
65+
throw new JwtAuthenticationException(UserError._JWT_EXPIRED_ACCESS_TOKEN);
6666
} catch (UnsupportedJwtException e) {
6767
log.warn("Unsupported JWT Token", e);
68-
throw new GeneralException(UserError._JWT_UNSUPPORTED_TOKEN);
68+
throw new JwtAuthenticationException(UserError._JWT_UNSUPPORTED_TOKEN);
6969
} catch (IllegalArgumentException e) {
7070
log.warn("JWT claims string is empty.", e);
71-
throw new GeneralException(UserError._JWT_INVALID_TOKEN);
71+
throw new JwtAuthenticationException(UserError._JWT_INVALID_TOKEN);
7272
}
73-
// return false;
7473
}
7574
public void validateRefreshToken(String refreshToken) {
7675
try {

src/main/java/org/atdev/artrip/domain/home/service/HomeService.java

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,6 @@ public List<HomeListResponse> getRandomPersonalized(Long userId, PersonalizedReq
116116
Set<Long> favoriteIds = getFavoriteIds(userId);
117117
setFavorites(results, favoriteIds);
118118

119-
adjustLocationFields(
120-
results,
121-
request.getIsDomestic(),
122-
request.getRegion(),
123-
request.getCountry()
124-
);
125-
126119
return results;
127120
}
128121

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

142-
adjustLocationFields(
143-
results,
144-
request.getIsDomestic(),
145-
request.getRegion(),
146-
request.getCountry()
147-
);
148-
149135
return results;
150136
}
151137

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

166-
adjustLocationFields(
167-
results,
168-
request.getIsDomestic(),
169-
request.getRegion(),
170-
request.getCountry()
171-
);
172-
173152
return results;
174153
}
175154

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

190-
adjustLocationFields(
191-
results,
192-
request.getIsDomestic(),
193-
request.getRegion(),
194-
request.getCountry()
195-
);
196169

197170
return results;
198171
}
199-
private void adjustLocationFields(List<HomeListResponse> results, boolean isDomestic, String region, String country) {
200-
201-
boolean isWhole = ("전체".equals(region) || region == null) && ("전체".equals(country) || country == null);
202-
203-
if (!isWhole) {
204-
results.forEach(r -> {
205-
r.setRegionName(null);
206-
r.setCountryName(null);
207-
});
208-
return;
209-
}
210172

211-
if (isDomestic) {
212-
results.forEach(r -> r.setCountryName(null));
213-
} else {
214-
results.forEach(r -> r.setRegionName(null));
215-
}
216-
}
217173

218174
}

src/main/java/org/atdev/artrip/global/config/SecurityConfig.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,20 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
6464
)
6565
.successHandler(oAuth2LoginSuccessHandler)
6666
)
67-
.addFilterBefore(new JwtAuthenticationFilter(jwtProvider),
68-
UsernamePasswordAuthenticationFilter.class)
69-
.addFilterBefore(new JwtExceptionFilter(objectMapper), JwtAuthenticationFilter.class);
67+
.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
68+
.addFilterBefore(jwtExceptionFilter(), JwtAuthenticationFilter.class);
7069

7170
return http.build();
7271
}
72+
@Bean
73+
public JwtAuthenticationFilter jwtAuthenticationFilter() {
74+
return new JwtAuthenticationFilter(jwtProvider);
75+
}
76+
77+
@Bean
78+
public JwtExceptionFilter jwtExceptionFilter() {
79+
return new JwtExceptionFilter(objectMapper);
80+
}
7381

7482
@Bean
7583
public CorsConfigurationSource corsConfigurationSource() {

0 commit comments

Comments
 (0)