我希望我的资源是这样的。相同的方法映射,但每个都将根据发送请求的权限进行调用。对此有何解决方案?
@RestController
@RequestMapping("/test")
public class TestResource {
@GetMapping
@PreAuthorize("hasAuthority('COMMITTEE')")
public String testForCommittee() {
return "This is a test. Custom result for committee.";
}
@GetMapping
@PreAuthorize("hasAuthority('ADMIN')")
public String testForAdmin() {
return "This is a test. Custom result for admin.";
}
}
答案 0 :(得分:0)
可能不是一个完美的解决方案,但这对你来说可能是一个很好的工作方式。
您可以在控制器中获得对Principal的引用。您可以使用泛型java.security.Principal。我使用了org.springframework.security.oauth2.provider.OAuth2Authentication,因为我正在使用OAuth。
@GetMapping
public String testForCommittee(org.springframework.security.oauth2.provider.OAuth2Authentication principal) {
Collection<GrantedAuthority> authorities = principal.getAuthorities();
//since you have all the authorities you can switch method call depending on the authorities
if(authorities.contains(new SimpleGrantedAuthority("COMMITTEE"))){
//user has COMMITEE authority.
}else if{
// check more
}
return "This is a test. Custom result for committee.";
}