{"id":223104,"date":"2021-08-01T10:08:54","date_gmt":"2021-08-01T02:08:54","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=223104"},"modified":"2021-07-23T10:12:16","modified_gmt":"2021-07-23T02:12:16","slug":"springboot-demo-config","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/springboot-demo-config.html","title":{"rendered":"springboot2.4\u8de8\u57df\u914d\u7f6e\u7684\u65b9\u6cd5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86springboot2.4\u8de8\u57df\u914d\u7f6e\u7684\u65b9\u6cd5\uff0c\u672c\u6587\u901a\u8fc7\u5b9e\u4f8b\u4ee3\u7801\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\uff0c\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6216\u5de5\u4f5c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u501f\u9274\u4ef7\u503c,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

1\u3001\u5982\u679c\u53ea\u662f\u4e00\u4e2a\u7b80\u5355\u7684springboot demo\uff0c\u7528\u4ee5\u4e0b\u914d\u7f6e\u5c31\u884c
\n\u65b0\u5efaconfig\u7c7b<\/p>\n

\r\n```\r\nimport org.springframework.context.annotation.Configuration;\r\nimport org.springframework.web.servlet.config.annotation.CorsRegistry;\r\nimport org.springframework.web.servlet.config.annotation.WebMvcConfigurer;\r\n  \r\n\/**\r\n * @author yk\r\n * @date 2021\/7\/19 14:36\r\n *\/\r\n@Configuration\r\npublic class WebConfig implements WebMvcConfigurer {\r\n    @Override\r\n    public void addCorsMappings(CorsRegistry registry) {\r\n        registry.addMapping(\"\/**\")\r\n                .allowedOriginPatterns(\"*\")\r\n                .allowedMethods(\"*\")\r\n                .maxAge(3600)\r\n                .allowCredentials(true);\r\n    }\r\n}\r\n \r\n```\r\n<\/pre>\n

2\u3001\u4f46\u662f\u5b9e\u9645\u5f00\u53d1\u4e2d\u6211\u4eec\u9700\u8981\u7ed3\u5408\uff0cspring-security\u3001oauth2\u7b49\u7b49\uff0c\u5c31\u4f1a\u53d1\u73b0\u4e0a\u9762\u7684\u914d\u7f6e\u5931\u6548\u4e86\uff0c\u90a3\u662f\u56e0\u4e3a\u524d\u9762\u7684Filter\u4f18\u5148\u7ea7\u592a\u9ad8\u4e86\uff0c\u90a3\u6211\u4eec\u53ef\u4ee5\u91c7\u53d6\u5982\u4e0b\u914d\u7f6e<\/p>\n

\r\n```\r\n \r\nimport org.springframework.boot.web.servlet.FilterRegistrationBean;\r\nimport org.springframework.context.annotation.Bean;\r\nimport org.springframework.context.annotation.Configuration;\r\nimport org.springframework.core.Ordered;\r\nimport org.springframework.web.cors.CorsConfiguration;\r\nimport org.springframework.web.cors.UrlBasedCorsConfigurationSource;\r\nimport org.springframework.web.filter.CorsFilter;\r\n  \r\n\/**\r\n * @author yk\r\n * @date 2021\/7\/19 16:21\r\n *\/\r\n@Configuration\r\npublic class CrosConfig {\r\n  \r\n    @Bean\r\n    public FilterRegistrationBean corsFilter() {\r\n        CorsConfiguration config = new CorsConfiguration();\r\n        config.setAllowCredentials(true);\r\n        config.addAllowedOriginPattern(\"*\");\r\n        config.addAllowedHeader(\"*\");\r\n        config.addAllowedMethod(\"*\");\r\n        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();\r\n        source.registerCorsConfiguration(\"\/**\", config);\r\n        FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));\r\n        \/\/\u8fd9\u91cc\u8bbe\u7f6e\u4f18\u5148\u7ea7\u6700\u9ad8\r\n        bean.setOrder(Ordered.HIGHEST_PRECEDENCE);\r\n        return bean;\r\n    }\r\n}\r\n<\/pre>\n

\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8espringboot2.4\u8de8\u57df\u914d\u7f6e\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"

1\u3001\u5982\u679c\u53ea\u662f\u4e00\u4e2a\u7b80\u5355\u7684springboot demo\uff0c\u7528\u4ee5\u4e0b\u914d\u7f6e\u5c31\u884c \u65b0\u5efaconfig\u7c7b “` import […]<\/p>\n","protected":false},"author":668,"featured_media":223105,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-223104","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/223104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=223104"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/223104\/revisions"}],"predecessor-version":[{"id":223107,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/223104\/revisions\/223107"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/223105"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=223104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=223104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=223104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}