Tomcat如何挑选经过身份验证的远程用户

时间:2017-11-28 15:58:47

标签: tomcat spring-boot logback

如果我通过logback启用tomcat访问日志,如下所示

logback.access:
  enabled: true

compile group: 'net.rakugakibox.spring.boot', name: 'logback-access-spring-boot-starter', version: '2.5.0'

配置如下

<configuration>
    <property name="LOG_HOME" value="${LOG_FILE_LOCATION:-/var/log/RSP}/${HOST_NAME}}"/>
    <appender name="ACCESS" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_HOME}/reservation_access.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/reservation_access-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>5MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder>
            <pattern>%h %l %u [%{TraceId}i] %t "%r" %s %b %D</pattern>
        </encoder>
    </appender>
    <appender-ref ref="ACCESS" />

</configuration>

它运行正常,但我无法理解它是如何选择%u的,documentation它被称为Remote user that was authenticated所以tomcat如何知道谁被认证用户或任何人都可以提供如何生成这些访问日志的源代码。(我已经阅读了基本文章what is Access Log Valve

如果我使用弹出启动属性启用访问日志,如下所示

server.tomcat.accesslog.pattern: '%h %l %u [%{TraceId}i] %t "%r" %s %b %D'
server.tomcat.accesslog.prefix: reservation_access
server.tomcat.accesslog.enabled: true
server.tomcat.accesslog.directory: /var/log/RSP/
server.tomcat.accesslog.suffix: .log

然后%u无效,

  • 那么这两个版本有什么区别?
  • 当为每个请求打印访问日志时,即在执行所有过滤器之后,或者一旦请求到达服务器,它将被打印?

    感谢

1 个答案:

答案 0 :(得分:0)

logback-access-spring-boot-starter%u读取HttpServletRequest#getRemoteUser()的值。

在Spring世界中,HttpServletRequest.remoteUser()通常由Spring Security填充。