Adding my implementation on top of baeldung's spring-auth-server examples by simply extending the authorization type to support spring extension grant type
Analogous to your requirement:
curl --location 'http://127.0.0.1:9000/oauth2/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Authorization: Basic YXJ0aWNsZXMtY2xpZW50OnNlY3JldA==' \
--header 'Cookie: JSESSIONID=86898AB2DB4AF13A884E2321B681876A' \
--data-urlencode 'grant_type=urn:ietf:params:oauth:grant-type:custom_code' \
--data-urlencode 'code=7QR49T1W3'
I'm hoping that these will give some path to proceed you to the next level. Added the code commit here for your reference