Skip to content

Commit

Permalink
- Spring Security OAuth2 작업중
Browse files Browse the repository at this point in the history
  • Loading branch information
CJW23 committed Jan 7, 2024
1 parent bf3b2d8 commit 6905fc7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
2 changes: 1 addition & 1 deletion chatting-api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
server:
port: 8080
port: 8090
spring:
kafka:
group-id: group-simple-chatting
Expand Down
2 changes: 1 addition & 1 deletion chatting-stream-server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-websocket'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
testImplementation 'org.springframework.kafka:spring-kafka-test'
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.cjw.chatting.config.security;

import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest;
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Service;

@Service
public class ChattingOAuth2UserService extends DefaultOAuth2UserService {
@Override
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
OAuth2User oAuth2User = super.loadUser(userRequest);
//custom한 로직 작성

return oAuth2User;
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@
package com.cjw.chatting.config.security;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.configurers.AbstractHttpConfigurer;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

@RequiredArgsConstructor
@EnableWebSecurity
@Configuration
public class SecurityConfig {
private final ChattingOAuth2UserService chattingOAuth2UserService;

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(a ->
a.anyRequest().permitAll())
.csrf(AbstractHttpConfigurer::disable)
.formLogin(AbstractHttpConfigurer::disable)
.httpBasic(AbstractHttpConfigurer::disable);
.httpBasic(AbstractHttpConfigurer::disable)
.oauth2Login(config -> {
config
.loginPage("/login")
.userInfoEndpoint(userInfoEndpointConfig ->
userInfoEndpointConfig.userService(chattingOAuth2UserService))
.successHandler(successHandler())
; //커스텀 userService 등록

});

return http.build();
}

@Bean
public AuthenticationManager authenticationManager(HttpSecurity httpSecurity) throws Exception {
AuthenticationManagerBuilder authenticationManagerBuilder = httpSecurity.getSharedObject(AuthenticationManagerBuilder.class);
authenticationManagerBuilder.authenticationProvider(null);
return authenticationManagerBuilder.build();
public AuthenticationSuccessHandler successHandler() {
return ((request, response, authentication) -> {
//response.sendRedirect("https://naver.com");
});
}
}
14 changes: 12 additions & 2 deletions chatting-stream-server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
server:
port: 8090
port: 8080
spring:
kafka:
group-id: group-simple-chatting
group-id: group-simple-chatting
security:
oauth2:
client:
registration:
google:
client-id: 1001974487126-fcm3n94a02tuhffrv59fssrc6e5hsig0.apps.googleusercontent.com
client-secret: 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
scope:
- profile
- email
6 changes: 3 additions & 3 deletions core/src/main/resources/application-core.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
spring:
datasource:
hikari:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/jpa?serverTimezone=UTC&characterEncoding=UTF-8
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:8889/chatting?serverTimezone=UTC&characterEncoding=UTF-8
username: root
password: awdsd123
password: root
jpa:
hibernate:
naming:
Expand Down

0 comments on commit 6905fc7

Please sign in to comment.