강의

멘토링

로드맵

Cộng đồng Hỏi & Đáp của Inflearn

Hình ảnh hồ sơ của mignon25
mignon25

câu hỏi đã được viết

Bảo mật mùa xuân OAuth2

Triển khai tích hợp Đăng nhập xã hội OAuth 2.0 (3)

[OAuth 2.0 Social Login 연동 구현 3] 에서 계속 index 문자열만 보이고 index 페이지가 보이지 않습니다.

Viết

·

599

·

Đã chỉnh sửa

0




server:
  port: 8081
  
spring:
  security:
    oauth2:
      client:

        registration:
          keycloak:
            authorization-grant-type: authorization_code
            client-id: oauth2-client-app
            client-name: oauth2-client-app
            client-secret: dXf021lMWuZ9kZafqxZn230MvVEdROIo
            redirect-uri: http://localhost:8081/login/oauth2/code/keycloak
            scope: profile,email

          google:
            client-id: 발급받은ID
            client-secret: 발급받은비밀번호
            scope: profile,email

          naver:
            client-id: 발급받은ID
            client-secret: 발급받은비밀번호
            authorization-grant-type: authorization_code
            client-name: naver-client-app
            redirect-uri: http://localhost:8081/login/oauth2/code/naver
            scope: profile,email
      

        provider:
          keycloak:
            authorization-uri: http://localhost:8080/realms/oauth2/protocol/openid-connect/auth
            issuer-uri: http://localhost:8080/realms/oauth2
            jwk-set-uri: http://localhost:8080/realms/oauth2/protocol/openid-connect/certs
            token-uri: http://localhost:8080/realms/oauth2/protocol/openid-connect/token
            user-info-uri: http://localhost:8080/realms/oauth2/protocol/openid-connect/userinfo
            user-name-attribute: preferred_username
          naver:
            authorization-uri: https://nid.naver.com/oauth2.0/authorize
            token-uri: https://nid.naver.com/oauth2.0/token
            user-info-uri: https://openapi.naver.com/v1/nid/me
            user-name-attribute: response

  mvc:
    static-path-pattern: /static/**

 

 

package springsecurityoauth2.demo.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class IndexController {

    @GetMapping("/")
    public String index(Model model, Authentication authentication, @AuthenticationPrincipal OAuth2User oAuth2User) {

        OAuth2AuthenticationToken oAuth2AuthenticationToken = (OAuth2AuthenticationToken) authentication;
        if (oAuth2AuthenticationToken != null) {
            Map<String, Object> attributes = oAuth2User.getAttributes();
            String name = (String) attributes.get("name");

            // 네이버는 response 계층이 하나 더 있으므로 별도 처리 필요
            if (oAuth2AuthenticationToken.getAuthorizedClientRegistrationId().equals("naver")) {
                Map<String, Object> response = (Map<String, Object>) attributes.get("response");
                name = (String) response.get("name");
            }

            model.addAttribute("user", name);
        }

        return "index";
    }
}

 

 

안녕하세요.

resource 파일들은 깃헙의 소셜로그인 브랜치에서 그대로 가져왔고, IndexController 와 application.yml 파일은 위와 같습니다.

 

브라우저에서 localhost:8081 로 접속하면

이렇게만 나옵니다 ㅠㅠ

어디가 잘못됐을까요?

 

 

javaspringspring-bootoauth

Quiz

61% người trả lời sai. Hãy thử ngay!

Spring Security Client 모듈이 OAuth 2.0 인증 흐름에서 수행하는 주요 역할은 무엇일까요?

사용자 비밀번호를 암호화합니다.

사용자의 권한 정보를 관리합니다.

인증 서버와 통신하여 임시 코드 및 토큰을 처리합니다.

클라이언트 측 UI를 렌더링합니다.

Câu trả lời 1

0

mh2님의 프로필 이미지
mh2
Người đặt câu hỏi

해결했습니다 ㅠㅠ

@RestController로 되어 있어서 그랬었나봐요...

@Controller 로 바꾸니 index 페이지 잘 나옵니다!!

Hình ảnh hồ sơ của mignon25
mignon25

câu hỏi đã được viết

Đặt câu hỏi