Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bypass Spring Security @preauthorize

I'm calling the following method from the web layer throw a logged in user that has the attached permission:

@PreAuthorize("hasRole('list_users_permission')")
public List<UserDto> getAllUsers() {
    ........
}

But now I want to call it through a scheduler job, which means that I haven't a logged in user.

Is there a way to bypass this annotation @PreAuthorize("hasRole('list_users_permission')") or to create a virtual user with all the needed permissions ?

like image 303
Abdullah Avatar asked Sep 21 '26 19:09

Abdullah


1 Answers

First, make getAllUsers() delegate to a non-secured method:

@PreAuthorize("hasRole('list_users_permission')")
public List<UserDto> getAllUsers() {
    return doGetAllUsers();
}

public List<UserDto> doGetAllUsers() {
    ...
}

Then make the scheduled code invoke doGetAllUsers().

like image 67
Jukka Avatar answered Sep 23 '26 09:09

Jukka