Springboot using session container to access SMS verification code

Source: Internet
Author: User

Springboot Use the session container to access the SMS Verification Code , (hand shake dug pit in the @WebListener class declaration when added an abstract cause springboot scan not to this listener )

requirements: in order to give the app interface, in the session to access the SMS Verification Code, interface speed and good swagger test from the session to take SMS verification Code information 66 No problem ( because in the browser-scoped access, Access when the request head automatically with SessionID so no problem ), when the postman test to get the session content is null (the reason is like mobile phone app or postman request when not with SessionID, So let the server give you the session data and it doesn't know which box to take the data from and can't get the data .

Implementation results: In the Springboot environment, the custom session container is implemented by SessionID to get the session

App phone verification code access two ways:

Mode one: Database or Redis, such as cache access, slightly.

Mode two: Session access is as follows, note: Do not delete

Words not much to say, directly on the code:

1. Add scanning annotations to the start-up class head:

@ServletComponentScan

2. Session Container Class

1  PackageCom.xxx.api.listener;2 3 Importjavax.servlet.http.HttpSession;4 ImportJava.util.HashMap;5 6 /**7 * @Auther:8 * @Date: 2018/6/22 18:589 * @Description:Ten  */ One  Public classSessioncontext { A     Private StaticSessioncontext instance; -     PrivateHashMap Mymap; -  the     PrivateSessioncontext () { -Mymap =NewHashMap (); -     } -  +      Public StaticSessioncontext getinstance () { -         if(Instance = =NULL) { +Instance =NewSessioncontext (); A         } at         returninstance; -     } -  -      Public synchronized voidAddsession (HttpSession session) { -         if(Session! =NULL) { - Mymap.put (Session.getid (), session); in         } -     } to  +      Public synchronized voidDelsession (HttpSession session) { -         if(Session! =NULL) { the Mymap.remove (Session.getid ()); *         } $     }Panax Notoginseng  -      Public synchronizedHttpSession getsession (String session_id) { the         if(session_id = =NULL)return NULL; +         return(HttpSession) mymap.get (session_id); A     } the}

3, Httpsessionlistener monitoring class, when the session is generated when the session is added to the container, the emphasis here plus annotations

@WebListener
1  PackageCom.xxxxx.api.listener;2 3 ImportCom.xn.api.app.login.ApiLoginController;4 ImportOrg.slf4j.Logger;5 Importorg.slf4j.LoggerFactory;6 7 ImportJavax.servlet.annotation.WebListener;8 Importjavax.servlet.http.HttpSession;9 Importjavax.servlet.http.HttpSessionEvent;Ten ImportJavax.servlet.http.HttpSessionListener; One ImportJava.util.HashMap; A ImportJava.util.Map; -  - /** the * @Auther: - * @Date: 2018/6/22 18:54 - * @Description: -  */ + @WebListener -  Public classSessionlistenerImplementsHttpsessionlistener { +     protected StaticLogger Logger=loggerfactory.getlogger (Apilogincontroller.class); A  at  -      Public StaticMap UserMap =NewHashMap (); -     PrivateSessioncontext Sessioncontext =sessioncontext.getinstance (); -  -  -      Public voidsessioncreated (httpsessionevent httpsessionevent) { inHttpSession session =httpsessionevent.getsession (); -Logger.debug ("info------>sessioncreated----->sessionid:" +Session.getid ()); to Sessioncontext.addsession (session); +     } -  the      Public voidsessiondestroyed (httpsessionevent httpsessionevent) { *HttpSession session =httpsessionevent.getsession (); $Logger.debug ("info------>sessiondeath----->sessionid:" +httpsessionevent.getsession (). GetId ());Panax Notoginseng Sessioncontext.delsession (session); -     } the  +}

4, access to normal access mode access can be

HttpSession HttpSession = request.getsession (); // This sessionid is returned to the app, allowing the session to be retrieved for the next authentication request based on SessionID String sessionId =  Httpsession.getid (); // httpSession Access Data httpsession.setattribute ("Phone", "Verification Code");

5, according to SessionID get session

Custom session Container Sessioncontext sessioncontext= sessioncontext.getinstance ();//Get corresponding sessionhttpsession from the custom session container Session = Sessioncontext.getsession (SESSIONID); String Verification Code = string.valueof (Session.getattribute ("Phone"));

Complete.

Springboot using session container to access SMS verification code

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.