使用SpringSocialConfigurer时如何更改SocialAuthenticationFilter.filterProcessesUrl?
How to change SocialAuthenticationFilter.filterProcessesUrl when using SpringSocialConfigurer?
Spring 社交 SocialAuthenticationFilter
处理 url /auth
。我该如何更改 url?
有一个方法SocialAuthenticationFilter.setFilterProcessesUrl(String filterProcessesUrl)
。如何访问 SocialAuthenticationFilter
对象以使用此方法? (我使用 SpringSocialConfigurer
,它会自动将 SocialAuthenticationFilter
添加到链中。)
不幸的是,使用原始 SpringSocialConfigurer
您无法设置已处理的 url。查看代码,您可以 "only" 流畅地配置 postLoginUrl
、postFailureUrl
、signupUrl
、connectionAddedRedirectUrl
、defaultFailureUrl
和 alwaysUsePostLoginUrl
.
恕我直言,这是 SpringSocialConfigurer
中的遗漏,您可能想在 https://github.com/spring-projects/spring-social/issues 上提交问题。
目前,只需使用 SpringSocialConfigurer
中的代码并提出您自己的(小)class。
根据用户 "Hille" 的建议,我现在通过执行以下操作创建了自己的 SpringSocialConfigurer
:
a) 通过 github (see here) 我已经复制了源代码并将其粘贴到我的自定义 class.
b) 然后我可以设置 filterProcessesUrl
- 这是一个例子:
public class MySpringSocialConfigurer extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {
@Override
public void configure(HttpSecurity http) throws Exception {
ApplicationContext applicationContext = http.getSharedObject(ApplicationContext.class);
UserIdSource userIdSource = getDependency(applicationContext, UserIdSource.class);
UsersConnectionRepository usersConnectionRepository = getDependency(applicationContext, UsersConnectionRepository.class);
SocialAuthenticationServiceLocator authServiceLocator = getDependency(applicationContext, SocialAuthenticationServiceLocator.class);
SocialUserDetailsService socialUsersDetailsService = getDependency(applicationContext, SocialUserDetailsService.class);
SocialAuthenticationFilter filter = new SocialAuthenticationFilter(
http.getSharedObject(AuthenticationManager.class),
userIdSource,
usersConnectionRepository,
authServiceLocator);
...
filter.setFilterProcessesUrl("/mysociallogin");
...
http.authenticationProvider(
new SocialAuthenticationProvider(usersConnectionRepository, socialUsersDetailsService))
.addFilterBefore(postProcess(filter), AbstractPreAuthenticatedProcessingFilter.class);
}
private <T> T getDependency(ApplicationContext applicationContext, Class<T> dependencyType) {
try {
T dependency = applicationContext.getBean(dependencyType);
return dependency;
} catch (NoSuchBeanDefinitionException e) {
throw new IllegalStateException("SpringSocialConfigurer depends on " + dependencyType.getName() +". No single bean of that type found in application context.", e);
}
}
}
Spring 社交 SocialAuthenticationFilter
处理 url /auth
。我该如何更改 url?
有一个方法SocialAuthenticationFilter.setFilterProcessesUrl(String filterProcessesUrl)
。如何访问 SocialAuthenticationFilter
对象以使用此方法? (我使用 SpringSocialConfigurer
,它会自动将 SocialAuthenticationFilter
添加到链中。)
不幸的是,使用原始 SpringSocialConfigurer
您无法设置已处理的 url。查看代码,您可以 "only" 流畅地配置 postLoginUrl
、postFailureUrl
、signupUrl
、connectionAddedRedirectUrl
、defaultFailureUrl
和 alwaysUsePostLoginUrl
.
恕我直言,这是 SpringSocialConfigurer
中的遗漏,您可能想在 https://github.com/spring-projects/spring-social/issues 上提交问题。
目前,只需使用 SpringSocialConfigurer
中的代码并提出您自己的(小)class。
根据用户 "Hille" 的建议,我现在通过执行以下操作创建了自己的 SpringSocialConfigurer
:
a) 通过 github (see here) 我已经复制了源代码并将其粘贴到我的自定义 class.
b) 然后我可以设置 filterProcessesUrl
- 这是一个例子:
public class MySpringSocialConfigurer extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {
@Override
public void configure(HttpSecurity http) throws Exception {
ApplicationContext applicationContext = http.getSharedObject(ApplicationContext.class);
UserIdSource userIdSource = getDependency(applicationContext, UserIdSource.class);
UsersConnectionRepository usersConnectionRepository = getDependency(applicationContext, UsersConnectionRepository.class);
SocialAuthenticationServiceLocator authServiceLocator = getDependency(applicationContext, SocialAuthenticationServiceLocator.class);
SocialUserDetailsService socialUsersDetailsService = getDependency(applicationContext, SocialUserDetailsService.class);
SocialAuthenticationFilter filter = new SocialAuthenticationFilter(
http.getSharedObject(AuthenticationManager.class),
userIdSource,
usersConnectionRepository,
authServiceLocator);
...
filter.setFilterProcessesUrl("/mysociallogin");
...
http.authenticationProvider(
new SocialAuthenticationProvider(usersConnectionRepository, socialUsersDetailsService))
.addFilterBefore(postProcess(filter), AbstractPreAuthenticatedProcessingFilter.class);
}
private <T> T getDependency(ApplicationContext applicationContext, Class<T> dependencyType) {
try {
T dependency = applicationContext.getBean(dependencyType);
return dependency;
} catch (NoSuchBeanDefinitionException e) {
throw new IllegalStateException("SpringSocialConfigurer depends on " + dependencyType.getName() +". No single bean of that type found in application context.", e);
}
}
}