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

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

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

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

  • 相关阅读:
    Codeforces Round #706 (Div. 2)
    Caddi Programming Contest 2021(AtCoder Beginner Contest 193)
    [ARC116] Deque Game
    Codeforces Round #721 (Div. 2)
    Codeforces Round #618 (Div. 1)
    Educational Codeforces Round 109 (Rated for Div. 2)
    [ABC201F] Insertion Sort
    AtCoder Regular Contest 119
    Codeforces Global Round 13
    Codeforces Round #673 (Div. 1)
  • 原文地址:https://www.cnblogs.com/wangbin2188/p/14889507.html
Copyright © 2011-2022 走看看