簡體   English   中英

Spring Security 角色 - 用戶只能更改自己的數據?

[英]Spring Security Role - user can change only own data?

我開始使用 Spring Security。 現在我設置該用戶必須登錄,如果他們不創建表。 或者,例如,在我配置為只有角色 ROLE_USER 的用戶可以刪除表的 ControllerClass 中。

我的問題是,我可以通過哪種方式設置,當用戶登錄並創建一些表並創建 teamPlayers 時,表或玩家只能編輯或刪除創建表和玩家的用戶。

例如,我在控制器方法中刪除表...

@RestController
@RequestMapping(value="/api/tables")
public class ApiTableController {

@Autowired
TableService tableService;
@Autowired
TableConverter tableConverter;

@PreAuthorize("hasRole('ROLE_USER')")    
@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
public ResponseEntity<TableDTO> deleteTable(@PathVariable Long id) {
    Table table = tableService.findOne(id);
    if (table != null) {
        TableDTO tableDTO = tableConverter.table2TableDTO(table);
        tableService.remove(id);
        return new ResponseEntity<>(tableDTO, HttpStatus.OK);
    } else {
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
}

在這種情況下,具有角色 ROLE_USER 的所有用戶都可以刪除所有表,但我不會只刪除表用戶如何創建表......它的工作原理或標准代碼有什么規則嗎? 就像 StackOwerflow 上的簡介一樣。 每個人都可以看到我們寫的內容,每個人都可以創建個人資料,只有我可以編輯我在網站上寫的個人資料或我的問題。 我怎么能用 Spring 安全做這樣的事情?

這是用戶類

@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue
@NotNull
@Column(name = "user_id")
private Long id;
@Column(name = "username")
private String name;
@Column(name = "password")
private String password;
@Column(name = "email")
private String email;
@Column(name = "country")
private String country;
@Column(name = "city")
private String city;
@Column(name = "dateCreated")
private Date dateCreated;
@Column(name = "enabled")
private boolean active;
@JoinTable(name = "user_security_role", joinColumns = { @JoinColumn(name = "user_id", 
referencedColumnName = "user_id") }, inverseJoinColumns = { 
@JoinColumn(name = "security_role_id", referencedColumnName = "id") })
@ManyToMany
private Set<SecurityRoleUser> securityRoleCollection;


@Override
public int hashCode() {

    int hash = 0;

    hash += (id != null ? id.hashCode() : 0);

    return hash;

}

@Override
public boolean equals(Object object) {

    if (!(object instanceof User)) {

        return false;

    }

    User other = (User) object;

    if ((this.id == null && other.id != null)
            || (this.id != null && !this.id.equals(other.id))) {

        return false;

    }

    return true;

}

這是班級表......

@Entity
@javax.persistence.Table(name="tblTable")
public class Table {
@Id
@GeneratedValue
@Column(name="table_id")
private Long id;
@Column(name="name", nullable=true)
private String name;
@Column(name="sport", nullable=true)
private String sport;
@Column(name="typeTable", nullable=true)
private String TypeTable;
@Column(name="dateCreated", nullable=true)
private Date dateCreated;
@Column(name="changed", nullable=true)
private Date changed;
@Column(name="description", nullable=true)
private String description;

我使用休眠、maven、RESTFull Web 服務器、backbone.js ....

不是很詳細的答案,但已經太久了,無法發表評論。

Spring 安全性附帶的功能正是您所需要的: 域對象安全性或 ACL

這是一個相當高級的功能,因為如果需要添加一組表來表示用戶對每個安全域對象的授權。 一種用於對象類,一種用於對象本身(僅存儲主鍵),另一種用於實際授權。 實際上,它可以看作是對共享文件系統的授權。

您通常使用帶有@PreAuthorize批注的 then 方法安全性, @PreAuthorize批注允許使用包含方法實際參數的表達式。 您直接允許用戶修改或刪除每個域對象。

除了上面已經引用的 Spring Security 參考手冊之外,您還可以在krams:::: Spring Security 3: Full ACL Tutorial上找到有關 ACL 的完整教程

我的建議:如果您遇到某些特定問題,請嘗試嘗試並在這里提出問題。

您可以在實體中使用 @PreRemove/@PreUpdate/@PrePersist 並實現您自己的邏輯。

  @PreRemove
    private void preventUnAuthorizedRemove() {

       String name = SecurityContextHolder.getContext().getAuthentication().getName();

      if(!name.equals(this.username)){
          throw new NotAuthorizedException("User can only delete himself ");
      }

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM