|
|
|
@ -0,0 +1,84 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* https://www.mall4j.com/
|
|
|
|
|
*
|
|
|
|
|
* 未经允许,不可做商业用途!
|
|
|
|
|
*
|
|
|
|
|
* 版权所有,侵权必究!
|
|
|
|
|
*/
|
|
|
|
|
package com.yami.shop.security.common.config;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.ArrayUtil;
|
|
|
|
|
// 导入自定义的授权配置适配器类,用于适配不同的授权配置相关逻辑,可根据具体业务需求进行扩展或定制
|
|
|
|
|
import com.yami.shop.security.common.adapter.AuthConfigAdapter;
|
|
|
|
|
// 导入默认的授权配置适配器实现类,当没有自定义的授权配置适配器时,会使用这个默认的实现类来处理相关逻辑
|
|
|
|
|
import com.yami.shop.security.common.adapter.DefaultAuthConfigAdapter;
|
|
|
|
|
// 导入自定义的授权过滤器类,用于对请求进行授权相关的过滤操作,比如验证用户权限等
|
|
|
|
|
import com.yami.shop.security.common.filter.AuthFilter;
|
|
|
|
|
import jakarta.servlet.DispatcherType;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
|
|
|
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
|
|
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 授权配置类,主要用于在Spring应用中配置授权相关的组件和规则。
|
|
|
|
|
* 这个类负责创建授权配置适配器以及注册授权过滤器等操作,以确保应用的授权功能能够正常工作。
|
|
|
|
|
*
|
|
|
|
|
* @author 菠萝凤梨
|
|
|
|
|
* @date 2022/3/25 17:33
|
|
|
|
|
*/
|
|
|
|
|
@Configuration
|
|
|
|
|
// 启用方法级别的安全配置,允许在方法上使用诸如 @PreAuthorize、@PostAuthorize 等注解来进行权限控制
|
|
|
|
|
@EnableMethodSecurity
|
|
|
|
|
public class AuthConfig {
|
|
|
|
|
// 通过自动注入获取授权过滤器实例,该过滤器将在后续的配置中被注册到Servlet容器中,用于对请求进行过滤处理
|
|
|
|
|
@Autowired
|
|
|
|
|
private AuthFilter authFilter;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建授权配置适配器的Bean。
|
|
|
|
|
* 如果在Spring容器中没有找到自定义的AuthConfigAdapter类型的Bean,
|
|
|
|
|
* 则会创建并返回一个默认的授权配置适配器实例(DefaultAuthConfigAdapter),
|
|
|
|
|
* 这样可以方便地进行默认授权配置或者允许用户自定义授权配置来替换默认配置。
|
|
|
|
|
*
|
|
|
|
|
* @return AuthConfigAdapter实例,用于适配授权配置相关逻辑
|
|
|
|
|
*/
|
|
|
|
|
@Bean
|
|
|
|
|
@ConditionalOnMissingBean
|
|
|
|
|
public AuthConfigAdapter authConfigAdapter() {
|
|
|
|
|
return new DefaultAuthConfigAdapter();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建并注册授权过滤器的FilterRegistrationBean。
|
|
|
|
|
* 这个方法会创建一个FilterRegistrationBean对象,用于将授权过滤器(AuthFilter)注册到Servlet容器中,
|
|
|
|
|
* 并配置相关的属性,比如过滤路径、过滤器名称、优先级以及处理的请求类型等。
|
|
|
|
|
* 这里使用了 @Lazy 注解,表示该Bean会延迟初始化,只有在第一次使用时才会真正创建实例,避免不必要的资源消耗。
|
|
|
|
|
*
|
|
|
|
|
* @param authConfigAdapter 授权配置适配器实例,用于获取过滤路径等配置信息
|
|
|
|
|
* @return FilterRegistrationBean<AuthFilter>对象,用于将授权过滤器注册到Servlet容器
|
|
|
|
|
*/
|
|
|
|
|
@Bean
|
|
|
|
|
@Lazy
|
|
|
|
|
public FilterRegistrationBean<AuthFilter> filterRegistration(AuthConfigAdapter authConfigAdapter) {
|
|
|
|
|
FilterRegistrationBean<AuthFilter> registration = new FilterRegistrationBean<>();
|
|
|
|
|
// 将之前注入的授权过滤器添加到FilterRegistrationBean中,这样Servlet容器就能识别并使用该过滤器来处理请求了
|
|
|
|
|
registration.setFilter(authFilter);
|
|
|
|
|
// 设置过滤路径,通过授权配置适配器获取需要过滤的路径列表,并转换为字符串数组格式。
|
|
|
|
|
// 这里使用ArrayUtil工具类将获取到的路径列表转换为String数组,以便设置给FilterRegistrationBean,
|
|
|
|
|
// 表示该过滤器将会对这些指定的路径下的请求进行过滤处理,此处的 /* 表示所有路径,具体路径配置通常由授权配置适配器决定
|
|
|
|
|
registration.addUrlPatterns(ArrayUtil.toArray(authConfigAdapter.pathPatterns(), String.class));
|
|
|
|
|
registration.setName("authFilter");
|
|
|
|
|
// 设置过滤器的优先级,数值越小优先级越高,这里设置为0,表示较高的优先级,在多个过滤器存在的情况下会先执行该过滤器
|
|
|
|
|
registration.setOrder(0);
|
|
|
|
|
registration.setDispatcherTypes(DispatcherType.REQUEST);
|
|
|
|
|
return registration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|