首页 文章

如何通过调用spring引导应用程序属性来覆盖jar的application.properties

提问于
浏览
-1

我正在使用maven依赖项在另一个spring引导项目中使用spring boot项目(jar) . jar文件没有在application.properties中定义的属性,我想在jar文件中获取当前spring boot项目的属性 . 有没有办法覆盖jar的application.properties .

===============Microservice1=====================

@SpringBootApplication
@ComponentScan({"com.jwt.security.*"})
public class MicroserviceApplication1 {

    public static void main(String[] args) throws Exception {

        SpringApplication.run(MicroserviceApplication1.class, args);

    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

application.properties

jwt.auth.secret: secret
jwt.auth.token_prefix : Bearer
jwt.auth.header_string : Authorization

UserController

@RestController
@Slf4j
public class UserController
{
@RequestMapping(value="/jwt")
    public String tokens(){
        log.debug("User controller called : Token()");
        return "successful authentication";
    }
}

pom.xml

...
    <dependency>
        <groupId>com.jwt.security</groupId>
        <artifactId>securityUtils</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
...

现在,下面是jar中的配置 .

==========securityUtils============

@Slf4j
@Component
//@PropertySource("classpath:/application.properties")
//@ConfigurationProperties(prefix = "jwt")
class TokenAuthenticationService {

    @Value("${jwt.auth.secret}")
    private static String secret;

    @Value("${jwt.auth.header_string}")
    private static String headerString;

    @Value("${jwt.auth.token_prefix}")
    private static String tokenPrefix;

    static void getAuthentication(HttpServletRequest request) {
        String token = request.getHeader(headerString);
        Date referenceTime = new Date();
        if (token != null) {
            final Claims claims = Jwts.parser()
                    .setSigningKey(secret.getBytes())
                    .parseClaimsJws(token.replace(tokenPrefix, ""))
                    .getBody();

            if (claims == null) {
                throw new BadCredentialsException("You are not authoriozed");
            } else {
                Date expirationTime = claims.getExpiration();
                if (expirationTime == null || expirationTime.before(referenceTime)) {
                    log.debug("The token is expired");
                    throw new TokenExpiredException("The token is expired");

                }
            }

        } else {
            throw new BadCredentialsException("You are not authoriozed");
        }
    }
}

在TokenAuthenticationService中,我想获取从调用microservice1加载的属性

谢谢

1 回答

  • 0

    它可能取决于您在项目中导入和使用该jar的方式 . 在项目配置类中,您需要像这样导入JAR项目的配置类

    @SpringBootApplication
    @Import(ConfigClassJarProject.class)
    public class Application {
    }
    

    这将创建JAR项目的bean并将它们放在应用程序上下文中以供使用 . 在为(SecurityUtils)类创建bean时,它将在属性中查找配置 . 现在你需要做两件事,首先更新你的securityUtils类,然后在属性名称后添加冒号,将默认值设置为null .

    @Value("${jwt.auth.secret:}")
    private static String secret;
    
    @Value("${jwt.auth.header_string:}")
    private static String headerString;
    
    @Value("${jwt.auth.token_prefix:}")
    private static String tokenPrefix;
    

    其次,使用此JAR在项目的属性文件中设置这些配置

    希望这会有所帮助 .

相关问题