赞
踩
转载地址: https://segmentfault.com/a/1190000011145364
- package com.sample.common.advice;
-
- import com.sample.modules.web.controller.JsonpController;
- import org.springframework.web.bind.annotation.ControllerAdvice;
- import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice;
-
- //配置jsonp,这里选择了 JsonpController
- @ControllerAdvice(basePackageClasses = {JsonpController.class})
- public class JSONPConfiguration extends AbstractJsonpResponseBodyAdvice {
- public JSONPConfiguration(){
- super("callback","jsonp");
- }
- }
- package com.sample.modules.web.controller;
-
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import java.util.HashMap;
- import java.util.Map;
-
- @RestController
- public class JsonpController {
-
- @RequestMapping(value = "/jsonp")
- public Map<String, Object> jsonp(){
- Map<String, Object> result = new HashMap<>();
- result.put("username", "admin");
- return result;
- }
- }

- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title></title>
- <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
- </head>
- <body>
- </body>
- <script>
- $(function(){
- $.ajax({
- type:"get",
- dataType: 'jsonp',
- jsonpCallback: "jsonpcallback",
- url:"http://www.test001.com:8080/jsonp"
- });
- })
-
- function jsonpcallback(result){
- alert(JSON.stringify(result))
- }
- </script>
- </html>

- package com.sample.common.config;
-
- 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("*"); // 1允许任何域名使用
- corsConfiguration.addAllowedHeader("*"); // 2允许任何头
- corsConfiguration.addAllowedMethod("*"); // 3允许任何方法(post、get等)
- return corsConfiguration;
- }
-
- @Bean
- public CorsFilter corsFilter() {
- UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
- source.registerCorsConfiguration("/**", buildConfig()); // 4
- return new CorsFilter(source);
- }
- }

- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title></title>
- <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
- </head>
- <body>
- </body>
- <script>
- $(function(){
- $.ajax({
- type:"get",
- dataType: 'json',
- url:"http://www.test001.com:8080/jsonp",
- success: function(result){
- alert(JSON.stringify(result))
- }
- });
- })
- </script>
- </html>

注意事项
- response.setHeader("Access-Control-Allow-Origin", "http://localhost:4001");
-
- //添加了Credentials, Origin不能为*, 必须指定
- response.addHeader("Access-Control-Allow-Credentials", "true");
- response.setHeader("Access-Control-Allow-Methods", "*");
-
- //需要配置header
- response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type,token,Accept, Connection, User-Agent, Cookie");
- response.setHeader("Access-Control-Max-Age", "3600");
- package com.vim.modules.web;
-
- import javax.servlet.*;
- import javax.servlet.annotation.WebFilter;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
-
- @WebFilter(urlPatterns = "/images/*")
- public class ChainStealingFilter implements Filter{
-
- @Override
- public void init(FilterConfig filterConfig) throws ServletException {
-
- }
-
- @Override
- public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
- HttpServletRequest request = (HttpServletRequest)servletRequest;
- HttpServletResponse response = (HttpServletResponse)servletResponse;
-
- String referer = request.getHeader("referer");
- String serverName = request.getServerName();
-
- if(referer == null || !(referer.contains(serverName))){
- request.getRequestDispatcher("/images/error.jpg").forward(request, response);
- return;
- }
- chain.doFilter(request, response);
- }
-
- @Override
- public void destroy() {
-
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。