2016-01-14 10 views
5

私は春3と春のセキュリティを使用しています。私はFacebook、Twitter、Googleなどのソーシャルアカウントを統合しています。私はそこにjavascript sdkのバージョンを使用していますが、私の問題は私がユーザーを登録することができますが、私はそれらを認証する方法がわかりません。例えば春のセキュリティへのソーシャルメディアログインの実装

:ユーザーが(FacebookやTwitter、Googleの)リンクのいずれかをクリックすると、正常に認証した後、新しいダイアログボックスが開かれ

私は彼らの基本的なプロフィールの詳細を取得することができます:電子メール、ID、名前、画像や私はこの情報を私のコントローラに渡しました。このサービスにはajaxを使い、ユーザがまだ登録されていなければユーザを保存するようにしました。

ここにすべては私のために正常に動作しているティル。私はユーザーIDを使用し、塩を使用してそれらを暗号化し、データベースにパスワードとして保存します(これは正しい方法であるかどうかわかりませんが)。今は私の混乱がどのようにユーザーを認証し、システムにログインします。

私のsecurity.xmlファイル

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns="http://www.springframework.org/schema/security" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
         http://www.springframework.org/schema/security 
         http://www.springframework.org/schema/security/spring-security-3.2.xsd"> 

    <!-- Configuration for master level user login --> 
    <http auto-config="true" use-expressions="true" 
     disable-url-rewriting="true"> 
     <!-- <csrf /> --> 
     <headers> 
      <cache-control /> 
      <content-type-options /> 
      <hsts /> 
      <frame-options /> 
      <xss-protection /> 
     </headers> 
     <!-- requires-channel="https" --> 
     <intercept-url pattern="/favicon.ico" access="permitAll" /> 
     <intercept-url pattern="/login*" access="permitAll" /> 
     <intercept-url pattern="/login/facebook-login*" access="permitAll" /> 
     <intercept-url pattern="/validateUserCredentials*" 
      access="permitAll" /> 
     <intercept-url pattern="/register*" access="permitAll" /> 
     <intercept-url pattern="/activation*" access="permitAll" /> 
     <intercept-url pattern="/restore*" access="permitAll" /> 
     <intercept-url pattern="/resend*" access="permitAll" /> 
     <intercept-url pattern="/resources/**" access="permitAll" /> 
     <intercept-url pattern="/license*" 
      access="hasAnyRole('${role.admin}', '${role.master}')" /> 
     <intercept-url pattern="/**" 
      access="hasAnyRole('${role.admin}', '${role.master}', '${role.owner}', '${role.simple}')" /> 
     <access-denied-handler error-page="/denied" /> 
     <form-login login-page="/login" default-target-url="/logged" 
      authentication-failure-url="/loginfailed" login-processing-url="/j_spring_security_check" /> 
     <logout logout-success-url="/login" invalidate-session="true" 
      delete-cookies="JSESSIONID,SPRING_SECURITY_REMEMBER_ME_COOKIE" /> 
     <session-management session-fixation-protection="migrateSession"> 
      <concurrency-control error-if-maximum-exceeded="true" 
       max-sessions="1" expired-url="/login" /> 
     </session-management> 
     <remember-me token-validity-seconds="86400" /> 
    </http> 

    <authentication-manager alias="authenticationManager"> 
     <authentication-provider ref="daoAuthenticationProvider" /> 
    </authentication-manager> 

    <beans:bean id="daoAuthenticationProvider" 
     class="org.springframework.security.authentication.dao.DaoAuthenticationProvider"> 
     <beans:property name="userDetailsService" ref="userDetailsService" /> 
     <beans:property name="saltSource" ref="saltSource" /> 
     <beans:property name="passwordEncoder" ref="passwordEncoder" /> 
    </beans:bean> 

    <beans:bean id="passwordEncoder" 
     class="org.springframework.security.authentication.encoding.ShaPasswordEncoder" /> 

    <beans:bean id="saltSource" 
     class="org.springframework.security.authentication.dao.ReflectionSaltSource"> 
     <beans:property name="userPropertyToUse" value="salt" /> 
    </beans:bean> 

    <beans:bean id="userDetailsService" name="userAuthenticationProvider" 
     class="com.luffy.security.AuthenticationUserDetailService"> 
    </beans:bean> 
</beans:beans> 

すべてのヘルプは理解されるであろう。私はこの問題を解決するために私ができるすべてをしましたが、私は信頼できる解決策を見つけることができません。

答えて

1

Facebook認証を実装しているとします。

ソリューション(1):

のFacebookからの正常な応答で、あなたは、このようなユーザー名/メールとのOAuth access_tokenはとしてFacebookのユーザーの資格情報を認証テーブルを更新するために、サーバーのAPIを呼び出すことができます。

$.post('api/fblogin', {access_token: accessToken}, function(response) {}); 

ソリューション(2): カスタムセキュリティハンドラ。ここでは、FacebookへのカスタムHTTPリクエストを開始できます。また、正常終了すると、ユーザーはサイトにアクセスできます。私は長い時間からのこのような何かを探していたが、私はまだ私はFBからユーザを認証されたソリューションの一つの問題を持っている答えを

import java.util.*; 
import javax.servlet.http.HttpServletRequest; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.security.authentication.*; 
import org.springframework.security.core.*; 
import org.springframework.stereotype.Component; 
import com.restfb.*;; 

@Component 
public class CustomAuthenticationProvider implements AuthenticationProvider { 
    private @Autowired HttpServletRequest request; 

    @Override 
    public Authentication authenticate(Authentication authentication) { 
     String fb_access_token = String.valueOf(request.getParameter("fb_access_token")); 

     //fb user 
     Authentication auth = null; 
     try { 
      FacebookClient fbClient = new DefaultFacebookClient(fb_access_token); 
      User user = fbClient.fetchObject("me",com.restfb.types.User.class); 
      String email = user.getEmail(); 
      String gender = user.getGender(); 
      String pic = "http://graph.facebook.com/" + user.getId() + "/picture?type=normal"; 
      //Your DB implementation 

     } catch (Exception e) { 
      throw new FacebookOAuthException("FB","OAuth",5002, null, null, "", ""); 
     }  
    } 
} 

春-のsecurity.xml

<authentication-manager alias="authenticationManager" xmlns="http://www.springframework.org/schema/security"> 
     <authentication-provider ref="customAuthenticationProvider" > 
     </authentication-provider> 
</authentication-manager> 

<bean id="customAuthenticationProvider" class="com.mi.common.CustomAuthenticationProvider"/> 
+0

感謝同じユーザーを春に認証して認証することで、春にユーザーを検証できるようにするにはどうすればいいですか?申し訳ありませんが、あなたはそれが愚かなことを発見した場合私はかなりの時間からこれで立ち往生しています – Luffy

+0

Iveはカスタム実装を追加しましたか?それが役に立てば幸い。 customAuthenticationを処理するには、スプリング側で少しの設定が必要です。 –

関連する問題