2016-10-17 3 views
0

SZENARIO 1 Wenn mit Facebook meiner WebappFrühling facebook Login: Anmeldung umleiten

Wenn der Browser bereits bei Facebook angemeldet ist die Anmeldung und

wenn die sozialen Authentifizierungsdetails sind Benutzer bereits registriert in meinem Webapp,

dann, wenn der Benutzer klickt auf „Anmeldung mit Facebook“ auf meinem Webapp,

er/sie von Facebook und das Berechtigungsobjekt zurückgegeben b autorisiert ist, y

Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 

ist der benannte Benutzer.

Alles ist gut.


SZENARIO 2 Wenn der Browser nicht in Facebook aber

die soziale Authentifizierungsdaten des Benutzers angemeldet ist bereits in meinem Webapp registriert,

dann, wenn der Benutzer klickt auf „Anmeldung mit Facebook "auf meiner webapp,

der Benutzer meldet sich mit Facebook durch die Eingabe von Benutzername und Passwort ABER

die das Objekt Authorization zurückgegeben durch

Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 

autorisiert ist als anonymous und hat den Namen AnonymousUser.

Darüber hinaus ist der Aufruf von Facebook nach Genehmigung/Anmeldung

Ich bin jedoch die gleiche registrierte Benutzer wie in Szenario erwartet 1

Meine Sicherheitskonfiguration unten. Kann mir jemand einen Hinweis geben, was das Problem sein könnte?

<beans xmlns="http://www.springframework.org/schema/beans" 
 
     xmlns:security="http://www.springframework.org/schema/security" 
 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 
        http://www.springframework.org/schema/beans/spring-beans.xsd 
 
        http://www.springframework.org/schema/security 
 
        http://www.springframework.org/schema/security/spring-security.xsd"> 
 

 
\t <security:http use-expressions="true" entry-point-ref="appAuthenticationEntryPoint"> 
 

 
\t \t <security:intercept-url pattern="/login" access="permitAll()" /> 
 
\t \t <security:intercept-url pattern="/flow-entry.html" access="permitAll()"/> 
 
\t \t <security:intercept-url pattern="/flow-jobpostdata.html" access="permitAll()"/> 
 
\t \t <security:intercept-url pattern="/flow-jobpostdata_anydegree.html" access="permitAll()"/> 
 
\t \t <security:intercept-url pattern="/j_spring_security_check" access="permitAll()"/> \t 
 
    <!-- Adds social authentication filter to the Spring Security filter chain. --> 
 
     <security:custom-filter before="PRE_AUTH_FILTER" ref="socialAuthenticationFilter"/> 
 
     <security:custom-filter position="FORM_LOGIN_FILTER" ref="SecurityAuthFilter"/> 
 
\t </security:http> 
 

 
<!-- authentication manager and its provider(social provider deals with social login & local user provider deals with form login) --> 
 
    <security:authentication-manager alias="authenticationManager"> 
 
    
 
     <security:authentication-provider ref="socialAuthenticationProvider"/> 
 
     <security:authentication-provider user-service-ref="localUserDetailService"/> 
 
    </security:authentication-manager> 
 
    
 

 
    
 

 
\t <bean id="customAuthenticationProvider" class="com.ikoda.service.loginservices.CustomAuthenticationProvider"> 
 
\t \t \t <property name="auService" ref="auService" /> 
 
