<commit_before>package org.lognet.springboot.grpc.security;

import io.grpc.BindableService;
import io.grpc.ServerInterceptor;
import io.grpc.Status;
import lombok.extern.slf4j.Slf4j;
import org.aopalliance.intercept.MethodInterceptor;
import org.lognet.springboot.grpc.GRpcErrorHandler;
import org.lognet.springboot.grpc.GRpcGlobalInterceptor;
import org.lognet.springboot.grpc.autoconfigure.ConditionalOnMissingErrorHandler;
import org.lognet.springboot.grpc.recovery.ErrorHandlerAdapter;
import org.lognet.springboot.grpc.recovery.GRpcExceptionHandler;
import org.lognet.springboot.grpc.recovery.GRpcExceptionScope;
import org.lognet.springboot.grpc.recovery.GRpcServiceAdvice;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor;
import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.core.AuthenticationException;

import java.util.Collection;
import java.util.Optional;



@Configuration
public class GrpcSecurityConfiguration {
    @Autowired
    private ConfigurableListableBeanFactory beanFactory;

    @Autowired
    private ObjectPostProcessor<Object> objectObjectPostProcessor;

    @SuppressWarnings({ "rawtypes" })
    private Collection<GrpcSecurityConfigurer> grpcSecurityConfigurers;

    private GrpcSecurity grpcSecurity;

    @Bean
    public BeanPostProcessor bypassMethodInterceptorForGrpcMethodInvocation(){
        return new BeanPostProcessor() {
            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                if(bean instanceof MethodSecurityInterceptor){
                    return (MethodInterceptor) invocation -> {
                        if (BindableService.class.isAssignableFrom(invocation.getMethod().getDeclaringClass())){
                            return invocation.proceed();
                        }
                        return ((MethodSecurityInterceptor) bean).invoke(invocation);
                    };
                }
                return bean;
            }
        };
    }

    @ConditionalOnMissingErrorHandler(AccessDeniedException.class)
    @Configuration
    static  class DefaultAccessDeniedErrorHandlerConfig{
        @GRpcServiceAdvice
        @Slf4j
        public static class DefaultAccessDeniedErrorHandler extends ErrorHandlerAdapter {

            public DefaultAccessDeniedErrorHandler(Optional<GRpcErrorHandler> errorHandler) {
                super(errorHandler);
            }
<commit_msg>Improve the problem of not being able to start BeanPostProcessorChecker and SecurityAutoConfiguration when disabled. (#257)<commit_after>package org.lognet.springboot.grpc.security;

import io.grpc.BindableService;
import io.grpc.ServerInterceptor;
import io.grpc.Status;
import lombok.extern.slf4j.Slf4j;
import org.aopalliance.intercept.MethodInterceptor;
import org.lognet.springboot.grpc.GRpcErrorHandler;
import org.lognet.springboot.grpc.GRpcGlobalInterceptor;
import org.lognet.springboot.grpc.autoconfigure.ConditionalOnMissingErrorHandler;
import org.lognet.springboot.grpc.recovery.ErrorHandlerAdapter;
import org.lognet.springboot.grpc.recovery.GRpcExceptionHandler;
import org.lognet.springboot.grpc.recovery.GRpcExceptionScope;
import org.lognet.springboot.grpc.recovery.GRpcServiceAdvice;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor;
import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.core.AuthenticationException;

import java.util.Collection;
import java.util.Optional;



@Configuration
public class GrpcSecurityConfiguration {
    @Autowired
    private ConfigurableListableBeanFactory beanFactory;

    @Autowired
    private ObjectPostProcessor<Object> objectObjectPostProcessor;

    @SuppressWarnings({ "rawtypes" })
    private Collection<GrpcSecurityConfigurer> grpcSecurityConfigurers;

    private GrpcSecurity grpcSecurity;

    @Bean
    public static BeanPostProcessor bypassMethodInterceptorForGrpcMethodInvocation(){
        return new BeanPostProcessor() {
            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                if(bean instanceof MethodSecurityInterceptor){
                    return (MethodInterceptor) invocation -> {
                        if (BindableService.class.isAssignableFrom(invocation.getMethod().getDeclaringClass())){
                            return invocation.proceed();
                        }
                        return ((MethodSecurityInterceptor) bean).invoke(invocation);
                    };
                }
                return bean;
            }
        };
    }

    @ConditionalOnMissingErrorHandler(AccessDeniedException.class)
    @Configuration
    static  class DefaultAccessDeniedErrorHandlerConfig{
        @GRpcServiceAdvice
        @Slf4j
        public static class DefaultAccessDeniedErrorHandler extends ErrorHandlerAdapter {

            public DefaultAccessDeniedErrorHandler(Optional<GRpcErrorHandler> errorHandler) {
                super(errorHandler);
            }

{'fraction_non_alphanumeric': 0.0722213537595748, 'fraction_numerical': 0.0014069094888228856, 'mean_word_length': 4.0818109610802225, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4633087', 'n_tokens_mistral': 1604, 'n_tokens_neox': 1606, 'n_words': 259}