Authorization responsibilities are completely extracted to separate server, which grants OAuth2 tokens for the backend resource services. Auth Server is used for user authorization as well as for secure machine-to-machine communication inside a perimeter.
In this project, I use Password credentials
grant type for users authorization (since it's used only by native PiggyMetrics UI) and Client Credentials
grant for microservices authorization.
Spring Cloud Security provides convenient annotations and autoconfiguration to make this really easy to implement from both server and client side. You can learn more about it in documentation and check configuration details in Auth Server code.
From the client side, everything works exactly the same as with traditional session-based authorization. You can retrieve Principal
object from request, check user's roles and other stuff with expression-based access control and @PreAuthorize
annotation.
Each client in PiggyMetrics (account-service, statistics-service, notification-service and browser) has a scope: server
for backend services, and ui
- for the browser. So we can also protect controllers from external access, for example:
@PreAuthorize("#oauth2.hasScope('server')")
@RequestMapping(value = "accounts/{name}", method = RequestMethod.GET)
public List<DataPoint> getStatisticsByAccountName(@PathVariable String name) {
return statisticsService.findByAccountName(name);
}
For more information please refer to the main repository afermon/PiggyMetrics-Kubernetes
- Forked from sqshq/PiggyMetrics