Spring MVC, getting principal from security context in service layer

前端 未结 1 1985
無奈伤痛
無奈伤痛 2020-12-29 07:38
  1. What are the advantages to get principal as a parameter Principal principal in spring controller and then pass it to service layer over getting principal
相关标签:
1条回答
  • 2020-12-29 08:14
      • Your service API will be more easy to use. You will see dependency on principal directly, so you wan't call some service method by mistake in environment where principal does not exist.
      • In general less dependencies on SpringSecurity code means less problems in a case of migration to new Spring Security version.
      • You will be able to reuse your service layer in environment where Spring Security does not exist.
    1. Prepare some wrapper class (for example AuthenticationService). Add getPrincipal() method to it. Implement your checks. Inject AuthenticationService everywhere insted of direct calls to SecurityContextHolder.
    0 讨论(0)
提交回复
热议问题