【问题标题】:Spring Security - Can´t use @Secured with the RoleSpring Security - 不能将@Secured 与角色一起使用
【发布时间】:2020-06-19 08:51:54
【问题描述】:

我的用户具有表角色中的角色。表角色有 roleId 和 roleName。

1   ADMIN

现在我想在后端使用@Secured。 我试过了:

    @RequestMapping("/listShifts", method = [RequestMethod.GET])
    @Secured("ADMIN")
    fun listShifts(model: Model, @RequestParam(required = false) search: String?, employeeId: Int?, departmentId: Int?): String {
        return "listShifts"
    }

但我无法访问该网站。

我该如何解决这个问题?

用户实体看起来像:

@Table(uniqueConstraints = [UniqueConstraint(name = "EMailAddress_UK", columnNames = ["EMailAddress"])])
class Employee(
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        var employeeId: Int? = null,
        @ManyToOne
        var role: Role? = Role(3),

}

谢谢。

【问题讨论】:

    标签: spring kotlin spring-security


    【解决方案1】:

    要在 Spring Boot 应用程序中启用 @Secured 注释,您需要首先通过将 @EnableGlobalMethodSecurity 注释添加到应用程序中具有 @Configuration 注释或本身是配置类的任何类来启用全局方法安全性

    查看此链接了解更多信息:

    https://www.appsdeveloperblog.com/spring-security-secured-annotation-example/#:~:text=To%20enable%20%40Secured%20annotation%20in,is%20a%20configuration%20class%20itself

    【讨论】:

      猜你喜欢
      • 2016-01-03
      • 2020-05-25
      • 2012-01-12
      • 2021-09-27
      • 2015-07-29
      • 2014-01-18
      • 2011-12-16
      • 2021-09-03
      • 2016-05-25
      相关资源
      最近更新 更多