본문 바로가기

Spring Boot

[Springboot] Facebook 로그인 연동 구현

반응형

1. 다음 종속성을 pom.xml 파일에 추가합니다

<dependency>
    <groupId>org.springframework.social</groupId>
    <artifactId>spring-social-facebook</artifactId>
    <version>3.0.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-social-facebook</artifactId>
    <version>2.2.5.RELEASE</version>
</dependency>

 

2. application.properties 파일에 Facebook App ID 및 App Secret을 추가합니다

spring.social.facebook.app-id=YOUR_APP_ID
spring.social.facebook.app-secret=YOUR_APP_SECRET

 

3. Facebook Connect 컨트롤러 구현

import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.web.ConnectController;
import org.springframework.stereotype.Controller;

@Controller
public class FacebookConnectController extends ConnectController {

    public FacebookConnectController(ConnectionFactoryLocator connectionFactoryLocator,
                                     ConnectionRepository connectionRepository) {
        super(connectionFactoryLocator, connectionRepository);
    }

}

 

4. Facebook 로그인 만들기

<a href="<c:url value='/connect/facebook' />">Login with Facebook</a>

 

5. 콜백 엔드포인트 구현

import org.springframework.social.connect.Connection;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class FacebookCallbackController {

    private final ProviderSignInUtils providerSignInUtils;

    public FacebookCallbackController(ProviderSignInUtils providerSignInUtils) {
        this.providerSignInUtils = providerSignInUtils;
    }

    @RequestMapping(value = "/facebook")
    public String facebook(HttpServletRequest request) {
        Connection<?> connection = providerSignInUtils.getConnectionFromSession(request);
        // store the user's data in your database
        // redirect the user to the appropriate page
        return "redirect:/";
    }

}
반응형