我想在客户端(没有jsps)有一个JS应用程序,它只与REST调用后端通信.我还想让用户能够使用FB,Twitter帐户登录.另外,我还想让用户注册自己的帐户.为此,我想在后端使用Spring-security和spring-social,在前面使用Javascript SDK从FB获取access_token,然后将其传递给后端.
问题是:如何创建一个REST控制器,使用spring-social和spring-security工具进行身份验证?
我通读了以下示例:
https://github.com/spring-projects/spring-social-samples
但为了这个目的,我无法真正找到如何使用ProviderSignInController或SpringSocialConfigurer.我想我不能在我的情况下使用SocialAuthenticationFilter,因为"/ auth/{providerid}"url不是我正在寻找的.但是,我猜这个ProviderSingInController似乎也没用.如果我错了,请纠正我.理想情况下,我希望受益于Spring Security框架的所有功能.
我将不胜感激任何建议.
最好的祝福
编辑
我想按照这里的流程:http://porterhead.blogspot.com/2013/01/writing-rest-services-in-java-part-4.html但使用Spring Social和Spring Security结合使用.
前端应用程序是用AngularJS编写的
第二次编辑
事实证明,您可以简单地利用所有Spring Social模块的优点.客户端唯一要做的就是在auth/facebook或任何链接上调用GET来触发整个0auth舞蹈,最终将返回身份验证结果.然后,您可以轻松控制流程(注册帐户或将一些相关信息返回给客户端,以便知道需要注册).所以SpringSocialConfigurer在这种情况下运行良好(除了它不支持范围设置的事实,但是,这可以手动更改,检查我的拉取请求@ github.com/spring-projects/spring-social/pull/ 141)
第3次编辑 - 2014年10月14日
根据要求,我将分享我如何使其成功.
鉴于我已按以下方式配置安全过滤器:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
...
@Override
public void configure(final HttpSecurity http) throws Exception {
http.formLogin()
...
.and().apply(getSpringSocialConfigurer());
}
private SpringSocialConfigurer getSpringSocialConfigurer() {
final SpringSocialConfigurer config = new SpringSocialConfigurer();
config.alwaysUsePostLoginUrl(true);
config.postLoginUrl("http://somehost.com:1000/myApp");
return config;
}
Run Code Online (Sandbox Code Playgroud)
一旦我的应用程序设置完毕,我唯一需要调用的是 带有GET请求的http://somehost.com:1000/myApp/auth/facebook.
我通过STOMP通过SockJS连接到我的Spring后端。一切正常,所有浏览器等的配置都很好。但是,我找不到发送初始消息的方法。该方案如下:
函数connect(){
var socket = new SockJS('http:// localhost:8080 / myEndpoint');
stompClient = Stomp.over(socket);
stompClient.connect({},function(frame){
setConnected(true);
console.log('Connected:'+ frame);
stompClient.subscribe('/ topic / notify',function(message){
showMessage(JSON.parse(message.body).content);
});
});
}
后端配置大致如下所示:
@组态
@EnableWebSocketMessageBroker
公共类WebSocketAppConfig扩展了AbstractWebSocketMessageBrokerConfigurer {
...
@Override
公共无效registerStompEndpoints(最终StompEndpointRegistry注册表){
Registry.addEndpoint(“ / myEndpoint”)。withSockJS();
}
通常,当模板已经自动连线时,我会以以下方式执行此操作,例如在REST控制器中:
@Autowired
私有SimpMessagingTemplate模板;
...
template.convertAndSend(主题,新消息(“成功!”));
如何在连接事件上实现这一目标?
更新
我设法使它起作用。但是,我对配置还是有些困惑。我将在此处显示2种配置如何发送初始消息:
1)第一个解决方案
JS部分
stompClient.subscribe('/app/pending', function(message){
showMessage(JSON.parse(message.body).content);
});
stompClient.subscribe('/topic/incoming', function(message){
showMessage(JSON.parse(message.body).content);
});
Run Code Online (Sandbox Code Playgroud)
Java部分
@Controller
public class WebSocketBusController {
@SubscribeMapping("/pending")
Run Code Online (Sandbox Code Playgroud)
组态
@Override
public void configureMessageBroker(final MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
Run Code Online (Sandbox Code Playgroud)
...和其他电话
template.convertAndSend("/topic/incoming", outgoingMessage);
Run Code Online (Sandbox Code Playgroud)
2)第二种解决方案
JS部分 …