Springboot without session development

Source: Internet
Author: User
Tags uuid

The session child Web project has been in existence since it appeared.

Recently in the development of the cluster project, think of session processing, between a session before doing a cluster processing

Spring-session-data-redis

This is a session management integrated with spring + Redis.

It's good to use, but it's a little inconvenient for managing the session.

So, I wrote a session to manage it myself.

Springboot Interceptor + Redis to intercept requests.

1. Determine if you are logged in

 Public Static BooleanIsLogin (HttpServletRequest request) {//Get Cookie valueString Cookievalue =Cookieutil.getcookievalue (Request, zipper_token_web_cookie_name); //The cookie value does not exist, indicating that it is not logged        if(Stringutil.isnullorempty (cookievalue)) {return false; }        //verifying the presence of a token value in a Redis expiresredisutil.strings s = redisutil.getinstance ().NewStrings (); if(!s.exists (Db_index, Gettokenexpireskey (cookievalue))) {            return false; }        //Update token time        LongR1 =S.expire (Db_index, Gettokenexpireskey (cookievalue), time_out); if(R1 = = 0) {            //hit failed, re-added cache valueS.setex (Db_index, Gettokenexpireskey (Cookievalue), Time_out, "");                } s.expire (Db_index, Gettokenkey (cookievalue), time_out_data); return true; }

2. User login, create token

 Public StaticString Createtoken (Usermodel record, list<menumodel>menulist) {        //Generate TokensUUID UUID =Uuid.randomuuid (); String token=uuid.tostring (); Redisutil.strings s= Redisutil.getinstance ().NewStrings (); Redisutil.hash h= Redisutil.getinstance ().NewHash (); //Set Expire tokenS.setex (Db_index, Gettokenexpireskey (token), time_out, ""); //set up data tokenString Tokenkey =Gettokenkey (token); Loginuser Loginuser=NewLoginuser (record);        H.hset (Db_index, Tokenkey, Login_user, loginuser.tostring ()); //Depending on the known code, menulist may be null        if(NULL!=menulist)        {H.hset (Db_index, Tokenkey, Menu_list, jsonobject.tojsonstring (menulist));                } s.expire (Db_index, Tokenkey, Time_out_data); //Return token        returntoken; }

After the user logs in successfully, the token exists in the cookie.

// Create token        String token = W.createtoken (Usermodel, menulist);         // Set Cookies         Cookieutil.setcookie (Request, Response,                w.zipper_token_web_cookie_name,                TOKEN, W.zipper_token_ Web_cookie_time_out);

3. User logoff, delete token

/**Delete according to token*/     Public Static BooleanDeletetoken (HttpServletRequest request) {//Get Cookie valueString Cookievalue =Cookieutil.getcookievalue (Request, zipper_token_web_cookie_name); if(Stringutil.isnullorempty (cookievalue)) {//token does not exist            return true; }                //Delete expire tokensredisutil.strings s = redisutil.getinstance ().NewStrings (); Long R=S.del (Db_index, Gettokenexpireskey (Cookievalue)); //Delete Data tokenRedisutil.hash h = redisutil.getinstance ().NewHash ();                H.hdel (Db_index, Gettokenkey (Cookievalue)); return(r = = 1); }

User logoff, delete token information

W.deletetoken (Request);

4. Get user information, menu information, etc.

/**Get login User Information*/     Public Staticloginuser Getloginuser (httpservletrequest request) {//Get Cookie valueString Cookievalue =Cookieutil.getcookievalue (Request, zipper_token_web_cookie_name); if(Stringutil.isnullorempty (cookievalue)) {//token does not exist            return NULL; } redisutil.hash H= Redisutil.getinstance ().NewHash (); String value=H.hget (Db_index, Gettokenexpireskey (cookievalue), login_user); if(Stringutil.isnullorempty (value)) {return NULL; }        returnJsonobject.parseobject (value, Loginuser.class); }        /**Get login User menu Information*/     Public StaticList<menumodel>getmenulist (HttpServletRequest request) {//Get Cookie valueString Cookievalue =Cookieutil.getcookievalue (Request, zipper_token_web_cookie_name); if(Stringutil.isnullorempty (cookievalue)) {//token does not exist            return NULL; } redisutil.hash H= Redisutil.getinstance ().NewHash (); String value=H.hget (Db_index, Gettokenkey (cookievalue), menu_list); if(Stringutil.isnullorempty (value)) {return NULL; }        returnJsonobject.parsearray (value, Menumodel.class); }

Redis Storage Fabric

In this way, a custom no-session management system has been developed.

Se7end

Statement: This blog article for the original, only represents my work in the study in a certain time to summarize the views or conclusions. When reprinted, please give the original link in the obvious position of the article page.

Springboot without session development

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.