使用Spring Boot 1.5.2和Thymeleaf 2.1,我試圖在HTML頁面上添加一些代碼來標識用戶的角色。Thymeleaf中的Spring Security表達式
然而,所有這些語句的評價爲真哪個是不正確的:
<div sec:authorize="hasAuthority('ADMIN')" > Has Authority ADMIN </div>
<div sec:authorize="hasAuthority('USER')" > Has Authority USER </div>
<div sec:authorize="hasRole('ROLE_ADMIN')">Has Role ROLE_ADMIN</div>
<div sec:authorize="hasRole('ROLE_USER')">Has Role ROLE_USER</div>
<div sec:authorize="hasRole('ADMIN')">Has Role ADMIN</div>
<div sec:authorize="hasRole('USER')">Has Role USER</div>
User.java
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles;
Role.java
@Entity
@Table(name = "role")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
@Column(name = "role")
private String role;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}