不知道這是做的最好的方式,但你可以用了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;
}
}
究竟是如何將你'包括DefaultRolesPrefixPostProcessor'?我嘗試了'@Bean public DefaultRolesPrefixPostProcessor defaultRolesPrefixPostProcessor(){}',但在啓動時出現以下異常。 'IllegalStateException:ObjectPostProcessor是必需的bean。確保您已使用@EnableWebSecurity和@ Configuration' –
我們在其中一個帶有「@Configuration」註釋類的靜態bean方法中創建它,因爲後處理器需要在任何其他bean之前創建: @ Bean public static DefaultRolesPrefixPostProcessor defaultRolesPrefixPostProcessor(){ return new DefaultRolesPrefixPostProcessor(); } –