It is safe, but depending on your situation, you may want to rather use the Ledger API RPC PartyManagementService#AllocateParty (which is what daml ledger allocate-parties uses), which can allow you to integrate party allocation in an existing application.
EDIT: as brought up by @Gary_Verhaegen in the following answer the JSON API also allows you to allocate a party. That is likely to be easier than and preferable to interacting with the Ledger API directly.
You need to call stub.withCallCredentials and pass an instance of CallCredential that passes the Authorization header with the Bearer schema. This is the current implementation for it in the Java bindings.
If you’re working with a ledger that requires authorization, you need a token with admin claims. This can either be a user token for a user that has admin rights or a custom claims token with admin = true. See Authorization — Daml SDK 2.2.0 documentation for details.