Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(oauth-security): 구글 로그인 추가 #9

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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 @@ -13,9 +13,7 @@
@EnableJpaAuditing
@Configuration
@RequiredArgsConstructor
@EnableJpaRepositories(basePackages = {
"com.inspire12.practice.api.module"
})

public class JpaCommonConfig {

//https://www.marcobehler.com/guides/spring-transaction-management-transactional-in-depth#_how_spring_and_jpa_hibernate_transaction_management_works
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;

import static org.springframework.security.config.Customizer.withDefaults;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
Expand All @@ -16,6 +18,7 @@
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.client.oidc.userinfo.OidcUserService;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.server.SecurityWebFilterChain;
Expand Down Expand Up @@ -50,17 +53,38 @@ public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.cors(cors -> cors.configurationSource(request -> {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOrigins(List.of("*")); // 모든 오리진 허용
config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE"));
return config;
}))
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(auth -> auth
.requestMatchers("/", "/api/v1/**", "/h2-console/**", "/favicon.ico").permitAll()
.anyRequest().authenticated()
);
.cors(cors -> cors.configurationSource(request -> {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOrigins(List.of("*")); // 모든 오리진 허용
config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE"));
return config;
}))

.csrf(AbstractHttpConfigurer::disable)
.oauth2Login(oauth2 -> oauth2
.defaultSuccessUrl("/success")
.userInfoEndpoint(userInfo -> userInfo
.oidcUserService(new OidcUserService())
)
)
.authorizeHttpRequests(auth -> auth
.requestMatchers("/", "/api/v1/**", "/h2-console/**", "/favicon.ico").permitAll()
.anyRequest().authenticated()
);
// Form Login 설정
// .formLogin(form -> form
// .loginPage("/login") // 커스텀 로그인 페이지 URL
// .defaultSuccessUrl("/") // 로그인 성공 시 리다이렉트할 URL
// .permitAll()
// )
//
// // OAuth 설정
// .oauth2Login(oauth2Login -> oauth2Login
// .loginPage("/login") // OAuth 로그인도 동일한 로그인 페이지 사용
// .defaultSuccessUrl("/") // OAuth 로그인 성공 시 리다이렉트할 URL
// .permitAll()
// );

return http.build();
// return
// http
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
@RequestMapping("/api/v1/comments")
@RestController
public class CommentApiController {
private final CommentUsecase commentService;
// private final CommentUsecase commentService;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.inspire12.practice.api.module.user.presentation;

import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AuthController {
@GetMapping("/success")
public String success(OAuth2User user) {
// OAuth2 사용자 정보를 가져옵니다.
return "Hello, " + user.getAttribute("name");
}
}
9 changes: 9 additions & 0 deletions src/main/resources/application-oauth-example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
spring:
security:
oauth2:
client:
registration:
google:
client-id: your-google-client-id
client-secret: your-google-client-secret
scope: profile, email
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
server:
port: 8080
port: 8090
servlet:
encoding:
force-response: true
Expand Down
Loading