/
JWTトークン検証を有効にする - OpenLegacy API

JWTトークン検証を有効にする - OpenLegacy API

OpenLegacy APIは、Springブートアプリケーションであるため、わずか数ステップでJWT検証を有効にすることができます。

前提条件:

  1. OpenLegacy IDE (すべてのMaven依存関係とJDK11を含む完全インストール)

  2. JWT トークン

  3. JWK-URI

  4. OpenLegacy API および SDK

 

ステップ1: APIの Pom.xmlの編集

  1. APIの pom.xmlへ移動します。

  2. securityに関連する依存性、テストの依存性も削除します。

  3. 以下の依存性を追加します:

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

 

ステップ2: セキュリティクラスの構成

  1. com.{your_api}.openlegacy.config パッケージに、以下のクラスを追加します。

    import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer; @Configuration public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests(authorizeRequests -> authorizeRequests // doesn't require authentication. .antMatchers("/swagger**/**").permitAll() // you can add more permit endpoints //require authentication. .anyRequest().authenticated() ) // Uses Jwt security. .oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt); } }

 

このクラスでは、必要に応じエンドポイントのアクセスを管理できます。

この例では、"/swagger**/**が許可されており、認証は必要ありませんが、他のすべてのエンドポイントには認証が必要です。

 

 

ステップ3: Application yml の変更

  1. man/src/resources フォルダーのApplication.ymlへ移動します。

  2. 以下の構成を追加します。

    spring: security: oauth2: resourceserver: jwt: jwk-set-uri: {{your_jwk-uri}}

 

これで APIのテストをすることができます。

 

 Note, you can add the following configuration to get more information in the logs. logging.level.org.springframework.security: trace

 

Related content