当前位置 主页 > 网站技术 > 代码类 >

    Java实现QQ第三方登录的示例代码(4)

    栏目:代码类 时间:2019-11-08 15:05

    异常类QQStateErrorException:

    public class QQStateErrorException extends Exception {
      public QQStateErrorException() {
        super();
      }
     
      public QQStateErrorException(String message) {
        super(message);
      }
     
      public QQStateErrorException(String message, Throwable cause) {
        super(message, cause);
      }
     
      public QQStateErrorException(Throwable cause) {
        super(cause);
      }
     
      protected QQStateErrorException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
      }
    }

    首页controller用于跳转页面

    @Controller
    public class IndexController {
     
      @GetMapping({"/index", "/"})
      public String index(){
        return "index";
      }
     
      @GetMapping("/home")
      public String home(HttpSession session, Model model){
        String openid = (String) session.getAttribute("openid");
        String nickname = (String) session.getAttribute("nickname");
        String figureurl_qq_2 = (String) session.getAttribute("figureurl_qq_2");
     
        model.addAttribute("openid",openid);
        model.addAttribute("nickname",nickname);
        model.addAttribute("figureurl_qq_2",figureurl_qq_2);
     
        return "home";
      }
    }

    还有两个简单的登录页面和信息页面

    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Title</title>
    </head>
    <body>
      <a href="/qq/login" rel="external nofollow" >QQ登录</a>
    </body>
    </html>

    home.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Title</title>
    </head>
    <body>
    <div>
      <img th:src="${figureurl_qq_2}">
    </div>
    <span th:text="${openid}"></span>
    <span th:text="${nickname}"></span>
    </body>
    </html>

    最后附上下载地址:https://github.com/machaoyin/qqdemo

    以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持IIS7站长之家。