Third-party logins under Javaweb application

Source: Internet
Author: User

Recent projects related to the site of third-party login, summed up the next found that some of the local configuration is still very troublesome, so the process is posted for your reference.

Baidu Developer Platform

For general applications, the use of a platform such as Baidu Open service platform to provide social services to meet the needs of users daily use.
First Open:
Baidu Open Service Platform
In the top right corner, click: Admin Console--Developer services Management
You can create your own project after entering the developer services management.
Build well as shown below:

Baidu will provide an API key and a key, this will be used for a while, first open show more-social services.

First enter the callback address settings, fill in the third-party login after the successful action address:

Note that the address here cannot be written as localhost and must be 127.0.0.1.
When you are finished, go back to the previous page and open the "PC-side JS component Settings".

Here you can set up a third-party login website and style, set up and click on the Generate page JS code.

<script type="Text/javascript" id="Bd_soc_login_boot"> </script><script type="Text/javascript">( function(){  vart =New Date(). GetTime (), script = document.getElementById ("Bd_soc_login_boot"), Redirect_uri =encodeURIComponent("Replace with URL after login succeeded"), Domid ="Replace the target node ID for the login component", src ="http://openapi.baidu.com/social/oauth/2.0/connect/login?redirect_uri="+ Redirect_uri +"&domid="+ Domid +"&client_type=web&response_type=code&media_types=sinaweibo%2Cqqdenglu%2Cbaidu%2Crenren& Size=-1&button_type=4&client_id=ozlmocp1zcue25hbrrr0ukrl&view=embedded&t= "+ t; SCRIPT.SRC = src;}) ();</script>

After generating the code, copy to the project's login page, and create a new div node under the Login box consistent with JS. The final style is as follows:

Below is the action after successful login:

 Public  class sociallogin extends httpservlet {    @Override    protected void Service(HttpServletRequest request, httpservletresponse response)throwsServletexception, IOException {HttpSession session = Request.getsession (); Userdao Userdao = (Userdao) daofactory.getinstance ("Userdao"); Userinfoservice Userinfoservice = (userinfoservice) servicefactory. getinstance ("Userinfoservice");//Receive CodeString code = Request.getparameter ("Code");//Send POST request, get token according to codemap<string, string> map =NewHashmap<string, string> (); Map.put ("Grant_type","Authorization_code"); Map.put ("Code", code); Map.put ("client_id","OZLMOCP1ZCUE25HBRRR0UKRL"); Map.put ("Client_secret","A08twipbc42uaj0eripvni1npdq3y5i8"); Map.put ("Redirect_uri","Http://127.0.0.1/LSZ/socialLogin"); String JSON = Httputils.postform ("Https://openapi.baidu.com/social/oauth/2.0/token", map);//TokenString Access_token = Jsonobject.fromobject (JSON). Get ("Access_token"). ToString ();//Get user informationString Json2 = httputils. Get ("https://openapi.baidu.com/social/api/2.0/user/info?access_token="+ Access_token +"&");//user nameString username = jsonobject.fromobject (json2). Get ("username"). ToString (); SYSTEM.OUT.PRINTLN (username);//Unique IDString Social_uid = Jsonobject.fromobject (Json2). Get ("Social_uid"). ToString (); System.out.println (SOCIAL_UID);/* * Boolean flag = Service.getuser (Integer.parseint (Social_uid));         * SYSTEM.OUT.PRINTLN (flag); */        //The following is the registration and login method of the project, which can be chosen to ignore        //if (flag) {User User =NewUser ();        User.setusername (username); UserService UserService = (userservice) servicefactory. getinstance ("UserService");Try{Boolean isregist = Userdao.finduser (username);if(isregist) {//Registered Direct loginuser = Userservice.login (username, Social_uid,1);intUsertype = User.getuserstate ();intu_id = user.getu_id ();                UserInfo UserInfo = Userinfoservice.ser_findinfo (u_id); Session.setattribute ("UserInfo", UserInfo); Session.setattribute ("u_id", u_id); Session.setattribute ("username", username); Session.setattribute ("Usertype", usertype); Response.sendredirect ("Index.do"); }Else{//Not registered first registration                BooleanIsRegist2 = userservice.regist (username, Social_uid,1);//When registration is complete, the user is logged in by defaultuser = Userservice.login (username, Social_uid,1);intu_id = user.getu_id ();BooleanIsintro =true;//Automatically generate a U_info record with u_id, which contains only u_id and i_id                BooleanIsinfo = Userinfoservice.ser_newuserinfo (u_id);//Get this record through u_idUserInfo UserInfo = Userinfoservice.ser_findinfo (u_id);if(IsRegist2 && isinfo && (userInfo! =NULL) {Session.setattribute ("Isintro", Isintro); Session.setattribute ("UserInfo", UserInfo); Session.setattribute ("username", username); Session.setattribute ("u_id", u_id); Response.sendredirect ("Index.do"); }            }        }Catch(Exception e) {//TODO auto-generated catch blockE.printstacktrace (); }//        //User.setsocial_uid (Integer.parseint (Social_uid));        //Service.saveuser (user);        //System.out.println ("Save user Information Success");        //} else {        //user user = Service.getuser2 (social_uid);        //System.out.println ("Read user information success");        // }}}

When a third party logs in, it generates a social_uid, unique, to authenticate the user, the social_uid can exist in a separate column in the user's table, allow the user to log on and reset the password, or the social_uid can be set directly to a password, but only third parties will be used in the future.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Third-party logins under Javaweb application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.