| | |
| | | package com.ruoyi.framework.config; |
| | | |
| | | import cn.dev33.satoken.interceptor.SaAnnotationInterceptor; |
| | | import cn.dev33.satoken.interceptor.SaRouteInterceptor; |
| | | import cn.dev33.satoken.router.SaRouter; |
| | | import cn.dev33.satoken.stp.StpUtil; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.framework.interceptor.PlusWebInvokeTimeInterceptor; |
| | | import com.yomahub.tlog.web.interceptor.TLogWebInterceptor; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.web.cors.CorsConfiguration; |
| | |
| | | import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; |
| | | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| | | |
| | | import java.util.Arrays; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 通用配置 |
| | | * |
| | |
| | | @Configuration |
| | | public class ResourcesConfig implements WebMvcConfigurer { |
| | | |
| | | // 注册sa-token的拦截器 |
| | | @Override |
| | | public void addInterceptors(InterceptorRegistry registry) { |
| | | List<String> urlPath = Arrays.asList( |
| | | "/login", |
| | | "/logout", |
| | | "/captchaImage", |
| | | "/*.html", |
| | | "/**/*.html", |
| | | "/**/*.css", |
| | | "/**/*.js", |
| | | "/profile/**", |
| | | "/common/download**", |
| | | "/common/download/resource**", |
| | | "/swagger-ui.html", |
| | | "/swagger-resources/**", |
| | | "/webjars/**", |
| | | "/*/api-docs", |
| | | "/druid/**", |
| | | "/actuator", |
| | | "/actuator/**" |
| | | ); |
| | | // 注册路由拦截器,自定义验证规则 |
| | | registry.addInterceptor(new SaRouteInterceptor((request, response, handler) -> { |
| | | // 登录验证 -- 排除多个路径 |
| | | SaRouter.match( |
| | | //获取所有的 |
| | | Collections.singletonList("/**"), |
| | | //排除下不需要拦截的 |
| | | urlPath, |
| | | () -> { |
| | | Long userId = SecurityUtils.getUserId(); |
| | | if(StringUtils.isNotNull(userId) ) { |
| | | long tokenTimeout = StpUtil.getTokenTimeout(); |
| | | long tokenActivityTimeout = StpUtil.getTokenActivityTimeout(); |
| | | System.out.println("剩余有效时间: " + tokenTimeout); |
| | | System.out.println("临时有效时间: " + tokenActivityTimeout); |
| | | } |
| | | }); |
| | | })).addPathPatterns("/**"); |
| | | registry.addInterceptor(new SaAnnotationInterceptor()).addPathPatterns("/**"); |
| | | // 全局链路跟踪拦截器 |
| | | registry.addInterceptor(new TLogWebInterceptor()); |
| | | // 全局访问性能拦截 |
| | | registry.addInterceptor(new PlusWebInvokeTimeInterceptor()); |
| | | } |
| | | |
| | | @Override |
| | |
| | | */ |
| | | @Bean |
| | | public CorsFilter corsFilter() { |
| | | UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); |
| | | CorsConfiguration config = new CorsConfiguration(); |
| | | config.setAllowCredentials(true); |
| | | // 设置访问源地址 |
| | |
| | | config.addAllowedHeader("*"); |
| | | // 设置访问源请求方法 |
| | | config.addAllowedMethod("*"); |
| | | // 对接口配置跨域设置 |
| | | // 有效期 1800秒 |
| | | config.setMaxAge(1800L); |
| | | // 添加映射路径,拦截一切请求 |
| | | UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); |
| | | source.registerCorsConfiguration("/**", config); |
| | | // 返回新的CorsFilter |
| | | return new CorsFilter(source); |
| | | } |
| | | } |