spring-security 使用Spring Cloud Config进行外部日志记录配置

ltskdhd1  于 2022-11-11  发布在  Spring
关注(0)|答案(1)|浏览(189)

我刚刚体验了Spring Cloud Config,在我的项目之外有一些配置文件。我按照说明设置了一个客户端和一个服务器(链接到一个Git),效果很好!
基本上我有不同的application.yml给我的每个配置文件,在这些文件中有一个服务器端口属性和一个指向日志配置文件的URL(每个配置文件一个log4j2.yml),它也在我的git仓库中。客户端向服务器请求与其配置文件匹配的应用程序.yml文件。2然后,服务器找到该文件并将端口和log4j 2配置文件返回给客户端。
我有我想要的,这是一个不同级别的日志取决于客户端的配置文件。
当我使用spring-security(使用默认用户名和简单密码)设置身份验证时,客户端恢复了端口,但当它尝试访问log4j 2配置文件时,服务器返回401错误,说明客户端未被授权访问此文件。
这可能是因为客户端不知道访问application.yml中的文件的凭据,并且我不知道是否可以在logging.config属性中插入凭据
我试过类似的方法,但效果不佳:

logging:
 config: http://user:password@localhost:8888/....../log4j2.yml

可能有一个替代方案,当URL是该文件时,告诉服务器忽略安全性,但如果有一天我必须进行身份验证才能访问它,我将无法做到这一点。
这是我的文件

胃肠道

application-dev.yml

server:
 port: 55556

logging:
 config: http://localhost:8888/ConfigExtClient/dev/master/log4j2.yml

客户

boostrap.yml

spring:
  application:
    name: ConfigExtClient
  profiles:
    active: dev
  cloud:
    config:
      uri: http://localhost:8888
      username: user
      password: foo

相依性(pom.xml)

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.1.RELEASE</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>1.1.0.M4</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-yaml</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

    </dependencies>

服务器

application.yml

server:
  port: 8888

spring:
  cloud:
    config:
      server:
        git:
          uri: URLtoGit

security:
  user:
    name: user
    password: foo

bootstrap.yml

spring:
  application:
    name: ConfigExtServer

相依性(pom.xml)

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.1.RELEASE</version>
</parent>

<dependencies>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
        <version>1.1.0.M4</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-yaml</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

</dependencies>

错误

Logging config file location 'http://localhost:8888/ConfigExtClient/dev/master/log4j2.yml' cannot be opened and will be ignored

我跟踪了该错误,它出现在 reinitializeLoggingSystemPropertySourceBootstrapConfiguration 类中:

try {
            ResourceUtils.getURL(logConfig).openStream().close();
            system.initialize(new LoggingInitializationContext(environment),
                    logConfig, logFile);
        }
        catch (Exception ex) {
            PropertySourceBootstrapConfiguration.logger
                    .warn("Logging config file location '" + logConfig
                            + "' cannot be opened and will be ignored");
        }

它进入catch,异常为:

Server returned HTTP response code: 401 for URL: http://localhost:8888/ConfigExtClient/dev/master/log4j2.yml

预先感谢您的帮助,
罗曼

8yoxcaq7

8yoxcaq71#

你可以在GitHub中设置你的配置客户端,比如this sample
它需要log4j2.component.propertiesbootstrap.yml配置...

引导程序.yml

logging:
  config: http://configServerAddress:8888/yourAppName/yourSpringProfile/gitBranch/log4j2.xml

log4j2.component.properties(日志4j2组件属性)

log4j.configurationFile=http://configServerAddress:8888/yourAppName/yourSpringProfile/gitBranch/log4j2.xml
log4j2.configurationUserName=guest
log4j2.configurationPassword=guest

相关问题