package hello;

import javax.servlet.ServletRequest;

import org.audit4j.core.MetaData;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class AuditMetaData implements MetaData {

	   private static final long serialVersionUID = 7243065407615627372L;

	    @Override
	    public String getOrigin() {
	        try {
	            return ((ServletRequest)((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest()).getRemoteAddr();
	        }catch(Exception e){
	            e.printStackTrace();
	        }
	        return "unidentified";
	    }

	    @Override
	    public String getActor() {
	        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
	        if (authentication != null && authentication.getPrincipal() instanceof UserDetails) {
	            return ((UserDetails) authentication.getPrincipal()).getUsername();
	        }
	        return "anonymous";
	    }


}

@@ -24,34 +24,131 @@
 java
-x.servlet.ServletRequest;%0A
+.util.ArrayList;%0Aimport java.util.List;%0A%0Aimport javax.servlet.ServletRequest;%0A%0Aimport org.apache.commons.lang3.StringUtils;
 %0Aimp
@@ -235,16 +235,75 @@
 cation;%0A
+import org.springframework.security.core.GrantedAuthority;%0A
 import o
@@ -1338,16 +1338,32 @@
 ername()
++ %22 %22+getRoles()
 ;%0A%09     
@@ -1403,12 +1403,501 @@
 %0A%09    %7D%0A
+%09    %0A%09    //how to have a string giving the list of roles that the user has%0A%09    private String getRoles() %7B%0A%09    %09%0A        %09List%3CString%3E roles = new ArrayList%3CString%3E();%0A        %09for( GrantedAuthority author :SecurityContextHolder.getContext().getAuthentication().getAuthorities()) %7B%0A        %09%09roles.add(author.toString());%0A        %09%7D%0A        %09if(roles.isEmpty()) %7B%0A        %09%09return %22no role%22;%0A        %09%7D else%0A        %09%09return  %22roles=%5B%22+StringUtils.join(roles, %22,%22)+ %22%5D%22;%0A%09    %09%0A%09    %7D%0A
 %0A%0A%7D%0A

complete springboot example with giving the list of roles