{"id":2500,"date":"2021-07-31T09:16:43","date_gmt":"2021-07-31T00:16:43","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=2500"},"modified":"2021-08-02T15:02:21","modified_gmt":"2021-08-02T06:02:21","slug":"spring-boot-profile-server-for-authorization-server","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=2500","title":{"rendered":"Spring Boot Profile Server for Authorization Server"},"content":{"rendered":"<h1>Spring Boot Profile Server for Authorization Server<\/h1>\n<h2>\ubaa9\ud45c<\/h2>\n<p>\uc694\uccad\ub41c \uc0ac\uc6a9\uc790 \uc815\ubcf4\ub97c \uc81c\uacf5\ud558\ub294 \ud504\ub85c\ud30c\uc77c \uc11c\ubc84\ub97c \uc0dd\uc131\ud569\ub2c8\ub2e4.<\/p>\n<h2>\uc18c\uc2a4\ucf54\ub4dc<\/h2>\n<p><a href=\"https:\/\/github.com\/skyer9\/SpringBootOauth2ProfileServer\">\uc5ec\uae30<\/a> \uc5d0 \uc804\uccb4 \uc18c\uc2a4\ucf54\ub4dc\uac00 \uc62c\ub77c\uac00 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<h2>build.gradle \uc218\uc815<\/h2>\n<pre><code class=\"language-gradle\">dependencies {\n    implementation &#039;org.springframework.boot:spring-boot-starter-oauth2-resource-server&#039;\n    implementation &#039;org.springframework.boot:spring-boot-starter-web&#039;\n    implementation &#039;org.springframework.security:spring-security-jwt&#039;\n    implementation &#039;org.springframework.boot:spring-boot-starter-data-jpa&#039;\n    implementation &#039;commons-io:commons-io:2.6&#039;\n\n    \/\/ \ubc84\uc804\uc744 \uba85\uc2dc\uc801\uc73c\ub85c \uc9c0\uc815\ud574\uc57c \ud55c\ub2e4(?)\n    implementation &#039;org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.5.2&#039;\n\n    runtimeOnly &#039;mysql:mysql-connector-java&#039;\n    compileOnly &#039;org.projectlombok:lombok&#039;\n    developmentOnly &#039;org.springframework.boot:spring-boot-devtools&#039;\n    annotationProcessor &#039;org.projectlombok:lombok&#039;\n    testImplementation &#039;org.springframework.boot:spring-boot-starter-test&#039;\n}<\/code><\/pre>\n<h2>application.yml \uc218\uc815<\/h2>\n<pre><code class=\"language-yaml\">spring:\n  datasource:\n    url: jdbc:mysql:\/\/${MYSQL_HOST:localhost}:3306\/db_oauth2\n    username: root\n    password: abcd1234\n    driver-class-name: com.mysql.cj.jdbc.Driver\n\nsecurity:\n  oauth2:\n    resource:\n      jwt:\n        key-uri: http:\/\/auth.localhost:9000\/oauth\/token_key\n\nserver:\n  port: 9001\n\nlogging:\n  level:\n    org:\n      springframework:\n        web: INFO\n        security: INFO<\/code><\/pre>\n<h2>\ud30c\uc77c \ucd94\uac00<\/h2>\n<p>User.java<\/p>\n<pre><code class=\"language-java\">@Getter\n@Entity\n@Table(name = &quot;tbl_user&quot;)\npublic class User {\n\n    @Id\n    @Column\n    private String uid;\n\n    @Column\n    private String name;\n\n    @Column\n    private String email;\n}<\/code><\/pre>\n<p>UserRepository.java<\/p>\n<pre><code class=\"language-java\">public interface UserRepository extends JpaRepository&lt;User, Long&gt; {\n\n    Optional&lt;User&gt; findByUid(String email);\n}<\/code><\/pre>\n<p>UserProfile.java<\/p>\n<pre><code class=\"language-java\">@Setter\n@Getter\npublic class UserProfile {\n\n    private String name;\n    private String email;\n\n    public UserProfile(String name) {\n        this.name = name;\n    }\n}<\/code><\/pre>\n<p>UserInfoController.java<\/p>\n<pre><code class=\"language-java\">@RequiredArgsConstructor\n@RestController\n@RequestMapping(&quot;\/api&quot;)\npublic class UserInfoController {\n\n    private final UserRepository userRepository;\n\n    @GetMapping(&quot;\/userinfo&quot;)\n    public ResponseEntity&lt;?&gt; userInfo() {\n\n        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();\n\n        String name = authentication.getName();\n        UserProfile profile = new UserProfile(name);\n\n        OAuth2Authentication oAuth2Authentication;\n        if (authentication instanceof OAuth2Authentication) {\n            oAuth2Authentication = (OAuth2Authentication) authentication;\n            Set&lt;String&gt; scopes = oAuth2Authentication.getOAuth2Request().getScope();\n\n            Optional&lt;User&gt; user = userRepository.findByUid(name);\n            if (user.isEmpty()) {\n                return ResponseEntity.ok(profile);\n            }\n\n            if (scopes.contains(&quot;email&quot;)) {\n                profile.setEmail(user.get().getEmail());\n            }\n        }\n\n        return ResponseEntity.ok(profile);\n    }\n}<\/code><\/pre>\n<h2>Profile Server \uc5d0 \ud68c\uc6d0\uac00\uc785 API \ucd94\uac00<\/h2>\n<p><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=2508\">Profile Server \uc5d0 \ud68c\uc6d0\uac00\uc785 API \ucd94\uac00<\/a><\/p>\n<h2>\ube44\ubc00\ubc88\ud638 \uaddc\uce59 \uac15\ud654<\/h2>\n<p><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=2513\">\ube44\ubc00\ubc88\ud638 \uaddc\uce59 \uac15\ud654<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Boot Profile Server for Authorization Server \ubaa9\ud45c \uc694\uccad\ub41c \uc0ac\uc6a9\uc790 \uc815\ubcf4\ub97c \uc81c\uacf5\ud558\ub294 \ud504\ub85c\ud30c\uc77c \uc11c\ubc84\ub97c \uc0dd\uc131\ud569\ub2c8\ub2e4. \uc18c\uc2a4\ucf54\ub4dc \uc5ec\uae30 \uc5d0 \uc804\uccb4 \uc18c\uc2a4\ucf54\ub4dc\uac00 \uc62c\ub77c\uac00 \uc788\uc2b5\ub2c8\ub2e4. build.gradle \uc218\uc815 dependencies { implementation &#039;org.springframework.boot:spring-boot-starter-oauth2-resource-server&#039; implementation &#039;org.springframework.boot:spring-boot-starter-web&#039; implementation &#039;org.springframework.security:spring-security-jwt&#039; implementation &#039;org.springframework.boot:spring-boot-starter-data-jpa&#039; implementation &#039;commons-io:commons-io:2.6&#039; \/\/ \ubc84\uc804\uc744 \uba85\uc2dc\uc801\uc73c\ub85c \uc9c0\uc815\ud574\uc57c \ud55c\ub2e4(?) implementation &#039;org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.5.2&#039; runtimeOnly &#039;mysql:mysql-connector-java&#039; compileOnly &#039;org.projectlombok:lombok&#039; developmentOnly &#039;org.springframework.boot:spring-boot-devtools&#039; annotationProcessor &#039;org.projectlombok:lombok&#039; testImplementation &#039;org.springframework.boot:spring-boot-starter-test&#039; } application.yml\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=2500\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[],"class_list":["post-2500","post","type-post","status-publish","format-standard","hentry","category-spring-boot-2-5"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2500","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2500"}],"version-history":[{"count":3,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2500\/revisions"}],"predecessor-version":[{"id":2521,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/2500\/revisions\/2521"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}