All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.dengchen2020.security.config.SecurityAutoConfiguration Maven / Gradle / Ivy

package io.github.dengchen2020.security.config;

import io.github.dengchen2020.security.core.filter.AuthenticationFilter;
import io.github.dengchen2020.security.core.interceptor.AuthenticationInterceptor;
import io.github.dengchen2020.security.core.support.password.BCryptPasswordEncoder;
import io.github.dengchen2020.security.core.support.password.PasswordEncoder;
import io.github.dengchen2020.security.core.support.token.TokenService;
import io.github.dengchen2020.security.event.listener.SecurityScheduledHandleListener;
import io.github.dengchen2020.security.properties.SecurityProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.filter.OrderedFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Security过滤拦截自动配置
 * @author dengchen
 * @since 2024/4/25
 */
@EnableConfigurationProperties(SecurityProperties.class)
@Configuration(proxyBeanMethods = false)
public class SecurityAutoConfiguration implements WebMvcConfigurer {

    private final TokenService tokenService;

    private final SecurityProperties securityProperties;

    public SecurityAutoConfiguration(TokenService tokenService, SecurityProperties securityProperties) {
        this.tokenService = tokenService;
        this.securityProperties = securityProperties;
    }

    @ConditionalOnMissingBean(PasswordEncoder.class)
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder(4);
    }

    @Bean
    public FilterRegistrationBean authenticationFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new AuthenticationFilter(tokenService));
        filterRegistrationBean.setOrder(OrderedFilter.REQUEST_WRAPPER_FILTER_MAX_ORDER - 300);
        return filterRegistrationBean;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AuthenticationInterceptor())
                .order(Ordered.HIGHEST_PRECEDENCE)
                .excludePathPatterns(securityProperties.getResource().getPermitPath());
    }

    @Bean
    public SecurityScheduledHandleListener securityScheduledHandleListener(){
        return new SecurityScheduledHandleListener();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy