Springboot使用session容器存取短信验证码

Springboot使用session容器存取短信验证码,(手抖挖了坑在@WebListener类声明时候加了个abstract导致springboot扫描不到这个监听器

需求:为了给app做接口,在session中存取短信验证码,接口速速撸好swagger测试从session中拿短信验证码信息66的没问题(因为在浏览器范围访问,访问的时候请求头自动带着sessionid的所以没问题),当用postman测试拿到的session内容是null(原因是像手机app或者postman请求时候并没有带sessionid,所以让服务端给你取session数据它并不知道从哪个盒子里拿数据故而拿不到数据

实现结果:在springboot环境中,自定义session容器实现通过sessionId获取session

app手机验证码存取两种方式:

方式一:数据库或者Redis等缓存存取,略。

方式二:session存取如下,注:没有做删除

话不多说,直接上代码:

1、在启动类头上加扫描注解:

@ServletComponentScan

2、session容器类

 1 package com.xxx.api.listener;
 2 
 3 import javax.servlet.http.HttpSession;
 4 import java.util.HashMap;
 5 
 6 /**
 7  * @Auther: 
 8  * @Date: 2018/6/22 18:58
 9  * @Description:
10  */
11 public class SessionContext {
12     private static SessionContext instance;
13     private HashMap mymap;
14 
15     private SessionContext() {
16         mymap = new HashMap();
17     }
18 
19     public static SessionContext getInstance() {
20         if (instance == null) {
21             instance = new SessionContext();
22         }
23         return instance;
24     }
25 
26     public synchronized void AddSession(HttpSession session) {
27         if (session != null) {
28             mymap.put(session.getId(), session);
29         }
30     }
31 
32     public synchronized void DelSession(HttpSession session) {
33         if (session != null) {
34             mymap.remove(session.getId());
35         }
36     }
37 
38     public synchronized HttpSession getSession(String session_id) {
39         if (session_id == null) return null;
40         return (HttpSession) mymap.get(session_id);
41     }
42 }

3、HttpSessionListener监听类,当session产生的时候把session加入到容器里,重点这里加上注解

@WebListener
 1 package com.xxxxx.api.listener;
 2 
 3 import com.xxxxx.api.app.login.ApiLoginController;
 4 import org.slf4j.Logger;
 5 import org.slf4j.LoggerFactory;
 6 
 7 import javax.servlet.annotation.WebListener;
 8 import javax.servlet.http.HttpSession;
 9 import javax.servlet.http.HttpSessionEvent;
10 import javax.servlet.http.HttpSessionListener;
11 import java.util.HashMap;
12 import java.util.Map;
13 
14 /**
15  * @Auther: 
16  * @Date: 2018/6/22 18:54
17  * @Description:
18  */
19 @WebListener
20 public class SessionListener implements HttpSessionListener {
21     protected static Logger logger=LoggerFactory.getLogger(ApiLoginController.class);
22 
23 
24     public static Map userMap = new HashMap();
25     private SessionContext sessionContext = SessionContext.getInstance();
26 
27 
28     public void sessionCreated(HttpSessionEvent httpSessionEvent) {
29         HttpSession session = httpSessionEvent.getSession();
30         logger.debug("info------>sessionCreated----->sessionId:" + session.getId());
31         sessionContext.AddSession(session);
32     }
33 
34     public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
35         HttpSession session = httpSessionEvent.getSession();
36         logger.debug("info------>sessionDeath----->sessionId:" + httpSessionEvent.getSession().getId());
37         sessionContext.DelSession(session);
38     }
39 
40 }

4、存取正常存取方式存取就可以

HttpSession httpSession = request.getSession();
//这个sessionId返回给APP,便于下次验证请求时获取session能根据sessionId获取
String sessionId = httpSession.getId();
//httpsession存取数据
httpSession.setAttribute("电话", "验证码");

 

5、根据sessionId获取session

//自定义session容器
SessionContext sessionContext= SessionContext.getInstance();
//从自定义session容器中拿到对应session
HttpSession session = sessionContext.getSession(sessionId);
String 验证码= String.valueOf(session.getAttribute("电话"));

 

完毕。

================================================================================================

附:注册的方式向springboot注入Listener

 1 package com.xxxxx.api.config;
 2 
 3 import com.xxxxxxx.api.listener.SessionListener;
 4 import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
 5 import org.springframework.context.annotation.Bean;
 6 import org.springframework.context.annotation.Configuration;
 7 
 8 /**
 9  * @Auther: 
10  * @Date: 2018/6/23 09:59
11  * @Description:
12  */
13 @Configuration
14 public class ListenerConfigure {
15     @Bean
16     public ServletListenerRegistrationBean<SessionListener> serssionListenerBean(){
17         ServletListenerRegistrationBean<SessionListener>
18                 sessionListener = new ServletListenerRegistrationBean<SessionListener>(new SessionListener());
19         return sessionListener;
20     }
21 
22 }

 

posted @ 2018-06-23 10:40  awei4home  阅读(3914)  评论(0编辑  收藏  举报