Skip to content

Commit 131e0ed

Browse files
committed
refactor: security config permit 경로 api 접두사 제거 & 리프레시 토큰 트랜잭션 어노테이션 & 로그인시 메인페이지 리다이렉트
1 parent ae66200 commit 131e0ed

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

src/main/java/com/back/global/jwt/refreshToken/service/RefreshTokenService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class RefreshTokenService {
2525
private long refreshTokenExpiration;
2626

2727
// 기존 리프레시 토큰 삭제하고 생성
28+
@Transactional
2829
public String generateRefreshToken(Long userId, String email) {
2930
// 기존 토큰 삭제
3031
refreshTokenRepository.deleteByUserId(userId);
@@ -53,6 +54,7 @@ public boolean validateToken(String token) {
5354
}
5455

5556
//기존 토큰 지우고 발급(회전)
57+
@Transactional
5658
public String rotateToken(String oldToken) {
5759
Optional<RefreshToken> oldRefreshToken = refreshTokenRepository.findByToken(oldToken);
5860

@@ -67,6 +69,7 @@ public String rotateToken(String oldToken) {
6769
}
6870

6971
//삭제
72+
@Transactional
7073
public void revokeToken(String token) {
7174
refreshTokenRepository.deleteByToken(token);
7275
}

src/main/java/com/back/global/security/CustomOAuth2LoginSuccessHandler.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,8 @@ public class CustomOAuth2LoginSuccessHandler implements AuthenticationSuccessHan
2424
@Override
2525
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
2626
SecurityUser securityUser = (SecurityUser) authentication.getPrincipal();
27-
2827
// Access Token과 Refresh Token 발급
2928
userAuthService.issueTokens(response, securityUser.getId(), securityUser.getEmail(), securityUser.getNickname());
30-
31-
// 프론트엔드로 리다이렉트
32-
String redirectUrl = frontendUrl + "/oauth/success";
33-
34-
response.sendRedirect(redirectUrl);
29+
response.sendRedirect(frontendUrl);
3530
}
3631
}

src/main/java/com/back/global/security/SecurityConfig.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.back.global.security;
22

3+
import org.springframework.beans.factory.annotation.Value;
34
import org.springframework.context.annotation.Bean;
45
import org.springframework.context.annotation.Configuration;
56
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@@ -18,6 +19,12 @@
1819
@EnableWebSecurity
1920
public class SecurityConfig {
2021

22+
@Value("${custom.site.frontUrl}")
23+
private String frontUrl;
24+
25+
@Value("${custom.site.backUrl}")
26+
private String backUrl;
27+
2128
private final CustomOAuth2UserService customOAuth2UserService;
2229
private final CustomOAuth2LoginSuccessHandler oauth2SuccessHandler;
2330
private final CustomOAuth2LoginFailureHandler oauth2FailureHandler;
@@ -46,18 +53,16 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
4653
.authorizeHttpRequests(auth -> auth
4754
.requestMatchers("/").permitAll()
4855
.requestMatchers("/h2-console/**").permitAll()
56+
.requestMatchers("/actuator/**").permitAll()
4957
.requestMatchers("/oauth2/**").permitAll()
5058
.requestMatchers("/login/oauth2/**").permitAll()
5159
.requestMatchers("/swagger-ui/**", "/api-docs/**").permitAll()
52-
.requestMatchers("/api/user/**").permitAll()
53-
.requestMatchers("/api/cocktail/**").permitAll()
54-
.requestMatchers("/api/chatbot/**").permitAll()
55-
.requestMatchers("/api/cocktails/**").permitAll()
56-
60+
.requestMatchers("/user/**").permitAll()
61+
.requestMatchers("/cocktails/**").permitAll()
62+
.requestMatchers("/chatbot/**").permitAll()
5763

5864
// 회원 or 인증된 사용자만 가능
59-
.requestMatchers("/api/admin/**").hasRole("ADMIN")
60-
// .requestMatchers("/api/cocktail/detail~~").authenticated()
65+
.requestMatchers("/admin/**").hasRole("ADMIN")
6166

6267
//그 외에는 인증해야함
6368
.anyRequest().authenticated()
@@ -95,9 +100,8 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
95100
public CorsConfigurationSource corsConfigurationSource() {
96101
CorsConfiguration configuration = new CorsConfiguration();
97102
configuration.setAllowedOrigins(Arrays.asList(
98-
"http://localhost:3000",
99-
"http://localhost:8080"
100-
//나중에 운영환경 추가
103+
frontUrl,
104+
backUrl
101105
));
102106
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"));
103107
configuration.setAllowedHeaders(Arrays.asList("*"));

0 commit comments

Comments
 (0)