2017-02-14 85 views
6

我使用的是具有嵌入式tomcat + spring安全性的spring引導。 從Tomcat我的訪問日誌看起來這帶有驗證用戶的嵌入式tomcat +訪問日誌的春季啓動

IP - - [14 /月/ 2017年:08:49:50 +0200] 「GET /頁/ 2 HTTP/1.1」 200 2606

那麼,如何我可以使日誌文件看起來像

IP - - [14 /月/ 2017年:08:49:50 +0200] 用戶名 - 「GET /頁/ 2 HTTP/1.1」 200 2606

每個請求都必須有用戶名。對於我使用數據庫用戶名和密碼信息的spring security來進行安全認證。

+0

不錯的問題,btw – Andremoniy

回答

4

你可能需要在應用程序屬性來更改access log pattern到這樣的事情:

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

其中%u是已經被驗證遠程用戶(見例如here)。


UPD:可能這是不夠的,因爲共同模式已經包含%u參數。在這種情況下,我建議兩個額外的步驟:

1)把用戶的名字進入請求會話參數,是這樣的:

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

2)添加下面的參數在訪問日誌模式:%{username}s

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

它應該從屬性usernameHttpSession如其描述here

+1

完美!我認爲%u適用於Tomcat Basic Auth .. –