如何使用spring-social-security SocialAuthenticationFilter指定OAuth2范围?

Vic*_*sky 0 java spring-security oauth-2.0 spring-social

我正在使用Spring Social和Spring Security对用户进行身份验证,并在我的Web应用程序上自动创建本地帐户.如何提供OAuth2 scope进行身份验证?

春季社交样本中,我看不出scope应该去哪里.

<bean id="socialAuthenticationFilter" class="org.springframework.social.security.SocialAuthenticationFilter"
    c:_0-ref="authenticationManager"
    c:_1-ref="userIdSource"
    c:_2-ref="usersConnectionRepository"
    c:_3-ref="connectionFactoryLocator"
    p:signupUrl="/spring-social-showcase/signup"
    p:rememberMeServices-ref="org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices#0" />

<bean id="socialAuthenticationProvider" class="org.springframework.social.security.SocialAuthenticationProvider"
    c:_0-ref="usersConnectionRepository"
    c:_1-ref="socialUsersDetailService" />
Run Code Online (Sandbox Code Playgroud)

一个特定的用例scope是让用户通过Facebook进行身份验证,然后获取用户的Facebook电子邮件(scope="email")以创建本地帐户.

Vic*_*sky 8

在您的配置中,您需要指定scope为的属性FacebookAuthenticationService.这是处理调用的服务auth/facebook

在XML配置中,而不是:

<facebook:config app-id="${facebook.clientId}" app-secret="${facebook.clientSecret}"/>
Run Code Online (Sandbox Code Playgroud)

使用:

<bean id="connectionFactoryLocator" class="org.springframework.social.security.SocialAuthenticationServiceRegistry">
    <property name="authenticationServices">
        <list>
            <bean class="org.springframework.social.facebook.security.FacebookAuthenticationService">
                <constructor-arg value="${facebook.clientId}" />
                <constructor-arg value="${facebook.clientSecret}" />
                <!-- Important: The next property name changed from "scope" to "defaultScope" in 1.1.0.M4 -->
                <property name="scope" value="email" />              
            </bean>
        </list>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

这适用于Spring Social 1.1.0.M3

  • 在1.1.0.M4中,属性名称已从范围更改为defaultScope.请参阅https://github.com/spring-projects/spring-social/blob/1.1.0.M4/spring-social-security/src/main/java/org/springframework/social/security/provider/OAuth2AuthenticationService.java (3认同)