配置文件
大约 1 分钟languagejavaspring
开启允许循环依赖
旭纸业项目中仅设置下面配置文件无效,还需要在 meService 上使用注解 Lazy
spring:
main:
allow-circular-references: true
路径符号
windows的路径符号正好是转义字符,需要用两个。路径中有空格则必须使用双引号括起来
openvpn:
exe: "C:\\Program Files\\OpenVPN\\bin\\openvpn.exe"
servers: "C:\\Program Files\\OpenVPN\\config\\xdf.ovpn#10.8.0.1"
配置文件实体类
制作下面实体类
@Data
@ConfigurationProperties(prefix = "hm.jwt")
public class JwtProperties {
private Resource location;
private String password;
private String alias;
private Duration tokenTTL = Duration.ofMinutes(10);
}
对应配置文件如下,hm 是根节点,表示将 hm.jwt 下的4个属性自动装配到实体类 JwtProperties 的属性中
hm:
jwt:
location: classpath:hmall.jks
alias: hmall
password: hmall123
tokenTTL: 30m
auth:
excludePaths:
- /search/**
- /users/login
- /items/**
- /hi
实体类使用注解 ConfigurationProperties 只是指定了读取配置文件后自动装配,并没有启用自动读取配置文件,还需要在配置类中启用,如下面通过注解 Configuration 指定类 SecurityConfig 是配置类,同时使用注解 EnableConfigurationProperties 启用自动读取配置
package com.hmall.user.config;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.rsa.crypto.KeyStoreKeyFactory;
import java.security.KeyPair;
@Configuration
@EnableConfigurationProperties(JwtProperties.class)
public class SecurityConfig {
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Bean
public KeyPair keyPair(JwtProperties properties){
// 获取秘钥工厂
KeyStoreKeyFactory keyStoreKeyFactory =
new KeyStoreKeyFactory(
properties.getLocation(),
properties.getPassword().toCharArray());
//读取钥匙对
return keyStoreKeyFactory.getKeyPair(
properties.getAlias(),
properties.getPassword().toCharArray());
}
}
