如何使用@preauthorize检查用户是否具有任何角色

发布于 2025-02-01 04:57:24 字数 2861 浏览 2 评论 0原文

我遵循了文章, https://learn.microsoft.com/en-us/azure/developer/java/spring-framework/configure-configure-spring-spring-spring-spring-boot-starter-starter-starter-starter-starter-java-java-app-with-app-with-app-apzure -Active-Dilectory 并用Hasauthority创建了一个应用程序角色

,我可以验证一个角色。但是,我想允许用户是管理员或拥有贡献者许可的用户。我尝试使用多个角色的https://stackoverflow com.com/questions/questions/questions/questions/questions/questions/questions/questions/questions/questions/preauthorize 中。

//  @PreAuthorize("hasAuthority('APPROLE_Admin')")
    @PreAuthorize("hasRole('Admin')")
    @GetMapping("/tutorials")
    public ResponseEntity<List<Tutorial>> getAllTutorials(@RequestParam(required = false) String title) {
        try {
            List<Tutorial> tutorials = new ArrayList<Tutorial>();

但是它会引发以下错误

”“在此处输入图像描述”

更新:24/05/2022

此代码正常工作

 @PreAuthorize("hasAnyAuthority('APPROLE_Admin', 'APPROLE_Contributor')")

,但以下代码抛出了错误

 //@PreAuthorize("hasRole('ROLE_Admin')")
 //@PreAuthorize("hasAnyRole('Admin', 'Contributor')")
 //@PreAuthorize("hasAnyRole('ROLE_Admin', 'ROLE_Contributor')")

logs:

[nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : GET "/api/tutorials", parameters={}
[nio-8080-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.bezkoder.spring.mssql.controller.TutorialController#getAllTutorials(String)
[nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Failed to complete request: org.springframework.security.access.AccessDeniedException: Access is denied
[nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}
[nio-8080-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
[nio-8080-exec-5] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
[nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 403

I followed the article, https://learn.microsoft.com/en-us/azure/developer/java/spring-framework/configure-spring-boot-starter-java-app-with-azure-active-directory and created an App role

with hasAuthority, I could validate a single role. However, I want to allow the user who is either an Admin or has Contributor permission. I tried with hasRole annotation as suggested in Multiple roles using @PreAuthorize.

//  @PreAuthorize("hasAuthority('APPROLE_Admin')")
    @PreAuthorize("hasRole('Admin')")
    @GetMapping("/tutorials")
    public ResponseEntity<List<Tutorial>> getAllTutorials(@RequestParam(required = false) String title) {
        try {
            List<Tutorial> tutorials = new ArrayList<Tutorial>();

But it throws the following error

enter image description here

Update: 24/05/2022

This code is working

 @PreAuthorize("hasAnyAuthority('APPROLE_Admin', 'APPROLE_Contributor')")

But the below code is throwing an error

 //@PreAuthorize("hasRole('ROLE_Admin')")
 //@PreAuthorize("hasAnyRole('Admin', 'Contributor')")
 //@PreAuthorize("hasAnyRole('ROLE_Admin', 'ROLE_Contributor')")

Logs:

[nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : GET "/api/tutorials", parameters={}
[nio-8080-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.bezkoder.spring.mssql.controller.TutorialController#getAllTutorials(String)
[nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Failed to complete request: org.springframework.security.access.AccessDeniedException: Access is denied
[nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}
[nio-8080-exec-5] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
[nio-8080-exec-5] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
[nio-8080-exec-5] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 403

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文