我有一个基于spring的应用程序,由spring-security保护。服务是通过HttpInvoker公开的,我想从一个基于Swing的客户端使用这些服务。
我知道如何通过HttpInvokerProxyFactoryBean在Swing客户端中使用这些远程服务。但是我找不到一个好的例子来处理我的基于swing的客户端的身份验证。你能给我指引正确的方向吗?
发布于 2017-07-12 10:32:02
我设法完成了它,并在这里发布了答案,以帮助任何有同样问题的人。
在您的swing客户端中,按如下方式设置HttpInvokerProxyFactoryBean。
@Bean
public HttpInvokerProxyFactoryBean invoker(AuthenticationSimpleHttpInvokerRequestExecutor httpInvokerRequestExecutor) {
HttpInvokerProxyFactoryBean invoker = new HttpInvokerProxyFactoryBean();
invoker.setServiceUrl(<URL_HERE>);
invoker.setServiceInterface(DeliveryService.class);
invoker.setHttpInvokerRequestExecutor(httpInvokerRequestExecutor);
return invoker;
}您的客户端中还需要有一个如下所示的。
@Bean
public AuthenticationSimpleHttpInvokerRequestExecutor httpInvokerRequestExecutor() {
return new AuthenticationSimpleHttpInvokerRequestExecutor();
}然后,你必须在你的本地SecurityContext中设置你的用户名和密码,如下所示。
ConfigurableApplicationContext context = SpringApplication.run(Application.class);
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(<Username>, <Password>));都设置好了,现在您可以检索下面的服务bean了。
<ServiceInterface> service = context.getBean(<ServiceInterface>.class);您还必须在服务器的安全配置中启用HTTP基本身份验证。否则,您将得到StreamCorruptedException。
https://stackoverflow.com/questions/45046604
复制相似问题