Team Project

[Spring] Spring Security๋ฅผ ํ™œ์šฉํ•œ ์†Œ์…œ ๋กœ๊ทธ์ธ (1/3)

ITs Min 2024. 3. 29.

๐Ÿ” 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);
    }
}

 

 

์š”์ฒญ์„ ํ•ด์„œ ๋ฐ›์€ ์ •๋ณด๋ฅผ ์ฒ˜๋ฆฌํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ์„œ๋น„์Šค์ด๋‹ค. ์‚ฌ์šฉ๋˜๋Š” ํŒŒ์ผ๋“ค์€ ๋‹ค์Œ ํฌ์ŠคํŒ…์— ๋‹ด์„ ์˜ˆ์ •์ด๋‹ค.


 

๋Œ“๊ธ€

TOP

๋Šฆ์—ˆ๋‹ค๊ณ  ์ƒ๊ฐํ•  ๋• ๋„ˆ๋ฌด ๋Šฆ์€ ๊ฑฐ๋‹ค.