To use @[email Protected]@[email protected] in spring data mongodb four annotations
Must implement Springsecurityauditoraware
Official code
class Springsecurityauditoraware implements auditoraware<user> { public User Getcurrentauditor () { = securitycontextholder.getcontext (). Getauthentication (); if Null | | ! authentication.isauthenticated ()) { returnnull; } return ( (Myuserdetails) Authentication.getprincipal ()). GetUser (); }}
To add a configuration file XML
<mapping-context-ref= "Custommappingcontext" auditor-aware-ref= " Yourauditorawareimpl "/>
Springboot Configuration method
@Configuration @enablemongoauditing class Config { @Bean public auditoraware<auditableuser> myauditorprovider () { return New Auditorawareimpl (); }}
Using annotations
@CreatedDate private LocalDateTime createdate; @CreatedBy private User createdby; @LastModifiedBy private User Lastmodifiedby; @LastModifiedDate private LocalDateTime lastmodifieddate;
So, you need to add a method in your user entity
public User GetUser () { return new User (this .getusername (), this .getpassword (), this .isenabled (), this .isaccountn Onexpired (), this .iscredentialsnone Xpired (), this .isaccountnonlocked () , this .getauthorities ()); }
Data is generated when springdata insert or save, and you will find that
"CreateDate": Isodate ("2017-10-25t07:06:09.730z"), "CreatedBy" : { "Password": "$2A$10$LCEZ8.WHHRSDRBI6NNITJE4OIH/XNHJKUSBFKZLNMYUHTKY683QXM", "username": "athos7817", "Authorities" : [ { "Role": "Auth_order_update", "_class": "Org.springframework.security.core.authority.SimpleGrantedAuthority" }, { "Role": "Auth_order_add", "_class": "Org.springframework.security.core.authority.SimpleGrantedAuthority" }, //The following omit 10,000 permissions ], "Accountnonexpired":true, "Accountnonlocked":true, "Credentialsnonexpired":true, "Enabled":true}, "Lastmodifiedby" : { "Password": "$2A$10$LCEZ8.WHHRSDRBI6NNITJE4OIH/XNHJKUSBFKZLNMYUHTKY683QXM", "username": "athos7817", "Authorities" : [ { "Role": "Auth_order_update", "_class": "Org.springframework.security.core.authority.SimpleGrantedAuthority" }, { "Role": "Auth_order_add", "_class": "Org.springframework.security.core.authority.SimpleGrantedAuthority" }, //The following omit 10,000 permissions ], "Accountnonexpired":true, "Accountnonlocked":true, "Credentialsnonexpired":true, "Enabled":true},
Who needs so much scrap data, and springsecurity the constructor of user, not allowed to pass in null
Public User (string Username, string password, collection<? extends grantedauthority>authorities) { This(Username, password,true,true,true,true, authorities); The public User (string Username, string password,BooleanEnabledBooleanAccountnonexpired,BooleanCredentialsnonexpired,BooleanAccountnonlocked, collection<? Extends grantedauthority>authorities) { if(Username! =NULL&& "". Equals (username) && Password! =NULL) { This. Username =username; This. Password =password; This. Enabled =enabled; This. accountnonexpired =accountnonexpired; This. credentialsnonexpired =credentialsnonexpired; This. accountnonlocked =accountnonlocked; This. Authorities =Collections.unmodifiableset (Sortauthorities (authorities)); } Else { Throw NewIllegalArgumentException ("Cannot pass null or empty values to constructor"); } }
Make changes to User modified to String
@CreatedDate private LocalDateTime createdate; @CreatedBy private String createdby; @LastModifiedBy private String lastmodifiedby; @LastModifiedDate private LocalDateTime lastmodifieddate;
@Bean public auditoraware<String> auditorprovider () { return New Springsecurityauditoraware (); }
/**/Publicclass Springsecurityauditoraware implements Auditoraware<string > {public String getcurrentauditor () { = securitycontextholder.getcontext (). Getauthentication (); if Null | | ! authentication.isauthenticated ()) { returnnull; } return ( (MyUser) Authentication.getprincipal ()). GetUserName (); }}
Results
"CreateDate": Isodate ("2017-10-25t07:35:46.636z"), "createdby": "Laizhenwei", " Lastmodifiedby ":" Laizhenwei ", " lastmodifieddate ": Isodate (" 2017-10-25t07:35:46.636z ")
Spring Data mongodb @[email protected]@[email protected] springsecurityauditoraware, log only user name