[英]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.