I am using Spring Authorization Server 1.1.2 for OAuth2 implementation with Spring Boot 3. For fetching oauth2 token, I have a custom page OauthRedirect.html
which calls oauth2/authorize
and oauth2/token
endpoints and the token is displayed on the same page similar to Google Playground. My application will be hosted on a VM with docker where the host name will be dynamic.
Now, I have an InMemoryClient registered as below:
@Bean
public RegisteredClientRepository registeredClientRepository() throws UnknownHostException, ReflectionException, MalformedObjectNameException, AttributeNotFoundException, InstanceNotFoundException, MBeanException {
RegisteredClient registeredClient = RegisteredClient.withId(UUID.randomUUID().toString())
.clientId(oauthProperties.clientId)
.clientSecret(passwordEncoder().encode(oauthProperties.clientSecret))
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
.authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
.redirectUri("http://localhost:"+oauthProperties.localServerPort+"/oauthredirect)
.scope("read")
.scope("write")
.build();
return new InMemoryRegisteredClientRepository(registeredClient);
}
And below is the oauth/authorize
call from UI as I want to come back to same page:
function authorizeApis() {
let redirectUri = window.location.protocol + '//' + window.location.host + window.location.pathname;
window.location.href = 'oauth2/authorize?client_id=ecat&scope=read&response_type=code&response_mode=query&redirect_uri=' + redirectUri;
}
This works fine for localhost. However,the issue is with redirectUri
value when deployed on docker. It will change as per the hostname and port. How would my registered client will know the host and port at the time of bean creation to have the same redirectUri
that UI is sending? I have tried using InetAddress
to get the address but it does not work as expected. Also, I could not find something like OAuth2AuthorizationRequestResolver
in authorization server. What can be the alternative for this? Please help.