強調:除了以上代碼,還要確認訪問路徑為 anon
ResourcesConfig.java
package com.ruoyi.framework.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.*; import com.ruoyi.common.config.RuoYiConfig; import com.ruoyi.common.constant.Constants; import com.ruoyi.framework.interceptor.RepeatSubmitInterceptor; /** * 通用配置 * * @author ruoyi */ @Configuration public class ResourcesConfig implements WebMvcConfigurer { /** * 首頁地址 */ @Value("${shiro.user.indexUrl}") private String indexUrl; @Autowired private RepeatSubmitInterceptor repeatSubmitInterceptor; /** * 默認首頁的設置,當輸入域名是可以自動跳轉到默認指定的網頁 */ @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("forward:" + indexUrl); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { /** 本地文件上傳路徑 */ registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**").addResourceLocations("file:" + RuoYiConfig.getProfile() + "/"); /** swagger配置 */ registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); } /** * 自定義攔截規則 */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**"); } /** * zhangxuDev 配置跨域問題 * @param registry */ @Override public void addCorsMappings(CorsRegistry registry) { // 設置允許跨域的路徑 registry.addMapping("/api/**") // 設置允許跨域請求的域名 .allowedOrigins("*") // 是否允許證書 .allowCredentials(true) // 設置允許的方法 .allowedMethods("GET", "POST", "DELETE", "PUT") // 設置允許的header屬性 .allowedHeaders("*") // 跨域允許時間 .maxAge(3600); } }