`

Facebook Connect Integration With Spring Security 3.x

阅读更多

Facebook Connect Integration With Spring Security 3.x

facebook_spring It’s so popular that nowadays almost all web applications on the internet use Facebook Connect to attract their potential users to move their pre-built social networking structure to on their own systems. It’s no exception that our new social networking based project at work wants to adopt this feature so since we use Spring 3.x as the main framework of our project, as team we’ve spend some time to inject the Facebook Connect structure into the Spring Security (F.K.A Acegi Security). As a nice side effect, i decided to extract this integration information from our in house project to contribute and rearranged the structure to share with you in a more easy and convenient way.

While sharing this experiment with Facebook Connect with you, i assume that you already know or ready to investigate the Facebook Connect API and Spring Security. You should find a significant amount of information about Spring Security from it’s official documentation http://static.springsource.org/spring-security/site/ and Facebook Connect again its own documentation wiki http://wiki.developers.facebook.com/index.php/Facebook_Connect

So as you know the Spring’s solution for securing web applications is an implementation of core security api across the stack of a few servlet filters. Due to stateless nature of http protocol spring keeps the information of authentication and authorization requests with help of browser session cookies and make some magic behind to keep your resources secure. You should refer to the picture given to take a look at Spring Security from a bird’s eye view with the help of a UML sequence diagram published here https://twiki.auscope.org/twiki/pub/Grid/AuScopePortalSecurity/PortalAuthorisation_details.jpg . Another resource for understanding spring security is the section 5.4 in the official technical documentation http://static.springsource.org/spring-security/site/docs/3.0.x/reference/technical-overview.html#tech-intro-web-authentication .

OK, you say shut up. So let’s do some stuff.

The integration code can be obtained from http://code.google.com/p/spring-security-facebook . You should checkout code with svn scm like shown below.

~$ svn checkout http://spring-security-facebook.googlecode.com/svn/trunk/ spring-security-facebook-read-only

This is a maven project so later you enter the project directory you could type

~$PROJECT_HOME$ mvn install

After build operation completes, you should use the library in your spring project by declaring dependency in your own project’s pom file. It’s something like:

< dependency >
     < groupId >org.springframework.security</ groupId >
     < artifactId >spring-security-facebook</ artifactId >
     < version >1.0.0-ALPHA</ version >
</ dependency >

And also you must also have facebook-java-api dependency in your pom file. You could find more information about this api from here http://code.google.com/p/facebook-java-api . It’s used in our library to talk with Facebook Connect Api.

< dependency >
     < groupId >com.google.code.facebookapi</ groupId >
     < artifactId >;facebook-java-api</ artifactId >
     < version >2.1.1</ version >
</ dependency >

Now you’re ready to go with real stuff. Here instead of trying to tell how to use this library, i prepared a sample web application. It’s the best way IMHO that you spring users could understand how library integrates Facebook Connect Api well with the Spring Security, because it will take too much time to visit all details while showing you how to accomplish this. But for you as spring users, i can provide the minimal Spring application context file here to show that how it’s clean to configure the integration.

<? xml version = "1.0" encoding = "UTF-8" ?>
< beans xmlns = "http://www.springframework.org/schema/beans"
     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:context = "http://www.springframework.org/schema/context"
     xmlns:security = "http://www.springframework.org/schema/security"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">
 
     < security:http entry-point-ref = "authenticaionEntryPoint" >
         < security:intercept-url pattern = "/static/login*/**" access = "IS_AUTHENTICATED_ANONYMOUSLY" />
         < security:intercept-url pattern = "/static/secure*/**" access = "ROLE_FACEBOOK_USER" />
         < security:logout logout-success-url = "/static/index.html" />
         < security:custom-filter before = "FORM_LOGIN_FILTER" ref = "facebookAuthenticationFilter" />
     </ security:http >
 
     < bean id = "authenticaionEntryPoint" class = "org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint" >
         < property name = "loginFormUrl" value = "/static/login.html" />
     </ bean >
 
     < bean id = "facebookAuthenticationFilter" class = "org.springframework.security.facebook.FacebookAuthenticationFilter" >
         < property name = "authenticationManager" ref = "authenticationManager" />
         < property name = "authenticationSuccessHandler" >
             < bean class = "org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler" >
                 < property name = "defaultTargetUrl" value = "/static/secure.html" />
                 < property name = "alwaysUseDefaultTargetUrl" value = "true" />
             </ bean >
         </ property >
         < property name = "authenticationFailureHandler" >
             < bean class = "org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler" >
                 < property name = "defaultFailureUrl" value = "/static/login.html" />
             </ bean >
         </ property >
     </ bean >
 
     < bean id = "authenticationProvider" class = "org.springframework.security.facebook.FacebookAuthenticationProvider" >
         < property name = "roles" value = "ROLE_FACEBOOK_USER" />
     </ bean >
 
     < bean id = "facebookHelper" class = "org.springframework.security.facebook.FacebookHelper" >
         < property name = "apiKey" value = "YOUR_API_KEY" />
         < property name = "secret" value = "YOUR_SECRET" />
     </ bean >
 
     < security:authentication-manager alias = "authenticationManager" >
         < security:authentication-provider ref = "authenticationProvider" />
     </ security:authentication-manager >
 
</ beans >

You can download the sample web project here: http://code.google.com/p/spring-security-facebook/downloads/detail?name=spring-security-facebook-web.zip

You can browse project details here: http://code.google.com/p/spring-security-facebook/

This is a hot new blog entry in the late night. But if anything broken, i’ ll try to fix it ASAP.

Well also if you intend to contribute to code, you’re welcome. Please contact with me.

Hope this helps.

分享到:
评论

相关推荐

    最新的Spring Security4.x开发所需要的jar包

    此外,Spring Security还支持OAuth2、OpenID Connect和其他现代身份验证协议,这使得它能够与其他身份验证服务集成,如Google、Facebook等。在4.x版本中,这些功能得到了进一步增强,以提高安全性和互操作性。 总的...

    Spring Security笔记.rar

    2. **OAuth2集成**:Spring Security支持OAuth2协议,可以用于实现第三方登录(如Facebook、Google)。 3. **CSRF防护**:Spring Security提供跨站请求伪造(CSRF)防护机制,防止恶意攻击。 总的来说,Spring ...

    SpringSecurity:Spring Security 5x

    在Spring Security 5.x版本中,它提供了许多改进和新特性,使得开发者能够更安全地构建现代应用程序。以下是关于Spring Security 5.x的一些关键知识点: 1. **依赖注入与Spring Boot集成**: - Spring Security 5....

    spring-security-oauth-1.0.2.RELEASE.zip

    3. **Spring Security**:这是一个强大的安全框架,提供认证、授权等功能,用于保护Java应用的安全。 4. **Spring Security OAuth**:它是Spring Security的扩展,提供了OAuth 1.0a和2.0的支持,帮助开发者轻松实现...

    spring-security-3.1.0.RC2 官方下载

    3. **表达式语言支持**:Spring Security 3.x引入了基于Spring Expression Language (SpEL)的访问控制表达式,允许开发者使用条件语句来定义谁可以访问哪些资源。 4. **Remember Me服务**:此版本可能包含了...

    Spring Security-3中文官方文档(及教程)

    7. **OAuth2整合**:Spring Security可以与OAuth2框架集成,支持第三方身份验证服务,如Google、Facebook等,实现社交登录功能。 8. **Web安全**:文档涵盖了HTTP基本认证、表单登录、HTTP方法转换、XSS防护、点击...

    facebook-android-sdk-4.18.0.zip

    Facebook Android SDK 4.18.0 是一个用于在Android应用程序中集成Facebook功能的开发工具包。这个SDK允许开发者轻松地实现用户登录、分享、广告、分析和其他Facebook服务。2017年发布的这个版本是当时最新的,为...

    spring security 4.0.0 所有jar包

    6. **MVC Integration**:Spring Security与Spring MVC紧密集成,可以轻松地在Spring MVC应用中添加安全控制。 7. **JPA and JDBC Realm Support**:Spring Security支持使用JDBC和JPA存储用户信息和权限,可以方便...

    springsecurity入门实例

    3. **过滤器链**:Spring Security 使用一系列过滤器来拦截请求并执行安全逻辑,如 `AuthenticationFilter` 和 `AuthorizationFilter`。 **二、设置项目** 1. **添加依赖**:在 Maven 或 Gradle 项目中添加 Spring...

    springcloud security。微服务安全-springcloud-security.zip

    7. **OpenID Connect集成**:对于需要第三方认证的服务,SpringCloud Security支持与OpenID Connect标准的提供商集成,如Google、Facebook等。 在"springcloud-security-master"目录中,你可能会找到以下文件和目录...

    spring-boot-oauth2-登录

    spring.security.oauth2.client.registration.github.client-id= spring.security.oauth2.client.registration.github.client-secret= 自定义oauth2登录 spring.security.oauth2.client.registration.kakao.client-...

    Spring.Cookbook.1783985801.epub

    You'll then add a database, forms, and user authentication, and learn how to handle mobile devices and integrate with Facebook and Twitter. Next, you will be introduced to unit testing, web services,...

    Spring-Security2.0 和 3.0中文文档

    Spring Security 3.0 开始引入OAuth支持,允许与其他OAuth提供者进行集成,实现了社交登录等功能,比如通过Google、Facebook账户登录。 6. **AOP(面向切面编程)安全**: 2.0 和 3.0 都支持AOP,可以对方法级别...

    spring security oauth2

    Spring Security OAuth2 是一个强大的框架,用于为基于Spring的应用程序提供认证和授权功能。这个框架是Spring Security的扩展,专门设计来处理OAuth2协议,它允许开发者安全地开放应用程序的API,同时保护用户数据...

    Spring-Security-demo.zip

    9. **OAuth2 集成**:如果 demo 包含了 OAuth2,那么我们可以学习如何将 Spring Security 与 OAuth2 服务提供商(如 Google 或 Facebook)集成,实现第三方登录功能。 10. **单元测试**:一个好的示例项目通常会...

    spring security3

    8. **OAuth集成**:虽然Spring Security 3.1.0.RC3版本相对较旧,但仍然支持OAuth协议,可以与其他OAuth服务提供商(如Google、Facebook等)进行身份验证集成。 9. **XML配置与Java配置**:在3.x版本中,Spring ...

    SpringSecurity3框架

    - Spring Security 3支持OAuth2协议,可以用于实现第三方登录功能,如Google、Facebook登录。 8. **AOP集成** - Spring Security利用Spring的AOP(面向切面编程)实现方法级别的安全控制,可以对方法调用进行访问...

    spring-security-demo.zip

    - Spring Security也支持OAuth2,可以用来实现社交登录(如Facebook、Google)或其他服务的API访问。 9. **Web Hooks安全**: - 对于Web Hook回调的安全保护,可以设置特定的访问规则,确保只有预期的来源才能...

    SPRING SECURITY配置

    Spring Security还支持OAuth2,可以与其他服务进行安全交互,如Google、Facebook登录。 ### 8. 实战案例 - **防止CSRF攻击**:Spring Security默认开启CSRF保护,可通过配置禁用或调整策略。 - **RESTful API安全*...

Global site tag (gtag.js) - Google Analytics