Spring data MongoDB @CreatedBy @lastmodifiedby@createdby@lastmodifiedby springsecurityauditoraware, log only user names

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.