当前位置: 首页 > 知识库问答 >
问题:

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

常嘉平
2023-03-14
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个答案

薛欣荣
2023-03-14

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

原来只缺少了一行配置:

<!--  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添加另外一行。

 类似资料:
  • 我正在尝试为正在进行的 spring-boot 项目实现身份验证和授权服务。我已经实现了一个基于 JPA 的身份验证提供程序,它工作正常。如何将 LDAP 身份验证提供程序添加到同一项目并根据用户身份验证类型在身份验证方法之间切换? 下面是我的代码 虽然我的LDAP证书是正确的,但它没有达到那个方法。如何从DB ex(LDAP、JPA、SSO)获取我的应用程序的身份验证方法,并执行相应的身份验证提

  • 问题内容: 使用Passport.js是否可以为同一路由指定多个身份验证提供程序? 例如(在护照指南中),我可以在以下示例路线中使用本地策略以及Facebook和Twitter策略吗? 问题答案: Passport的中间件的构建方式使您可以在一个呼叫中使用多种策略。 但是,它是用OR顺序定义的。也就是说,只有在所有策略均未成功返回的情况下,它才会失败。 这是您将如何使用它: 换句话说,使用它的方法

  • 我的问题是,我希望有两个身份验证提供商 之前:我有我的UserDetailServiceImpl,我根据数据库中的数据验证了用户的身份(不确定是哪个提供者) 现在:我使用了ActiveDirectoryLdapAuthentiation提供程序,如下所示 我成功了,所以我可以认证。 问题是: 我现在无法再使用数据库用户登录,现在只有LDAP。 未使用UserDetailsService,因此用户具

  • 问题内容: 在Spring Security中,有多个身份验证提供程序的参考,但是找不到Java config中的示例。 以下链接给出了XML表示法: Spring Security中的多个身份验证提供程序 我们需要使用LDAP或DB进行身份验证 下面是我们的示例代码: 问题答案: 也许这会帮助你:

  • 我希望我的web应用程序的用户通过LDAP和额外的自定义身份验证进行身份验证。这是一个用Kotlin编写的Spring Boot应用程序。我将AuthenticationManagerBuilder配置为 我希望链接身份验证,以便如果CustomAuthenticationProvider成功地进行身份验证(函数authenticate不抛出),身份验证将继续使用LDAP身份验证提供程序。 正如所

  • 问题内容: 我的Web应用程序有多个身份验证管理器(一个用于API,一个用于WEB访问)。该api应该仅具有基本的身份验证服务- 通过spring安全标记进行配置,如下所示: 我无法内联身份验证提供程序,因为我希望它可以被子bean配置覆盖。 我的问题是我无法在security:authentication-provider元素上定义别名/ id以在身份验证管理器中引用它。有一个简单的解决方法吗?