\t </bean> 
 

 
    <bean id="socialAuthenticationProvider" class="org.springframework.social.security.SocialAuthenticationProvider"> 
 
     <constructor-arg ref="inMemoryUsersConnectionRepository"/> 
 
     <constructor-arg ref="socialUserDetailService"/> 
 
    </bean> 
 

 
    <!-- form login beans --> 
 
    
 
    
 

 
     
 
     
 
    <bean id="successHandler" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler"> 
 
    </bean> 
 
     
 
    <bean id="appAuthenticationEntryPoint" 
 
      class="com.ikoda.service.loginservices.AppAuthenticationEntryPoint"> 
 
     <constructor-arg name="loginFormUrl" value="/login"/> 
 
    </bean> 
 
    
 
    <bean id="rememberMeServices" 
 
      class="org.springframework.security.web.authentication.NullRememberMeServices"/> 
 

 
    <bean id="failureHandler" 
 
      class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"> 
 
     <constructor-arg name="defaultFailureUrl" value="/login?error=true"/> 
 
    </bean> 
 
    
 
    
 

 

 
    
 
    <bean class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter" 
 
      id="SecurityAuthFilter"> 
 
     <property name="authenticationManager" ref="authenticationManager"/> 
 
     <property name="authenticationSuccessHandler" ref="successHandler"/> 
 
     <property name="authenticationFailureHandler" ref="failureHandler"/> 
 
     <property name="filterProcessesUrl" value="/j_spring_security_check"/> 
 
     <property name="rememberMeServices" ref="rememberMeServices"/> 
 
    </bean> 
 

 
    <!-- social login filter which is a pre authentication filter and works for /auth service url --> 
 
    <bean id="socialAuthenticationFilter" class="org.springframework.social.security.SocialAuthenticationFilter"> 
 
     <constructor-arg name="authManager" ref="authenticationManager"/> 
 
     <constructor-arg name="userIdSource" ref="userIdSource"/> 
 
     <constructor-arg name="usersConnectionRepository" ref="inMemoryUsersConnectionRepository"/> 
 
     <constructor-arg name="authServiceLocator" ref="appSocialAuthenticationServiceRegistry"/> 
 
     <property name="authenticationSuccessHandler" ref="successHandler"/> 
 
    </bean> 
 

 

 
    <!-- inmemory connection repository which holds connection repository per local user --> 
 
    <bean id="inMemoryUsersConnectionRepository" 
 
      class="org.springframework.social.connect.mem.InMemoryUsersConnectionRepository"> 
 
     <constructor-arg name="connectionFactoryLocator" ref="appSocialAuthenticationServiceRegistry"/> 
 
     <property name="connectionSignUp" ref="connectionSignUp"/> 
 
    </bean> 
 

 
    <!-- service registry will holds connection factory of each social provider--> 
 
    <bean id="appSocialAuthenticationServiceRegistry" 
 
      class="com.ikoda.service.loginservices.AppSocialAuthenticationServiceRegistry"> 
 
     <constructor-arg> 
 
      <list> 
 
       <ref bean="facebookAuthenticationService"/> 
 
       <ref bean="googleAuthenticationService"/> 
 
      </list> 
 
     </constructor-arg> 
 
    </bean> 
 

 
    <bean id="facebookAuthenticationService" 
 
      class="org.springframework.social.facebook.security.FacebookAuthenticationService"> 
 
     <constructor-arg name="apiKey" value="11111"/> 
 
     <constructor-arg name="appSecret" value="11111"/> 
 
    </bean> 
 

 
    <bean id="googleAuthenticationService" 
 
      class="org.springframework.social.google.security.GoogleAuthenticationService"> 
 
     <constructor-arg name="apiKey" value="111-lpmhcmuj1577bd6god0696g4u2g16c1i.apps.googleusercontent.com"/> 
 
     <constructor-arg name="appSecret" value="111-111-"/> 
 
    </bean> 
 
    
 

 
    <bean id="userIdSource" class="org.springframework.social.security.AuthenticationNameUserIdSource"/> 
 

 
    <!-- If no local user is associated to a social connection then connection sign up will create a new local user and map it to social user --> 
 
    <bean id="connectionSignUp" class="com.ikoda.service.loginservices.AppConnectionSignUp"/> 
 

 
</beans>

Antwort

0

Die Umleitung auf die Registrierung ist das beabsichtigte Verhalten des Frühlings Sozial. Der Schlüssel ist, wie man das Verhalten verwaltet.

Eine Möglichkeit, das Problem zu lösen, besteht in der Authentifizierung des sozialen Logins in der Controller-Anmeldemethode. Um dies zu tun, müssen Sie auf die soziale Verbindung zugreifen. Sie können dies tun, indem Sie in Ihrem Controller ProviderSignInUtils autowiren.

Hinweis AppConnectionSignUP ist eine Implementierung von org.springframework.social.connect.ConnectionSignUp

Connection<?> connection = providerSignInUtils.getConnectionFromSession(webRequest); 
     if (null != connection) 
     { 

      String userIdString = appConnectionSignUp.execute(connection); 
     } 

Eine andere Möglichkeit, das Problem zu lösen, ist Ihre eigene ConnectionSignUp

<bean id="connectionSignUp" class="com.ikoda.service.loginservices.AppConnectionSignUp"/>

und Bezug zu implementieren, die in Ihrem UsersConnectionRepository

 <bean id="jdbcUsersConnectionRepository" 
 
       class="org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository"> 
 
      <constructor-arg ref="dataSource" /> 
 
      <constructor-arg ref="appSocialAuthenticationServiceRegistry" /> 
 
      <constructor-arg ref="textEncryptor" /> 
 
      <property name="connectionSignUp" ref="connectionSignUp"/> 
 
     </bean>

Wenn Sie diesen Ansatz verwenden, wird die Anmeldung für soziale Netzwerke den Controller/signUp umgehen und die Methode execute von connectionSignUP bean direkt aufrufen.

Unterdessen war das inMemoryUsersConnectionRepository in der obigen Frage schlecht beraten. Ich wechselte zum jdbcUsersConnectionRepository.