I want to log the username from which every request was made. After successfully logged in, I use this method to set the username to session variable - username.
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
String username = merchandiserRepository.findByUsername(authentication.getName()).getUsername();
request.getSession(true).setAttribute("username", username);
logger.warn("Successfully logged in: " + username);
response.sendRedirect("/page/1");
}
And in my Tomcat log pattern is:
server.tomcat.accesslog.pattern=%h %l %{username}s %t "%r" %s %b %T %{User-Agent}i
Where %{username}s should be the username, but it displays " - " I saw this attribute from - https://tomcat.apache.org/tomcat-8.5-doc/config/valve.html
I'm using redis for session persistent. Any suggestions why it is not working?
I think I understand what you are trying to accomplish. I accomplished the same thing, a slightly different way.
Try adding this dependency:
<dependency>
<groupId>net.rakugakibox.spring.boot</groupId>
<artifactId>logback-access-spring-boot-starter</artifactId>
<version>2.5.0</version>
</dependency>
And here is my logback-access.xml which I put into my src/main/resources/
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration debug="false" scan="false">
<statusListener class="ch.qos.logback.core.status.NopStatusListener"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashAccessEncoder">
<fieldNames>
<message>message</message>
</fieldNames>
</encoder>
</appender>
<appender-ref ref="CONSOLE"/>
</configuration>
I don't know the root cause of your issue, but this might be a solution to what you are trying to accomplish.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.