繁体   English   中英

春季安全全球授权

[英]global authorization in spring security

我正在努力把我的头围在Spring Security上。 我要实现的是,API是全局安全的,因此不是特定于方法的,具体取决于HTTP谓词和用户角色。

因此,在GET方法上,您需要进行身份验证并承担USER角色。 所有其他都需要ADMIN特权。 我的用户实体类如下(使用Lombok数据注释):

@Entity
@Data
@NoArgsConstructor
@Table(uniqueConstraints=@UniqueConstraint(columnNames={"username"}))

public class User {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private long id;

  @NotNull
  private String username, password;

  @NotNull
  @Enumerated(EnumType.STRING)
    private UserRole role;

  private boolean activated, tokenExpired;
}

任何帮助将不胜感激,而不希望项目中没有XML。

我是老手,有很多老把戏,所以这里是XML解决方案:

applicationContext-security.xml

<http use-expressions="true" disable-url-rewriting="true">

    <intercept-url pattern="/**" method="GET" access="hasRole('ROLE_USER')"/>
    <intercept-url pattern="/**" access="hasRole('ROLE_ADMIN')"/>

    <http-basic/>

</http>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM