public boolean addRootUserAndRole()
{
// TODO Auto-generated method stub
//1.创建root角色
Srole role = new Srole(DEFAULT_ROOT_ROLE);
role.setDescription(DEFAULT_ROOT_ROLE);
sroleDAO.merge(role);//不管是否存在,直接覆盖
//2.创建root用户(若不存在)
List<Users> users = usersDAO.findByName(DEFAULT_ROOT_USER);
Users user = null;
if(users==null||users.size()==0)//没找到用户名为root的用户,则要创建
{
user = new Users();
user.setId(MyUUIDGen.getUUID());
user.setName(DEFAULT_ROOT_USER);
user.setSalt(MyUUIDGen.getUUID());
String encodedPassword = passwordEncoder.encodePassword(DEFAULT_ROOT_USER_PASSWORD,user.getSalt());
user.setPassword(encodedPassword);
user.setEnabled(true);
usersDAO.merge(user);
}
else
{
user = users.get(0);
}
//3.关联root用户与root角色
AuthoritiesId aid = new AuthoritiesId(user.getId(), role.getAuthority());
Authorities authority = new Authorities(aid, user, role);
authoritiesDAO.merge(authority);
return true;
}