【问题标题】:How to change SocialAuthenticationFilter.filterProcessesUrl when using SpringSocialConfigurer?使用 SpringSocialConfigurer 时如何更改 Social AuthenticationFilter.filter Processes Url?
【发布时间】:2017-02-09 11:37:27
【问题描述】:

Spring Social SocialAuthenticationFilter 处理 url /auth。如何更改此网址?
有一个方法SocialAuthenticationFilter.setFilterProcessesUrl(String filterProcessesUrl)。如何访问SocialAuthenticationFilter 对象以使用此方法? (我使用SpringSocialConfigurer,它会自动将SocialAuthenticationFilter添加到链中。)

【问题讨论】:

    标签: spring spring-security spring-social spring-social-facebook


    【解决方案1】:

    很遗憾,使用原始SpringSocialConfigurer 无法设置处理后的网址。查看代码,您可能“只”流畅地配置 postLoginUrlpostFailureUrlsignupUrlconnectionAddedRedirectUrldefaultFailureUrlalwaysUsePostLoginUrl 等内容。

    恕我直言,这是SpringSocialConfigurer 中的一个遗漏,您可能想在https://github.com/spring-projects/spring-social/issues 提出问题。

    暂时,只需使用来自SpringSocialConfigurer 的代码,并提出您自己的(小)类。

    【讨论】:

    • 谢谢 - 是的,通过创建自定义 Configurer 可以设置它。我已经在另一个答案中发布了我的解决方案。
    • 我还在 github 上提交了一个问题 - see here
    【解决方案2】:

    按照用户“Hille”的建议,我现在通过执行以下操作创建了自己的SpringSocialConfigurer

    a) 通过 github (see here) 我已复制源代码并将其粘贴到我的自定义类中。

    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);
            }
        }
    }
    

    【讨论】:

    • 您可能希望将您的代码写得更“有弹性”,并保持现有的流畅方法,即添加if (filterProcessUrl != null ) { filter.setFilterProcessesUrl(filterProcessUrl); } 加上一个额外的设置器,如public MySpringSocialConfigurer filterProcessUrl(String filterProcessUrl),其代码类似于例如userIdSource(UserIdSource userIdSource).
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 2022-06-18
    • 2023-04-04
    • 2021-01-09
    • 1970-01-01
    相关资源
    最近更新 更多