I want add a guest user with Ajax request :
$.ajax({
type : "GET",
url : restUrlUser + "/front/saveGuestUser",
success : function(res) {
if (res)
return res.id
}
});
and in the controller i get the request with this method :
@RequestMapping("/front/saveGuestUser")
@ResponseBody
public Long saveGuestUser() {
return iUserService.saveGuestUser();
}
and the in service layer i use the SecurityContextHolder and add the user into it :
@Transactional
@Override
public User saveGuestUser(){
User entity=new User();
Long userId=iUserRepository.getNextAutoIncrement();
entity.setId(userId);
entity.setUserName("guestUser"+userId.toString());
entity.setFirstName("guestUser"+userId.toString());
entity.setLastName("guestUser"+userId.toString());
entity.setPassWord("password"+userId.toString());
Set<GrantedAuthority> grant = new HashSet<GrantedAuthority>();
grant.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
super.save(entity);
entity.setAuthorities(grant);
Authentication authentication = new UsernamePasswordAuthenticationToken(entity, null, entity.getAuthorities());
SecurityContextHolder.getContext().setAuthentication(authentication);
return entity;
}
but when the request returns to the User Interface and i want to use application in another tab i get the 403 access denied . what should i have to do ?