'Spring Security Google OAuth2 authorizationUri became domain after deployed on aws

So I am current testing with spring securtiy with google oauth. it works fine, when trying to login with google through /oauth2/authorization/google on localhost but when i deployed the application on tomcat on aws ec2 instance, the authorizationuri became sometime like below

when running on localhost: https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount? ............

when running on aws ec2 domain: http://{domain}:{port}/o/oauth2/v2/auth? ..........

but when i manually replace the http domain and port with https://accounts.google.com/ it directs me to google login page and are able to complete the login successfully.

So i wonder if there is any part of the setting ive being missing or did wrong that cause it to happen. Thanks in advance.

the complete code for spring security setting is like below

@Configuration

@EnableWebSecurity class SecurityConfig : WebSecurityConfigurerAdapter() {

@Autowired
private lateinit var oidUserSer:OidUserService


@Bean
fun clientRegistrationRepository(): ClientRegistrationRepository {
    return InMemoryClientRegistrationRepository(googleClientRegistration())
}

private fun googleClientRegistration(): ClientRegistration {
    return ClientRegistration.withRegistrationId("google")
        .clientId("clientId")
        .clientSecret("secret")
        .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
        .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
        .redirectUri("https://{domain:port}/login/oauth2/code/{registrationId}")
        .scope("openid", "profile", "email", "address", "phone")
        .authorizationUri("https://accounts.google.com/o/oauth2/v2/auth")
        .tokenUri("https://www.googleapis.com/oauth2/v4/token")
        .userInfoUri("https://www.googleapis.com/oauth2/v3/userinfo")
        .userNameAttributeName(IdTokenClaimNames.SUB)
        .jwkSetUri("https://www.googleapis.com/oauth2/v3/certs")
        .clientName("Google")
        .build()
}

@Bean
fun customAuthorizationRequestResolver(): CustomAuthorizationRequestResolver {
    val repo = InMemoryClientRegistrationRepository(
        CommonOAuth2Provider.GOOGLE.getBuilder("google")
            .clientName("Google")
            .clientId("61770666483-dj64uabnia7tq2g0kri2ajrb7sl21r3t.apps.googleusercontent.com")
            .clientSecret("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")
            .redirectUri("https://{domain:port}/login/oauth2/code/{registrationId}")
            .build()
    )

    val baseUri = OAuth2AuthorizationRequestRedirectFilter.DEFAULT_AUTHORIZATION_REQUEST_BASE_URI
    val customizeAuthorizationRequest= CustomAuthorizationRequestResolver(repo,baseUri)


    return customizeAuthorizationRequest
}


/**
 * セキュリティの有効範囲設定
 */
@Override
override fun configure(web: WebSecurity) {
    // org.springframework.security.web.firewall.RequestRejectedException:
    // The request was rejected because the URL contained a potentially malicious String ";"
    // というエラーログがコンソールに出力されるため、下記を追加
    val firewall = DefaultHttpFirewall()
    web.httpFirewall(firewall)
    web.ignoring().antMatchers(
        "/img/**",
        "/css/**",
        "/js/**",
        "/libs/**"
    )
}
@Override
@Throws(Exception::class)
override fun configure(http: HttpSecurity) {     


    http.exceptionHandling()

        .authenticationEntryPoint(LoginUrlAuthenticationEntryPoint("/login"));


    http.oauth2Login()
        .loginPage("/login")                       
        .defaultSuccessUrl("/login-success", true)   
        .userInfoEndpoint().oidcUserService(oidUserSer).

        and()
        .failureUrl("/login?error")
        .authorizationEndpoint()
        .authorizationRequestResolver(customAuthorizationRequestResolver())



  
    http.logout()
        //.logoutRequestMatcher(AntPathRequestMatcher("/logout**"))  
        .logoutUrl("/logout")
        .logoutSuccessUrl("/login?logout")     
        .logoutSuccessHandler(CustomLogoutSuccessHandler())
        //.invalidateHttpSession(true)
    //セッション設定
    http.sessionManagement().
    invalidSessionUrl("/login?timeout=true")


}

}

class CustomAuthorizationRequestResolver(repo: ClientRegistrationRepository?,
                                     authorizationRequestBaseUri: String?):OAuth2AuthorizationRequestResolver{
private var defaultResolver:DefaultOAuth2AuthorizationRequestResolver? = DefaultOAuth2AuthorizationRequestResolver(repo, authorizationRequestBaseUri)

override fun resolve(request: HttpServletRequest): OAuth2AuthorizationRequest? {
    val authorizationRequest:OAuth2AuthorizationRequest? =
        this.defaultResolver?.resolve(request)
    return customAuthorizationRequest(authorizationRequest)
}


override fun resolve(request: HttpServletRequest?, clientRegistrationId: String?): OAuth2AuthorizationRequest? {
    val  authorizationRequest = this.defaultResolver?.resolve(
        request, clientRegistrationId);

    return authorizationRequest?.let { customAuthorizationRequest(it) }
}

private fun customAuthorizationRequest(authorizationRequest: OAuth2AuthorizationRequest?): OAuth2AuthorizationRequest? {
    var param:OAuth2AuthorizationRequest? = null
    if(authorizationRequest!=null){
        val additionalParameter: LinkedHashMap<String,Any> = LinkedHashMap(authorizationRequest!!.additionalParameters)
        additionalParameter.put("prompt","select_account+consent")
        param = OAuth2AuthorizationRequest.from(authorizationRequest).additionalParameters(additionalParameter).build()
    }
    return param
}

}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source