/
JWTトークン検証を有効にする - OpenLegacy API
JWTトークン検証を有効にする - OpenLegacy API
OpenLegacy APIは、Springブートアプリケーションであるため、わずか数ステップでJWT検証を有効にすることができます。
前提条件:
OpenLegacy IDE (すべてのMaven依存関係とJDK11を含む完全インストール)
JWT トークン
JWK-URI
OpenLegacy API および SDK
ステップ1: APIの Pom.xmlの編集
APIの pom.xmlへ移動します。
securityに関連する依存性、テストの依存性も削除します。
以下の依存性を追加します:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-resource-server</artifactId> </dependency>
ステップ2: セキュリティクラスの構成
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 の変更
man/src/resources
フォルダーのApplication.ymlへ移動します。以下の構成を追加します。
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
Enable JWT Token Validation - OpenLegacy API
Enable JWT Token Validation - OpenLegacy API
More like this
How To Disable OAuth2 Security in OpenLegacy API Projects
How To Disable OAuth2 Security in OpenLegacy API Projects
More like this
OAuth2 プロバイダの構成方法
OAuth2 プロバイダの構成方法
More like this
API プロパティ構成
API プロパティ構成
More like this
How to Configure OAuth2 Provider
How to Configure OAuth2 Provider
More like this
API Caller RESTful API Documentation
API Caller RESTful API Documentation
More like this