I have configured an IdentityServer4 server to protect my API resources.
I have linked my .NET Core based API resource to the Identity Server but I need to know how to link a Java-based API resource.
I have a REST API based on Spring Framework and I need to configure it in a way it validates incoming JWT tokens with the Identity Server.
I found the answer to my own question.
I use Gradle so I added the following line to my build.gradle file:
compile group: 'org.springframework.security.oauth.boot', name: 'spring-security-oauth2-autoconfigure', version: '2.1.1.RELEASE'
Then is my application.properties file I added the following:
security.oauth2.client.client-id=<API_RESOURCE_ID>
security.oauth2.client.client-secret=<API_RESOURCE_SECRET>
security.oauth2.client.access-token-uri=<TOKEN_ENDPOINT>
security.oauth2.client.user-authorization-uri=<AUTHORIZE_ENDPOINT>
security.oauth2.client.scope=openid
security.oauth2.resource.filter-order=3
security.oauth2.resource.user-info-uri=<USERINFO_ENDPOINT>
security.oauth2.resource.token-info-uri=<INTROSPECT_ENDPOINT>
security.oauth2.resource.prefer-token-info=true
logging.level.org.springframework.security=DEBUG
Then I needed to implement ResourceServerConfigurerAdapter interface.
@Override
public void configure(HttpSecurity http) throws Exception {
http
.requestMatcher(new RequestHeaderRequestMatcher("Authorization"))
.authorizeRequests().anyRequest().fullyAuthenticated();
}
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.resourceId(<API_RESOURCE_ID>);
}
And finally adding @EnableOAuth2Sso to my Application.java file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With