Spring Security OAuth2 Client 自定义配置

Spring Security OAuth2 Spring Boot About 1,359 words

添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>

原始 Spring Boot 自动配置

provider

provider下的keycloak是自定义的名称,可任意取名。

spring:
  security:
    oauth2:
      client:
        provider:
          keycloak:
            issuer-uri: http://localhost:8080/realms/my-realm

registration

registration下的keycloak是自定义的名称,可任意取名。

keycloak下的provider是对应上文中的provider下的keycloak

spring:
  security:
    oauth2:
      client:
        registration:
          keycloak:
            provider: keycloak
            client-id: my-client
            client-secret: 
#            redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
            scope:
              - openid
#              - offline_access
#              - email
#              - profile
#              - roles

Java 代码配置

@Bean
ClientRegistrationRepository clientRegistrationRepository() {
    ClientRegistration clientRegistration = ClientRegistrations.fromIssuerLocation("http://localhost:8080/realms/my-realm")
            .registrationId("keycloak")
            .clientId("my-client")
            .clientSecret("")
            .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
            .scope("openid")
            .build();
    return new InMemoryClientRegistrationRepository(clientRegistration);
}
Views: 438 · Posted: 2024-05-06

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh