๐ Spring Security๋ฅผ ํ์ฉํ ์์ ๋ก๊ทธ์ธ
์ค๊ฐ ํ๋ก์ ํธ์์ ๊ตฌํํ๋ ์์ ๋ก๊ทธ์ธ์ SpringSecurity์ OAuth2๋ฅผ ํ์ฉํ์ฌ ๋ค์ ๋ง๋ค์๋ค.
์ค๊ฐ ํ๋ก์ ํธ์์๋ ๋ค์ด๋ฒ์ ์นด์นด์ค๋ง ๊ตฌํํ์์ง๋ง ์ด ๋ฐฉ๋ฒ๋ค์ด javascript๋ฅผ ํ์ฉํ๊ธฐ ๋๋ฌธ์ ์ด๋ฒ์๋
Spring์ ๋ง๊ฒ ํ์ฉํ์ฌ ๋ค์ด๋ฒ, ์นด์นด์ค ๋ฟ๋ง ์๋๋ผ ๊ตฌ๊ธ ๋ก๊ทธ์ธ๋ ๊ตฌํํ ์์ ์ด๋ค.
๐ signIn.jsp
<a href="/oauth2/authorization/google" class="btn btn-social btn-block buffer"><i class="xi-google"></i> Sign In
with Google</a>
<a href="/oauth2/authorization/kakao" class="btn btn-social btn-block kakao"><i class="xi-kakaotalk"></i> Sign In
with Kakao</a>
<a href="/oauth2/authorization/naver" class="btn btn-social btn-block naver"><i class="xi-naver"></i> Sign In with
Naver</a>
View์์๋ oauth2๋ฅผ ํฌํจํ ๊ฒฝ๋ก๋ฅผ ์์ฒญํ๋ค.
๐ SecurityConfig.java
package infinitystone.chalKag.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;
import infinitystone.chalKag.biz.customOAuth2.CustomOAuth2UserService;
@Configuration
@EnableWebSecurity
public class SecurityConfig {
private final CustomOAuth2UserService customOAuth2UserService;
public SecurityConfig(CustomOAuth2UserService customOAuth2UserService) {
this.customOAuth2UserService = customOAuth2UserService;
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf((csrf) -> csrf.disable());
http
.formLogin((auth) -> auth.disable());
http
.httpBasic((basic) -> basic.disable());
http
.oauth2Login((oauth2) -> oauth2
.userInfoEndpoint((userInfoEndpointConfig) -> userInfoEndpointConfig
.userService(customOAuth2UserService))
.defaultSuccessUrl("/oauth2SignIn"));
http
.authorizeHttpRequests((auth) -> auth
.anyRequest().permitAll());
return http.build();
}
}
Oauth2๋ฅผ ํฌํจํ ๊ฒฝ๋ก๋ฅผ SecurityConfig๊ฐ ๋์์ฑ์ customOAuth2UserService๋ก ์ด๋์ํจ๋ค.
๐ CustomOAuth2UserService.java
package infinitystone.chalKag.biz.customOAuth2;
import java.util.UUID;
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 CustomOAuth2UserService extends DefaultOAuth2UserService {
//DefaultOAuth2UserService OAuth2UserService์ ๊ตฌํ์ฒด
private final UserRepository userRepository;
public CustomOAuth2UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2AuthenticationException {
OAuth2User oAuth2User = super.loadUser(userRequest);
System.out.println(oAuth2User.getAttributes());
String registrationId = userRequest.getClientRegistration().getRegistrationId();
OAuth2Response oAuth2Response = null;
if (registrationId.equals("naver")) {
oAuth2Response = new NaverResponse(oAuth2User.getAttributes());
}
else if (registrationId.equals("google")) {
oAuth2Response = new GoogleResponse(oAuth2User.getAttributes());
}
else if(registrationId.equals("kakao")) {
oAuth2Response = new KakaoResponse(oAuth2User.getAttributes());
}
else {
return null;
}
String memberEmail= oAuth2Response.getEmail();
UserEntity existData = userRepository.findByEmail(memberEmail);
String role = "ROLE_USER";
if (existData == null) {
UserEntity userEntity = new UserEntity();
UUID uuid = UUID.randomUUID();
String temporaryPassword = uuid.toString().substring(0, 8);
System.out.println("temporaryPassword ํ์ธ ["+temporaryPassword+"]");
userEntity.setUsername(oAuth2Response.getName());
userEntity.setBirth(oAuth2Response.getBirthYear()+oAuth2Response.getBirthday());
userEntity.setEmail(oAuth2Response.getEmail());
userEntity.setNickname(oAuth2Response.getNickname());
userEntity.setPh(oAuth2Response.getPh());
userEntity.setPw(temporaryPassword);
userEntity.setGender(oAuth2Response.getGender());
userEntity.setRole(role);
userRepository.save(userEntity);
}
return new CustomOAuth2User(oAuth2Response, role);
}
}
์์ฒญ์ ํด์ ๋ฐ์ ์ ๋ณด๋ฅผ ์ฒ๋ฆฌํ์ฌ ๊ฐ์ฒด๋ฅผ ๋ฆฌํดํ๋ ์๋น์ค์ด๋ค. ์ฌ์ฉ๋๋ ํ์ผ๋ค์ ๋ค์ ํฌ์คํ ์ ๋ด์ ์์ ์ด๋ค.
'Team Project' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Spring] Spring Security๋ฅผ ํ์ฉํ ์์ ๋ก๊ทธ์ธ (3/3) (0) | 2024.04.09 |
|---|---|
| [Spring] Spring Security๋ฅผ ํ์ฉํ ์์ ๋ก๊ทธ์ธ (2/3) (0) | 2024.04.01 |
| [Spring] SMS ์ธ์ฆ ๊ธฐ๋ฅ Controller ์ด๊ด์์ (0) | 2024.03.11 |
| [JSP] ์ปค์คํ ํ๊ทธ๋ฅผ ์ด์ฉํ์ฌ nav ๊ตฌ์ฑ (0) | 2024.01.29 |
| [Java] ์ถ๊ตฌ ์ ์ ์ด์ ์์ฅ (0) | 2023.12.14 |
๋๊ธ