简体   繁体   English

将Facebook身份验证添加到我的Spring MVC应用程序中

[英]Adding Facebook authentication to my Spring MVC application

I'm trying to configure OAuth2 authentication into an existing Spring MVC app that is currently configured to use simple FORM authentication. 我正在尝试将OAuth2身份验证配置到当前配置为使用简单FORM身份验证的现有Spring MVC应用程序中。 I'd like to use Facebook as authentication provider, but I also need to maintain form authentication and to register user data in application DB the same way for both authentication methods (obviously for social auth some fields will be absent, for ex. password ). 我想使用Facebook作为身份验证提供程序,但我还需要维护表单身份验证并以相同的方式在应用程序数据库中注册用户数据两种身份验证方法(显然对于社交身份验证,某些字段将不存在,例如password ) 。

I'm using Spring Boot 1.4.1 with these additional dependencies: 我正在使用Spring Boot 1.4.1以及这些额外的依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.security.oauth</groupId>
    <artifactId>spring-security-oauth2</artifactId>
</dependency>

But I cannot find a tutorial or some clear documentation explaining how to solve my case (FORM + OAuth2). 但我找不到教程或一些明确的文档来解释如何解决我的案例(FORM + OAuth2)。

I tried to follow this tutorial but it is based on Spring Boot + AngularJS, while I'm using plain HTML + Thymeleaf. 我尝试按照本教程,但它基于Spring Boot + AngularJS,而我使用纯HTML + Thymeleaf。

This is my current Spring Security configuration: 这是我目前的Spring Security配置:

http.authorizeRequests() //
     .antMatchers("/css/**").permitAll() // static resources
     .antMatchers("/webjars/**").permitAll() // static resources
     .antMatchers("/images/**").permitAll() // static resources
     .antMatchers("/login").permitAll().anyRequest().authenticated();
http.formLogin()
     .failureUrl("/login?error").defaultSuccessUrl("/").loginPage("/login").permitAll()//
     .and().logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
     .logoutSuccessUrl("/login?logout").permitAll();

What should I add to configure OAuth2 and to get user info once authenticated? 我应该添加什么来配置OAuth2并在验证后获取用户信息?


UPDATE After some googlin' I found that the way to go could be to use this dependency: 更新在一些googlin之后'我发现要走的路可能是使用这种依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-social-facebook</artifactId>
</dependency>

in place of plain spring-security-oauth2 , but I still can't find a simple way to achieve what I want. 取代普通的spring-security-oauth2 ,但我仍然找不到一种简单的方法来实现我想要的。

The best tutorial I found is this , but is a bit dated and is not based on Spring Boot, so I can't understand if some utility and autoconfiguration exist. 我找到的最好的教程是这个 ,但是有点过时并且不是基于Spring Boot,所以我无法理解是否存在一些实用程序和自动配置。

I tried to use this tutorial for other social network, but i got that you application as client has to provide next config in application.yml : 我尝试将此教程用于其他社交网络,但我得到了您的应用程序,因为客户端必须在application.yml中提供下一个配置:

facebook:
  client:
    clientId: 233668646673605
    clientSecret: 33b17e044ee6a4fa383f46ec6e28ea1d
    accessTokenUri: https://graph.facebook.com/oauth/access_token
    userAuthorizationUri: https://www.facebook.com/dialog/oauth
    tokenName: oauth_token
    authenticationScheme: query
    clientAuthenticationScheme: form
  resource:
    userInfoUri: https://graph.facebook.com/me

this info will be used during outh2 process to get authorization token 此信息将在outh2过程中用于获取授权令牌

I resolve my problem with configuration simple OAuth2 with Spring through my Authorization Server, you can check my answer here if you haven't found how to do it 我通过我的授权服务器使用Spring配置简单的OAuth2来解决我的问题,如果你还没找到怎么做,可以在这里查看我的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Spring MVC应用程序中使用facebook登录? - Login with facebook in Spring MVC application? 在我的(java spring mvc + mysql应用程序,thymeleaf)中实现spring security后,身份验证发生了一些奇怪的事情 - After implementing spring security in my (java spring mvc + mysql application, thymeleaf) something weird happens with the authentication 将Spring MVC添加到Spring Java“桌面”应用程序 - Adding spring mvc to a spring java “desktop” application 来自Java(Jetty下的Spring MVC)应用程序的ADFS身份验证和模拟 - ADFS authentication and impersonation from a Java (Spring MVC under Jetty) application 将Spring Boot添加到我的Spring MVC Hibernate应用程序中 - Add Spring boot into my spring mvc hibernate application 将Web界面(Spring MVC)添加到现有Java应用程序 - Adding a web interface (Spring MVC) to existing Java application 添加Spring Security后,我的spring应用程序停止运行 - My spring application stopped running after adding Spring Security 如何在我的Spring应用程序中添加SASL身份验证 - How I can add SASL Authentication in my Spring Application HTTP 400-向我的Spring MVC控制器添加参数时的状态 - HTTP 400 - status when adding argument to my Spring MVC controller 在我的 Spring MVC 应用程序中实现 Spring Actuator 而不添加 Spring Boot - Implement Spring Actuator in my Spring MVC app without adding Spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM