मैं हाइबरनेट/जेपीए 2 में स्प्रिंग सुरक्षा डेटाबेस प्रमाणीकरण के साथ काम करने के लिए डीएओ को लागू करने की कोशिश कर रहा हूं। स्प्रिंग क्रम में निम्नलिखित संबंधों और संगठनों का उपयोग करता है उपयोगकर्ता & भूमिकाओं का प्रतिनिधित्व करने के लिए:हाइबरनेट/जेपीए 2 के साथ वसंत सुरक्षा उपयोगकर्ता/प्राधिकरणों को कैसे लागू करें?
PostgreSQL बनाने क्वेरी के रूप में repesented:
CREATE TABLE users
(
username character varying(50) NOT NULL,
"password" character varying(50) NOT NULL,
enabled boolean NOT NULL,
CONSTRAINT users_pkey PRIMARY KEY (username)
);
CREATE TABLE authorities
(
username character varying(50) NOT NULL,
authority character varying(50) NOT NULL,
CONSTRAINT fk_authorities_users FOREIGN KEY (username)
REFERENCES users (username) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
);
GrantedAuthorities
, UserDetailsService
और UserDetailsmanager
की ऑन-बोर्ड कार्यान्वयन का उपयोग करना, सब कुछ ठीक है। हालांकि, मैं वसंत के जेडीबीसी कार्यान्वयन से संतुष्ट नहीं हूं और अपने स्वयं के लिखना चाहूंगा। ऐसा करने के लिए में, मैं निम्नलिखित व्यापार वस्तुओं से संबंधों का प्रतिनिधित्व बनाने की कोशिश की:
उपयोगकर्ता इकाई:
@Entity
@Table(name = "users", uniqueConstraints = {@UniqueConstraint(columnNames = {"username"})})
public class AppUser implements UserDetails, CredentialsContainer {
private static final long serialVersionUID = -8275492272371421013L;
@Id
@Column(name = "username", nullable = false, unique = true)
private String username;
@Column(name = "password", nullable = false)
@NotNull
private String password;
@OneToMany(
fetch = FetchType.EAGER, cascade = CascadeType.ALL,
mappedBy = "appUser"
)
private Set<AppAuthority> appAuthorities;
@Column(name = "accountNonExpired")
private Boolean accountNonExpired;
@Column(name = "accountNonLocked")
private Boolean accountNonLocked;
@Column(name = "credentialsNonExpired")
private Boolean credentialsNonExpired;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "personalinformation_fk", nullable = true)
@JsonIgnore
private PersonalInformation personalInformation;
@Column(name = "enabled", nullable = false)
@NotNull
private Boolean enabled;
public AppUser(
String username,
String password,
boolean enabled,
boolean accountNonExpired,
boolean credentialsNonExpired,
boolean accountNonLocked,
Collection<? extends AppAuthority> authorities,
PersonalInformation personalInformation
) {
if (((username == null) || "".equals(username)) || (password == null)) {
throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
}
this.username = username;
this.password = password;
this.enabled = enabled;
this.accountNonExpired = accountNonExpired;
this.credentialsNonExpired = credentialsNonExpired;
this.accountNonLocked = accountNonLocked;
this.appAuthorities = Collections.unmodifiableSet(sortAuthorities(authorities));
this.personalInformation = personalInformation;
}
public AppUser() {
}
@JsonIgnore
public PersonalInformation getPersonalInformation() {
return personalInformation;
}
@JsonIgnore
public void setPersonalInformation(PersonalInformation personalInformation) {
this.personalInformation = personalInformation;
}
// Getters, setters 'n other stuff
और GrantedAuthorities के एक कार्यान्वयन के रूप में अधिकार इकाई:
@Entity
@Table(name = "authorities", uniqueConstraints = {@UniqueConstraint(columnNames = {"id"})})
public class AppAuthority implements GrantedAuthority, Serializable {
//~ Instance fields ================================================================================================
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "id", nullable = false)
private Integer id;
@Column(name = "username", nullable = false)
private String username;
@Column(name = "authority", nullable = false)
private String authority;
// Here comes the buggy attribute. It is supposed to repesent the
// association username<->username, but I just don't know how to
// implement it
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "appuser_fk")
private AppUser appUser;
//~ Constructors ===================================================================================================
public AppAuthority(String username, String authority) {
Assert.hasText(authority,
"A granted authority textual representation is required");
this.username = username;
this.authority = authority;
}
public AppAuthority() {
}
// Getters 'n setters 'n other stuff
मेरी समस्या @ManyToOne
assoc है। AppAuthorities
: इसे "उपयोगकर्ता नाम" माना जाता है, लेकिन कोशिश करने और ऐसा करने से त्रुटि उत्पन्न होती है, क्योंकि मुझे उस विशेषता को String
के रूप में टाइप करना होगा ... जबकि हाइबरनेट संबंधित इकाई की अपेक्षा करता है। तो मैंने जो कोशिश की वह वास्तव में सही इकाई प्रदान कर रहा है और @JoinColumn(name = "appuser_fk")
द्वारा एसोसिएशन बना रहा है। यह निश्चित रूप से, बकवास है, क्योंकि उपयोगकर्ता को लोड करने के लिए, मेरे पास username
में विदेशी कुंजी होगी, जबकि हाइबरनेट appuser_fk
में इसकी खोज करेगा, जो हमेशा खाली रहेगा।
तो मेरा प्रश्न यह है: डेटा मॉडल के सही जेपीए 2 कार्यान्वयन के लिए उपरोक्त मेटोन कोड को संशोधित करने के बारे में कोई सुझाव?
धन्यवाद
आह! यह तो बहुत अच्छी खबर है! जैसा कि आप सुझाव देते हैं, मैं ठीक से आगे बढ़ूंगा, धन्यवाद। –
क्या आप प्रोजेक्ट यूआरएल साझा कर सकते हैं? मैं पहले से ही oauth2 के साथ वसंत बूट सुरक्षा आराम एपीआई का उपयोग कर एक मॉड्यूल बनाने के लिए अपने 5 दिनों बर्बाद कर दिया। समस्या तब होती है जब मैं यूआरएल को "अधिकारियों" जैसे झूठे डेटा दे रहा हूं: शून्य, "खाता नॉन एक्स्पेरड": झूठी, "खाता नॉन लॉक": झूठी, "क्रेडेंशियल नॉन एक्स्पेरड": झूठी, "सक्षम": झूठी। – Harsh