Spring boot with integrated tomcat + access log with user authentication

I am using spring boot with tomcat + spring built-in. My access log from tomcat is as follows

IP - - [14 / Feb / 2017: 08: 49: 50 +0200] "GET / page / 2 HTTP / 1.1" 200 2606

So how can I make the log file look like

IP - - [14 / Feb / 2017: 08: 49: 50 +0200] the username is "GET / page / 2 HTTP / 1.1" 200 2606

Each request must have the username from which it is made. For security authentication, I use spring security with database username and password information.

+6
source share
1 answer

You probably need to modify the access log template in the application properties like this:

server.tomcat.accesslog.pattern=%h %l %t %u "%r" %s %b 

where %u is the authenticated remote user (see example here ).


UPD Perhaps this is not enough, since the generic template already contains the %u parameter. In this case, I would recommend two additional steps:

1) Put the username in the query session parameter, for example:

request.getSession().addAttribute("username", user.getName());

2) Add the following parameter to the access log template: %{username}s

 server.tomcat.accesslog.pattern=%h %l %t %u %{username}s "%r" %s %b 

which should accept an attribute named username from HttpSession , as described here .

+4
source

Source: https://habr.com/ru/post/1014957/


All Articles