[英]Integrate Spring-based java module into a standalone java application
I have a Spring-based module that I need to integrate with an existing/legacy Java "standalone" app. 我有一个基于Spring的模块,需要与现有/旧版Java“独立”应用程序集成。 The Spring-based module is a simple implementation of an AuthenticationProvider (spring-security).
基于Spring的模块是AuthenticationProvider的简单实现(spring-security)。
What I would like to do is hook up the spring based module in a Java application in a way that I can simply call the authenticate method on that provider from the Java code. 我想做的就是以一种可以通过Java代码简单地在该提供程序上调用authenticate方法的方式来连接Java应用程序中基于spring的模块。
Is that possible? 那可能吗? What is required?
需要什么?
Is it possible to wrap the spring module in a plain Java library and use that as API interface for my standalone Java app? 是否可以将spring模块包装在纯Java库中并将其用作独立Java应用程序的API接口?
I already searched for specific tutorials but it seems there isn't one that fit this requirement. 我已经在搜索特定的教程,但似乎没有一个适合这一要求。
OK, I have to use the ApplicationContext directly so I can manage the Spring framework in my application. 好的,我必须直接使用ApplicationContext ,以便可以在应用程序中管理Spring框架。
public class MyApplication {
private static final Logger LOGGER = LoggerFactory.getLogger(MyApplication.class);
public static void main(String[] args) {
MyAuthenticationProvider authProvider;
try (ConfigurableApplicationContext springApplicationContext = new AnnotationConfigApplicationContext(
MySpringConfiguration.class)) {
springApplicationContext.registerShutdownHook();
authProvider = springApplicationContext.getBean(MyAuthenticationProvider.class);
}
Authentication request = new UsernamePasswordAuthenticationToken("foo", "foo");
Authentication result = authProvider.authenticate(request);
if (result.isAuthenticated()) {
LOGGER.debug("User is authenticated");
} else {
LOGGER.debug("Cannot authenticate user.");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.