我有一个基于Spring的模块,需要与现有的/遗留的Java“独立”应用程序集成。基于spring的模块是AuthenticationProvider (spring-security)的一个简单实现。
我想要做的是在Java应用程序中连接基于spring的模块,我可以简单地从Java代码中调用该提供者上的身份验证方法。
这有可能吗?什么是必需的?
是否可以将spring模块封装在一个普通的Java库中,并将其用作独立Java应用程序的API接口?
我已经寻找了特定的教程,但似乎没有一个适合这个要求。
发布于 2015-10-02 09:07:51
好的,我必须直接使用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.");
}
}https://stackoverflow.com/questions/32865448
复制相似问题