I'm using ConfigBean in order to populate my BD, like so:
@Singleton
@Startup
@RunAs("Administrator")
public class ConfigBean {
@EJB
private ClientBean clientBean;
@EJB
private AdministratorBean adminstratorBean;
@PostConstruct
public void populateDB() {
try{
clientBean.create(new ClientDTO("client1", "secret", "Manuel", "[email protected]", "Av. José Maceda", "918 923 232"));
clientBean.create(new ClientDTO("client2", "secret", "Manuel", "[email protected]", "Av. Alberto Alves", "+00351 256 0033 12"));
adminstratorBean.create(new AdministratorDTO("administrator1", "secret", "Manuel", "[email protected]", "Director"));
}catch(Exception ex){
System.out.println(ex);
}
}
Both my beans look like so:
@POST
@RolesAllowed("Administrator")
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
public Response create(AdministratorDTO administratorDTO){...}
As for my web.xml:
...
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>dae_project_realm</realm-name>
</login-config>
<security-role>
<description/>
<role-name>Client</role-name>
</security-role>
<security-role>
<role-name>Administrator</role-name>
</security-role>
</web-app>
Everything seems ok. However, when I run the application and the method populateDB is called, I keep getting the error:
javax.ejb.AccessLocalException: Client not authorized for this invocation
I saw a few interesting links, but I still couldn't get it to work:
In order to use @RunAs, we need to create a user of that role. For that, you need to do the following steps.
Update @RunAs to use the new created user:
@Singleton
@Startup
@RunAs("BOB") //BOB belongs to the desired group
public class ConfigBean {
@EJB
private ClientBean clientBean;
@EJB
private AdministratorBean adminstratorBean;
@PostConstruct
public void populateDB() {
try{
clientBean.create(new ClientDTO("client1", "secret", "Manuel", "[email protected]", "Av. José Maceda", "918 923 232"));
clientBean.create(new ClientDTO("client2", "secret", "Manuel", "[email protected]", "Av. Alberto Alves", "+00351 256 0033 12"));
adminstratorBean.create(new AdministratorDTO("administrator1", "secret", "Manuel", "[email protected]", "Director"));
}catch(Exception ex){
System.out.println(ex);
}
}