• 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");
        }

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

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

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

  • 相关阅读:
    在eclipse创建android project,最后一步点击finish没反应
    有哪些可以将网页另存图片的chrome插件?功能类似网页截图
    极品家丁—优酷全网独播喜剧
    如何安装chrome扩展?比如json-handle插件如何安装
    安装用户脚本的福音:Tampermonkey(油猴)
    多微博账号同时发微博的插件--fawave
    正能量-真正男子汉2
    如何看待优酷广告?
    秋雨连绵思晚天
    如何用Postman组装Request并且查看Response
  • 原文地址:https://www.cnblogs.com/wangbin2188/p/14889507.html
Copyright © 2020-2023  润新知