提问者:小点点

多个身份验证提供程序:/j_spring_security_check和社会登录


public SpringSecurityLocalUser loadUserByUsername(final String userId) throws UsernameNotFoundException 

security_applicationcontext.xml

<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">

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

		<security:intercept-url pattern="/login" access="permitAll()" />
		<security:intercept-url pattern="/flow-entry.html" access="hasRole('ROLE_USER')"/>
		<security:intercept-url pattern="/flow-jobpostdata.html" access="permitAll()"/>
		<security:intercept-url pattern="/flow-jobpostdata_anydegree.html" access="permitAll()"/>
		 <security:intercept-url pattern="/j_spring_security_check" access="permitAll()"/>
		
	
	 <!-- 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"/>
	
	
	</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>

	<bean id="socialAuthenticationProvider" class="org.springframework.social.security.SocialAuthenticationProvider">
		<constructor-arg ref="inMemoryUsersConnectionRepository"/>
		<constructor-arg ref="socialUserDetailService"/>
	</bean>
	 
	 <bean id="appAuthenticationEntryPoint"
		 class=" jake.delivery.controller.welcome.AppAuthenticationEntryPoint">
		<constructor-arg name="loginFormUrl" value="/login"/>
 
	<bean id="failureHandler"
		 class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
		<constructor-arg name="defaultFailureUrl" value="/services/accessdenied"/>
	</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="jake.delivery.controller.welcome.AppSocialAuthenticationServiceRegistry">
		<constructor-arg>
			<list>
				<ref bean="facebookAuthenticationService"/>
			</list>
		</constructor-arg>
	</bean> 

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

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

	<bean id="connectionSignUp" class="jake.delivery.controller.welcome.AppConnectionSignUp"/>





</beans>

UserDetailService实现

package jake.prototype2.service.loginservices;

import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jake.prototype2.model.structure.SSm;

public class LocalUserDetailService implements UserDetailsService {



public LocalUserDetailService()
{
    SSm.getLogger().debug("init" );
}

@Override
@Transactional
public SpringSecurityLocalUser loadUserByUsername(final String userId) throws UsernameNotFoundException 
{

                SSm.getLogger().debug(this.getClass().getName()+"\n\n\n\n\n  I don't do anything yet\n\n\n\n\n\n",new Exception());
                SSm.getLogger().debug("userId" + userId);
                                    throw new UsernameNotFoundException("  fork me sideways  ");

}

}

斯塔克特雷斯。虽然没有例外,但我捕获了stacktrace以供参考。


共1个答案

匿名用户

这个问题实际上与需要多个身份验证提供者有关。

原来只缺少了一行配置:

<!--  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 ref="customAuthenticationProvider" />
    <security:authentication-provider user-service-ref="localUserDetailService"/>
</security:authentication-manager>

<bean id="customAuthenticationProvider" class="jake.delivery.controller.welcome.CustomAuthenticationProvider">
    <property name="auService" ref="auService" />
</bean>

我需要为CustomAuthenticationProviderAuthentication-Manager添加另外一行。