【发布时间】:2015-10-21 14:26:06
【问题描述】:
我需要将 Spring Security 添加到我的项目中。正确的方法是什么?我必须为他们创建实体 User 和 UserRole 以及 DAO 和 Services。我使用 EntityManager 来访问数据。我读到,我只需要为 UserDetails 编写实现,但我不知道如何正确地做到这一点。这是我的代码:
用户.java
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonProperty
private Integer id;
@Column(name = "username", length = 20, nullable = false)
@JsonProperty
private String username;
@Column(name = "password", nullable = false)
@JsonProperty
private String password;
@Column(name = "enabled", nullable = false)
@JsonProperty
private boolean enabled;
@Column(name = "email", nullable = false)
@JsonProperty
private String email;
@OneToMany(mappedBy = "user", cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
private Set<UserRole> userRoles;
//getters and setters
UserRole.java
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonProperty
private Integer id;
@ManyToOne(optional = false)
@JoinColumn(name = "user_ID", referencedColumnName = "id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private User user;
@Column(name="role")
@JsonProperty
private String role;
//getters and setters
我该怎么办?
【问题讨论】:
标签: java spring spring-mvc jpa spring-security