当前位置:   article > 正文

java -- 基础 -- 网络相关_org.springframework.web.servlet.mvc.method.annotat

org.springframework.web.servlet.mvc.method.annotation public interface respo

转载地址:  https://segmentfault.com/a/1190000011145364

1、通过 jsonp 跨域

  • 配置类
  1. package com.sample.common.advice;
  2. import com.sample.modules.web.controller.JsonpController;
  3. import org.springframework.web.bind.annotation.ControllerAdvice;
  4. import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice;
  5. //配置jsonp,这里选择了 JsonpController
  6. @ControllerAdvice(basePackageClasses = {JsonpController.class})
  7. public class JSONPConfiguration extends AbstractJsonpResponseBodyAdvice {
  8. public JSONPConfiguration(){
  9. super("callback","jsonp");
  10. }
  11. }
  • 控制器
  1. package com.sample.modules.web.controller;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. @RestController
  7. public class JsonpController {
  8. @RequestMapping(value = "/jsonp")
  9. public Map<String, Object> jsonp(){
  10. Map<String, Object> result = new HashMap<>();
  11. result.put("username", "admin");
  12. return result;
  13. }
  14. }
  • 网页文件
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
  7. </head>
  8. <body>
  9. </body>
  10. <script>
  11. $(function(){
  12. $.ajax({
  13. type:"get",
  14. dataType: 'jsonp',
  15. jsonpCallback: "jsonpcallback",
  16. url:"http://www.test001.com:8080/jsonp"
  17. });
  18. })
  19. function jsonpcallback(result){
  20. alert(JSON.stringify(result))
  21. }
  22. </script>
  23. </html>

2、通过跨域资源共享(CORS)

  • 配置类
  1. package com.sample.common.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.web.cors.CorsConfiguration;
  5. import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
  6. import org.springframework.web.filter.CorsFilter;
  7. @Configuration
  8. public class CorsConfig {
  9. private CorsConfiguration buildConfig() {
  10. CorsConfiguration corsConfiguration = new CorsConfiguration();
  11. corsConfiguration.addAllowedOrigin("*"); // 1允许任何域名使用
  12. corsConfiguration.addAllowedHeader("*"); // 2允许任何头
  13. corsConfiguration.addAllowedMethod("*"); // 3允许任何方法(post、get等)
  14. return corsConfiguration;
  15. }
  16. @Bean
  17. public CorsFilter corsFilter() {
  18. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  19. source.registerCorsConfiguration("/**", buildConfig()); // 4
  20. return new CorsFilter(source);
  21. }
  22. }
  • 网页文件
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
  7. </head>
  8. <body>
  9. </body>
  10. <script>
  11. $(function(){
  12. $.ajax({
  13. type:"get",
  14. dataType: 'json',
  15. url:"http://www.test001.com:8080/jsonp",
  16. success: function(result){
  17. alert(JSON.stringify(result))
  18. }
  19. });
  20. })
  21. </script>
  22. </html>
  • 注意事项

  1. response.setHeader("Access-Control-Allow-Origin", "http://localhost:4001");
  2. //添加了Credentials, Origin不能为*, 必须指定
  3. response.addHeader("Access-Control-Allow-Credentials", "true");
  4. response.setHeader("Access-Control-Allow-Methods", "*");
  5. //需要配置header
  6. response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type,token,Accept, Connection, User-Agent, Cookie");
  7. response.setHeader("Access-Control-Max-Age", "3600");

3、防盗链

  1. package com.vim.modules.web;
  2. import javax.servlet.*;
  3. import javax.servlet.annotation.WebFilter;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import java.io.IOException;
  7. @WebFilter(urlPatterns = "/images/*")
  8. public class ChainStealingFilter implements Filter{
  9. @Override
  10. public void init(FilterConfig filterConfig) throws ServletException {
  11. }
  12. @Override
  13. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
  14. HttpServletRequest request = (HttpServletRequest)servletRequest;
  15. HttpServletResponse response = (HttpServletResponse)servletResponse;
  16. String referer = request.getHeader("referer");
  17. String serverName = request.getServerName();
  18. if(referer == null || !(referer.contains(serverName))){
  19. request.getRequestDispatcher("/images/error.jpg").forward(request, response);
  20. return;
  21. }
  22. chain.doFilter(request, response);
  23. }
  24. @Override
  25. public void destroy() {
  26. }
  27. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/229781
推荐阅读
相关标签
  

闽ICP备14008679号