zoukankan      html  css  js  c++  java
  • springboot跳转到其它页面

    一、单纯跳转

    有时候我们需要将用户提交的请求跳转到其它页面,下面代码即可实现这个功能

    @Controller
    @RequestMapping("login")
    @Api("模拟登陆")
    public class LoginController {
        private final static Logger logger = LoggerFactory.getLogger(LoginController.class);
    
        @GetMapping("redirect")
        @ApiOperation("跳转到百度")
        public void doLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
            logger.info(request.getPathInfo());
            response.sendRedirect("http://www.baidu.com");
        }
    }

     二、模拟登陆

    有时候我们需要在后端模拟前端登陆页面的登陆,登陆成功后跳转

    @GetMapping("redirect2")
        @ApiOperation("跳转到百度")
        public void doLogin2(HttpServletRequest request, HttpServletResponse response) throws IOException {
            //创建HttpClient
            CloseableHttpClient httpClient = HttpClients.createDefault();
            //登陆网址
            String loginUrl = "http://xxx.com.cn/api/user/login";
            HttpPost httpPost = new HttpPost(loginUrl);
            //以Post方式请求,设置登录用户名和密码
            List<NameValuePair> nameValuePairs = new ArrayList<>();
            nameValuePairs.add(new BasicNameValuePair("loginName", "xxxxx")); //这里改为自己的用户名
            nameValuePairs.add(new BasicNameValuePair("loginPwd", "123456"));//这里改为自己的密码
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            logger.info("statusCode:" + statusCode);
            InputStream inputStream = httpResponse.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = reader.readLine()) != null) {
                logger.info(line);
            }
            inputStream.close();
            reader.close();
            //登陆成功后跳转到百度
            response.sendRedirect("http://www.baidu.com");
        }

    三、模拟登陆后访问内部页面

    登陆后访问百度其实没有什么意义,因为百度本来也不需要登陆;

    如果我们登陆后可以去访问一些本来需要登陆才能访问的页面,就可以获取更多有价值的信息

  • 相关阅读:
    C# 中的高性能计时器(Daniel Strigl著,野比译)(转)
    C#中SerialPort类 随笔
    迭代器
    枚举数与可枚举类型(笔记)
    jQuery的关注与学习
    敏捷开发的4句宣言 大数据
    bat脚本
    c++动态库中回调函数使用
    C++中遍历lua table
    vs2010编译release版本却依赖debug版本库msvcrd100.dll
  • 原文地址:https://www.cnblogs.com/wangbin2188/p/14889507.html
Copyright © 2011-2022 走看看