在Spring Boot中修改OAuth2ClientAuthenticationProcessingFilter

时间:2016-02-25 03:33:39

标签: spring spring-security spring-boot spring-security-oauth2

我想修改Spring Boot 1.3.2中OAuth2ClientAuthenticationProcessingFilter配置创建的@EnableOAuth2Sso。据我所知,OAuth2ClientAuthenticationProcessingFilter类是在内部构造的,没有任何应用程序上下文连接。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:2)

不确定这是最好的方法,但您可以使用BeanPostProcessor执行此操作:

public static class DefaultRolesPrefixPostProcessor implements BeanPostProcessor, PriorityOrdered {

        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof FilterChainProxy) {

                FilterChainProxy chains = (FilterChainProxy) bean;

                for (SecurityFilterChain chain : chains.getFilterChains()) {
                    for (Filter filter : chain.getFilters()) {
                        if (filter instanceof OAuth2ClientAuthenticationProcessingFilter) {
                            OAuth2ClientAuthenticationProcessingFilter oAuth2ClientAuthenticationProcessingFilter = (OAuth2ClientAuthenticationProcessingFilter) filter;
                            oAuth2ClientAuthenticationProcessingFilter
                                    .setAuthenticationSuccessHandler(authenticationSuccessHandler());
                        }
                    }
                }
            }
            return bean;
        }
    }