ssm-百度第三方登录

    1.首先我们进入controller层login方法跳转login页面

      @RequestMapping("login")
      public ModelAndView login(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("../login");
        return mv;
      }  
2.然后进入login页面 
<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <script type="text/javascript"> function tolog(){ location='http://openapi.baidu.com/oauth/2.0/authorize?response_type=code&client_id=IDIhE9Fxo2AGexqEmIl3dBO7&redirect_uri=http://localhost:8080/baidu'; } </script> <body> <center> <button οnclick="tolog()">登录</button> </center> </body> </html>

3.通过链接去访问baidu,并带参数,通过client_id和redirect_url获取code,并通过回调url进入controller中的baidu方法

@RequestMapping("baidu")
public ModelAndView baidu(Code c) throws IOException {
    String code = c.getCode();
    System.out.println(code);

    HashMap<String, String> map = new HashMap<String,String>();
    map.put("grant_type", "authorization_code");
    map.put("code", code);
    map.put("client_id", "IDIhE9Fxo2AGexqEmIl3dBO7");
    map.put("client_secret", "O2Q6zS6sWYks1tTHKd65TslbsyVXDSgx");
    map.put("redirect_uri", "http://localhost:8080/baidu");
    String json = HttpUtils.postForm("https://openapi.baidu.com/oauth/2.0/token", map);
    System.out.println(json);
    ObjectMapper mapper = new ObjectMapper();
    BToken token = mapper.readValue(json, BToken.class);
    String access_token = token.getAccess_token();
    String json2 = HttpUtils.get("https://openapi.baidu.com/rest/2.0/passport/users/getLoggedInUser?access_token=" + access_token);
    System.out.println(json2);
    ObjectMapper mapper2 = new ObjectMapper();
    User user = mapper2.readValue(json2, User.class);
    user.setUname(new String(user.getUname().getBytes(),"utf-8"));
    ModelAndView mv = new ModelAndView();
    mv.addObject("user",user);
    mv.setViewName("success");
    return mv;
}

4.通过code及其他参数可以获取Access_Token,通过Access_Token可以获取用户信息
然后跳转到成功页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
   <center>
      <img src="http://tb.himg.baidu.com/sys/portrait/item/${user.portrait }"><br>
      <h3>${user.uid }</h3>
      <h3>${user.uname }</h3>
   </center>

</body>
</html>


5.pojo(附代码)

5.1 User

package com.jk.pojo;

import java.io.Serializable;

public class User implements Serializable{

   private String uid;
   private String uname;
   private String portrait;
   public String getUid() {
      return uid;
   }
   public void setUid(String uid) {
      this.uid = uid;
   }
   public String getUname() {
      return uname;
   }
   public void setUname(String uname) {
      this.uname = uname;
   }
   public String getPortrait() {
      return portrait;
   }
   public void setPortrait(String portrait) {
      this.portrait = portrait;
   }
}

5.2 Code

package com.jk.pojo;

import java.io.Serializable;

public class Code implements Serializable{

   private String code;

   public String getCode() {
      return code;
   }

   public void setCode(String code) {
      this.code = code;
   }
}

5.3 BToken

package com.jk.pojo;

import java.io.Serializable;

public class BToken implements Serializable{

   private Integer expires_in;
   private String refresh_token;
   private String access_token;
   private String session_secret;
   private String session_key;
   private String scope;
   private String error_code;
   private String error_msg;

   public String getError_msg() {
      return error_msg;
   }

   public void setError_msg(String error_msg) {
      this.error_msg = error_msg;
   }

   public String getError_code() {
      return error_code;
   }

   public void setError_code(String error_code) {
      this.error_code = error_code;
   }

   public Integer getExpires_in() {
      return expires_in;
   }

   public void setExpires_in(Integer expires_in) {
      this.expires_in = expires_in;
   }

   public String getRefresh_token() {
      return refresh_token;
   }

   public void setRefresh_token(String refresh_token) {
      this.refresh_token = refresh_token;
   }

   public String getSession_secret() {
      return session_secret;
   }

   public void setSession_secret(String session_secret) {
      this.session_secret = session_secret;
   }

   public String getSession_key() {
      return session_key;
   }

   public void setSession_key(String session_key) {
      this.session_key = session_key;
   }

   public String getScope() {
      return scope;
   }

   public void setScope(String scope) {
      this.scope = scope;
   }

   public String getAccess_token() {
      return access_token;
   }

   public void setAccess_token(String access_token) {
      this.access_token = access_token;
   }
}




 
 
 
 
 
发布了14 篇原创文章 · 获赞 92 · 访问量 1572

猜你喜欢

转载自blog.csdn.net/jn19970215/article/details/72191123