[MSA Prepare] Spring Authorization Server Client Credentials

·2024년 5월 4일
0

MSA Prepare

목록 보기
11/21
post-thumbnail

Client Credentials

  • 클라이언트가 컨텍스트 외부에서 액세스 토큰을 얻어 특정 리소스에 접근을 요청할 때 사용하는 방식
  • Access Token 정보를 요청함
  • Access Token를 응답함, Refresh Token은 사용하지 않음
  • Access Token 기반으로 Resource Server와 통신함

USE

Client 수정하기

  • AuthorizationServerConfig.java
        @Bean
        public RegisteredClientRepository registeredClientRepository() {
            RegisteredClient oidcClient = RegisteredClient.withId(UUID.randomUUID().toString())
                    .clientId("my-client")
                    .clientSecret("{noop}mypassword")
                    .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_POST)
                    .authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
                    .scope(OidcScopes.OPENID)
                    .clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
                    .tokenSettings(TokenSettings.builder().accessTokenTimeToLive(Duration.ofSeconds(360)).build())
                    .build();
            return new InMemoryRegisteredClientRepository(oidcClient);
        }
        
  • clientId, clientSecret: Id와 Secret를 설정한다. Secret의 경우 password Encoder를 사용해야한다.
  • clientAuthenticationMethod: 클라이언트 인증방식을 설정한다. POST, BASIC, JWT등을 사용할 수 있다.
  • authorizationGrantType: 인증방식을 설정한다.
  • clientSettings: 클라이언트 설정을 구성, 위의 코드에서는 사용자의 승인 동의를 추가함
  • tokenSettings: 토큰을 설정
  • InMemoryRegisteredClientRepository: 클라이언트를 InMemory 방식으로 사용함

사용해보기

리소스 서버로 확인하기

  • 간단하게 Oauth2 Resource Server를 만들어서 확인해 보았다.
  • spring.security.oauth2.resourceserver.jwt.jwk-set-uri을 http://localhost:9000/oauth2/jwks 다음으로 설정해서 사용하였다.
  • 다음과 같이 확인이 가능하다.

profile
백엔드 개발자가 꿈인 컴공과

0개의 댓글

Powered by GraphCDN, the GraphQL CDN