{"id":1987,"date":"2021-05-07T00:54:25","date_gmt":"2021-05-06T16:54:25","guid":{"rendered":"https:\/\/www.xiaobo.li\/notes\/?p=1987"},"modified":"2021-05-07T00:54:25","modified_gmt":"2021-05-06T16:54:25","slug":"webmvcconfig","status":"publish","type":"post","link":"https:\/\/www.xiaobo.li\/notes\/archives\/1987","title":{"rendered":"WebMvcConfig"},"content":{"rendered":"<p>.<\/p>\n<pre class=\"pure-highlightjs\"><code class=\"java\">\r\n\r\nimport org.slf4j.Logger;\r\nimport org.slf4j.LoggerFactory;\r\nimport org.springframework.context.annotation.Bean;\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\nimport org.springframework.web.socket.server.standard.ServerEndpointExporter;\r\n\r\n\r\n@Configuration\r\npublic class WebMvcConfig implements WebMvcConfigurer {\r\n\r\n    private final Logger logger = LoggerFactory.getLogger(WebMvcConfig.class);\r\n\r\n    @Override\r\n    public void addCorsMappings(CorsRegistry registry) {\r\n        registry.addMapping(\"\/**\")\r\n                .allowedOrigins(\"*\")\r\n                .allowedMethods(\"GET\", \"POST\",\"OPTIONS\")\r\n                .allowedHeaders(\"*\")\r\n                .exposedHeaders(\"Access-Control-Allow-Headers\",\r\n                        \"Access-Control-Allow-Methods\",\r\n                        \"Access-Control-Allow-Origin\",\r\n                        \"Access-Control-Max-Age\",\r\n                        \"X-Frame-Options\")\r\n                .allowCredentials(false)\r\n                .maxAge(3600);\r\n    }\r\n\r\n    \r\n    \/**\r\n     * The bean shown in the preceding example registers any @ServerEndpoint\r\n     * annotated beans with the underlying WebSocket container. When deployed to a\r\n     * standalone servlet container, this role is performed by a servlet container\r\n     * initializer, and the ServerEndpointExporter bean is not required.\r\n     *\/\r\n    @Bean\r\n    public ServerEndpointExporter serverEndpointExporter() {\r\n        return new ServerEndpointExporter();\r\n    }\r\n    \r\n}<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>. import org.slf4j.Logger; import org.sl &hellip; <a href=\"https:\/\/www.xiaobo.li\/notes\/archives\/1987\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[297],"tags":[],"class_list":["post-1987","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/1987","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/comments?post=1987"}],"version-history":[{"count":0,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/1987\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/media?parent=1987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/categories?post=1987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/tags?post=1987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}