springboot 跨域配置類及跨域請求配置
下面通過代碼看下springboot 跨域配置類,代碼如下所示:
ackage org.fh.config; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Component; /** * 說明:跨域訪問處理 * 作者:FH Admin * from:fhadmin.cn */ @Component public class CORSFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; response.setContentType("textml;charset=UTF-8"); response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin")); response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); response.setHeader("Access-Control-Max-Age", "0"); response.setHeader("Access-Control-Allow-Headers","Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With,userId,token"); response.setHeader("Access-Control-Allow-Credentials", "true"); // 是否支持cookie跨域 response.setHeader("XDomainRequestAllowed", "1"); filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { } }
1.模型管理:web在線流程設計器、導入導出xml、復制流程、部署流程
2.流程管理:導入導出流程資源文件、查看流程圖、根據流程實例反射出流程模型、激活掛起
3.運行中流程:查看流程信息、當前任務節(jié)點、當前流程圖、作廢暫停流程、指派待辦人、自由跳轉
4.歷史的流程:查看流程信息、流程用時、流程狀態(tài)、查看任務發(fā)起人信息
5.待辦任務:查看本人個人任務以及本角色下的任務、辦理、駁回、作廢、指派一下代理人
6.已辦任務:查看自己辦理過的任務以及流程信息、流程圖、流程狀態(tài)(作廢 駁回 正常完成)
補充:下面看下springboot跨域請求配置的兩種方法
方式一:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; @Configuration public class CorsConfig { ? ? private CorsConfiguration buildConfig() { ? ? ? ? CorsConfiguration corsConfiguration = new CorsConfiguration(); ? ? ? ? corsConfiguration.addAllowedOrigin("*"); // 允許任何域名使用 ? ? ? ? corsConfiguration.addAllowedHeader("*"); // 允許任何頭 ? ? ? ? corsConfiguration.addAllowedMethod("*"); // 允許任何方法(post、get等) ? ? ? ? corsConfiguration.setAllowCredentials(true); ? ? ? ? return corsConfiguration; ? ? } ? ?? ? ? @Bean ? ? public CorsFilter corsFilter() { ? ? ? ? UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); ? ? ? ? source.registerCorsConfiguration("/**", buildConfig()); // 對接口配置跨域設置 ? ? ? ? return new CorsFilter(source); ? ? } }
方式二:
import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration //加配置注解可以掃描到 public class WebConfig implements WebMvcConfigurer{ ? ?? ? ? //跨域請求配置 ? ? @Override ? ? public void addCorsMappings(CorsRegistry registry) { ? ? ? ? WebMvcConfigurer.super.addCorsMappings(registry); ? ? ? ? registry.addMapping("/**")// 對接口配置跨域設置 ? ? ? ? ? ? ? ? .allowedHeaders("*")// 允許任何頭 ? ? ? ? ? ? ? ? .allowedMethods("POST","GET")// 允許方法(post、get等) ? ? ? ? ? ? ? ? .allowedOrigins("*")// 允許任何域名使用 ? ? ? ? ? ? ? ? .allowCredentials(true); ? ? } ? ?? }
到此這篇關于springboot 跨域配置類及跨域請求配置的文章就介紹到這了,更多相關springboot 跨域配置內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java8時間api之LocalDate/LocalDateTime的用法詳解
在項目中,時間的使用必不可少,而java8之前的時間api?Date和Calander等在使用上存在著很多問題,于是,jdk1.8引進了新的時間api-LocalDateTime,本文就來講講它的具體使用吧2023-05-05Spring Security使用數據庫認證及用戶密碼加密和解密功能
這篇文章主要介紹了Spring Security使用數據庫認證及用戶密碼加密和解密,本文通過代碼與截圖的形式給大家介紹的非常詳細,對大家的工作或學習具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03spring boot activiti工作流的搭建與簡單使用
這篇文章主要給大家介紹了關于spring boot activiti工作流的搭建與簡單使用的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-08-08springboot post接口接受json時,轉換為對象時,屬性都為null的解決
這篇文章主要介紹了springboot post接口接受json時,轉換為對象時,屬性都為null的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10