sign in

Single sign-on: Single sign-on means that after logging in at one place, the login information can also be used in other domains. Single sign-on uses jsonP to achieve cross-domain access.


jsonP: javascript dynamically generates a 'script' tag in the html page, and takes the parameters we want to send to other domain names behind the src url by GET method, and other domains wrap the processed data in the way of js function call. Return to our website domain name


The basic idea of ​​single sign-on: use the Token after the user logs in successfully as the key value, the user's id or the json string of the user object as the value and store it in redis, and then store the token as the value in the cookie (for example, the name is User) , use jsonP to get the name of the cookie, pass the name of the cookie to the background and then get the user information in Redis

How JsonP implements cross-domain access: Initiate an ajax request and configure a dataType:'jsonp' to initiate a cross-domain request. jsonp specifies that the data type returned by the server is in jsonp format. You can see the request path initiated, and automatically bring a callback=xxx, where xxx is the name of a callback function randomly generated by jquery.

code show as below

< script > 
7  
8          $(document).ready( function () {
 9  
10              $( " #btn " ).click( function () {
 11  
12                  $.ajax({
 13                      url: " http://localhost:9090 /student " ,
 14                      type: " GET " ,
 15                      dataType: " jsonp " , // Specify the data type returned by the server 
16                      success:function (data) {
 17                          var result = JSON.stringify(data); // convert json object to string 
18                          $( " #text " ).val(result);
 19                      }
 20                  });
 21  
22              });
 23  
24          });
 25      </ script >



server code

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 2     response.setCharacterEncoding("UTF-8");
 3     response.setContentType("text/html;charset=UTF-8");
 4 
 5     //数据
 6     List<Student> studentList = getStudentList();
 7 
 8 
 9     JSONArray jsonArray = JSONArray.fromObject(studentList);
10     String result = jsonArray.toString();
11 
12     //前端传过来的回调函数名称
13     String callback = request.getParameter("callback" );
 14      // Wrap the returned data with the name of the callback function, so that the returned data is passed back as the parameter of the callback function 
15      result = callback + "(" + result + ")" ;
 16  
17      response.getWriter().write(result);
 18 }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325446275&siteId=291194637