Single Sign-on

Source: Internet
Author: User

From a third-party system to a single point of access to the target system, the third-party system will send tokens for verification, by parsing tokens, to obtain the corresponding user information JSON string. Set it to the session of your system.

public class Sessionfilter extends onceperrequestfilter{//login page private String loginpage = Configure.getinstance (). Getp    Roperty ("com.zyzx.dmc.login.html"); @Override protected void dofilterinternal (HttpServletRequest request, httpservletresponse response, Filterchain filter    Chain) throws Servletexception, IOException {httpservletrequest hrequest = (httpservletrequest) request;        HttpSession session = Hrequest.getsession (); Non-filtered uri string[] notfilter = new string[] {"login.html", ". js", "/css", "/images", "/logout", "/druid", "/login"        , "/ssoauth"};        The URI of the request String URL = Request.getrequesturl (). toString ();        Token string token = Request.getparameter ("token");//String url = Uri.replaceall ("html", "Bak");        Whether to filter Boolean doFilter = true;                for (String s:notfilter) {if (Url.indexof (s)! =-1) {//If the URI contains a non-filtered URI, it is not filtered                DoFilter = false; Break; }}/* * if (uri.contains ("JSP") && uri.indexof ("login.jsp") = =-1) {DoFilter * = tr Ue } */if (doFilter) {//Execute filter//Get the login entity final iusersession from session Userse            Ssion = (iusersession) session.getattribute (iusersession.asia_session_name); if (usersession = = null) {//not logged on status if (null = = token) {Response.sendredire                    CT (loginpage + "? goto=" + URL);                Return                    Token exists to save session, verify user information}else {jsonobject result = Checktokeninfo (token);                        if (null = = result) {Response.sendredirect (LoginPage + "? goto=" + URL);                    Return                        }//Verify Success if ("Suc". Equals (Result.get ("result"))) {//Normal login Map<string,string> Sessionuserinfo = new hashmap<string, string> ();                        Usermessage usermessage = new Usermessage ();                        Sessionuserinfo = Jsonutil.json2hashmap (Result.get ("UserInfo"). toString ());                                                Sessionuserinfo.put ("token", token);                         String IP = request.getheader ("X-forwarded-for"); if (IP = = NULL | | ip.length () = = 0 | |                         "Unknown". Equalsignorecase (IP)) {IP = request.getheader ("Proxy-client-ip"); if (IP = = NULL | | ip.length () = = 0 | |                         "Unknown". Equalsignorecase (IP)) {IP = request.getheader ("Wl-proxy-client-ip"); if (IP = = NULL | | ip.length () = = 0 | |                         "Unknown". Equalsignorecase (IP)) {IP = request.getremoteaddr (); } Usermessage.setuserid(Sessionuserinfo.get ("User_account"));                        Usermessage.setusername (Sessionuserinfo.get ("user_name"));                        Usermessage.setsessionid (Sessionuserinfo.get ("token"));                        Usermessage.setclientip (IP);                        Request.getsession (). SetAttribute (Iusersession.asia_session_name,usermessage);                    Response.sendredirect (URL);                    }else if ("Fail". Equals (Result.get ("result"))) {Response.sendredirect (LoginPage + "? goto=" + URL); }}//If the login entity does not exist in the session, the popup prompts to log back in to the Boolean isajaxrequest                = Isajaxrequest (request); if (isajaxrequest) {//sets the character set of request and response, prevents garbled response.setcontenttype ("Text/ht                    Ml;charset=utf-8 ");                    Response.senderror (HttpStatus.UNAUTHORIZED.value (), "You have been too long to operate, please refresh the page");                Return }            } else {//If there is a login entity in the session, continue Filterchain.dofilter (request, response);        }} else {//If filtering is not performed, continue filterchain.dofilter (request, response); }}/** * To determine whether the AJAX request < feature description > * * @param request * @return is true, False * @see [Class, Class # method, Class # Member] */public static Boolean isajaxrequest (HttpServletRequest request) {String Header = Request.getheader        ("X-requested-with");        if (header = null && "XMLHttpRequest". Equals (header)) return true;    else return false; }/** * * Verify token exists * @param tokenvalue * @return * @throws IOException */Private Jsonob Ject Checktokeninfo (String tokenvalue) throws IOException {string checkurl = Configure.getinstance (). GetProperty ("        Xxxxxurl ") +tokenvalue;        HttpClient HttpClient = new HttpClient ();        GetMethod httpget = new GetMethod (Checkurl);  try {          Httpclient.executemethod (HttpGet);            String result = Httpget.getresponsebodyasstring ();            Jsonobject JSON = jsonobject.fromobject (result);        return JSON;        } finally {httpget.releaseconnection (); }    }    }

  

Single Sign-on

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.