Error: {“message”:“Endpoint ‘BarAdminEndpoint’ method ‘addOrUpdateBar’ request cannot be accessed, reason: ‘Access denied’”}
even though user has the required role.
UserDetailsServiceImpl:
@Override
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
...
if (users.isEmpty()) {
System.err.println("No user present with username: " + username);
throw new UsernameNotFoundException("No user present with username: " + username);
} else {
User user = users.get(0);
return new org.springframework.security.core.userdetails.User(user.email, user.hashedPassword,
getAuthorities(user));
}
}
private static List<GrantedAuthority> getAuthorities(User user) {
List<GrantedAuthority> list = new ArrayList<>();
list.add(new SimpleGrantedAuthority(""+user.role)); // Roles are 0 for User and 1 for Admin
return list;
}
BarAdminEndpoint:
@Endpoint
@RolesAllowed(AuthenticatedUser.ROLE_ADMIN) // Is "1" string
public class BarAdminEndpoint {
public Bar addOrUpdateBar(Bar bar){
...
}
}