I am trying to submit a basic batch job from within my startup bean, which is giving me an error message of "User UNAUTHENTICATED is not authorized to start batch jobs."
This is my startup bean:
@Singleton
@Startup
public class ControllerBean {
@PersistenceContext(unitName = "item-persister")
EntityManager entityManager;
@PostConstruct
public void initialize() {
JobOperator jobOperator = BatchRuntime.getJobOperator();
long execID = jobOperator.start("testjob", null);
}
}
In my server.xml, I have configured a username and password:
<basicRegistry id="basic" realm="ibm/api">
<user name="bob" password="bobpwd"/>
</basicRegistry>
<authorization-roles id="com.ibm.ws.batch">
<security-role name="batchAdmin">
<user name="bob"/>
</security-role>
</authorization-roles>
How do I authenticate properly so that my job can be run by a startup bean?
An easy way is to:
Configure a RunAs identity
You need to align the
@RunAs
annotation value with the server configuration.In the server config (server.xml):
In your Java code calling JobOperator:
In your snippet you had the basic registry and the user mapped to the batch authorization role. You just needed to establish this user on the thread via the
@RunAs
.