欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot+SpringSession+Redis實(shí)現(xiàn)session共享及唯一登錄示例

 更新時間:2021年04月29日 16:45:18   作者:保爾-科查筋  
這篇文章主要介紹了SpringBoot+SpringSession+Redis實(shí)現(xiàn)session共享及唯一登錄示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

最近在學(xué)習(xí)springboot,session這個點(diǎn)一直困擾了我好久,今天把這些天踩的坑分享出來吧,希望能幫助更多的人。

一、pom.xml配置 

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
 
<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
</dependency>

二、application.properties的redis配置

#redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=123456
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
#超時一定要大于0
spring.redis.timeout=3000
spring.session.store-type=redis

在配置redis時需要確保redis安裝正確,并且配置notify-keyspace-events Egx,spring.redis.timeout設(shè)置為大于0,我當(dāng)時這里配置為0時springboot時啟不起來。

三、編寫登錄狀態(tài)攔截器RedisSessionInterceptor

//攔截登錄失效的請求
public class RedisSessionInterceptor implements HandlerInterceptor
{
    @Autowired
    private StringRedisTemplate redisTemplate;
 
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
    {
        //無論訪問的地址是不是正確的,都進(jìn)行登錄驗(yàn)證,登錄成功后的訪問再進(jìn)行分發(fā),404的訪問自然會進(jìn)入到錯誤控制器中
        HttpSession session = request.getSession();
        if (session.getAttribute("loginUserId") != null)
        {
            try
            {
                //驗(yàn)證當(dāng)前請求的session是否是已登錄的session
                String loginSessionId = redisTemplate.opsForValue().get("loginUser:" + (long) session.getAttribute("loginUserId"));
                if (loginSessionId != null && loginSessionId.equals(session.getId()))
                {
                    return true;
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
 
        response401(response);
        return false;
    }
 
    private void response401(HttpServletResponse response)
    {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
 
        try
        {
            response.getWriter().print(JSON.toJSONString(new ReturnData(StatusCode.NEED_LOGIN, "", "用戶未登錄!")));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
 
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception
    {
 
    }
 
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception
    {
 
    }
}

四、配置攔截器

@Configuration
public class WebSecurityConfig extends WebMvcConfigurerAdapter
{
    @Bean
    public RedisSessionInterceptor getSessionInterceptor()
    {
        return new RedisSessionInterceptor();
    }
 
    @Override
    public void addInterceptors(InterceptorRegistry registry)
    {
        //所有已a(bǔ)pi開頭的訪問都要進(jìn)入RedisSessionInterceptor攔截器進(jìn)行登錄驗(yàn)證,并排除login接口(全路徑)。必須寫成鏈?zhǔn)?,分別設(shè)置的話會創(chuàng)建多個攔截器。
        //必須寫成getSessionInterceptor(),否則SessionInterceptor中的@Autowired會無效
        registry.addInterceptor(getSessionInterceptor()).addPathPatterns("/api/**").excludePathPatterns("/api/user/login");
        super.addInterceptors(registry);
    }
}

五、登錄控制器

@RestController
@RequestMapping(value = "/api/user")
public class LoginController
{
    @Autowired
    private UserService userService;
 
    @Autowired
    private StringRedisTemplate redisTemplate;
 
    @RequestMapping("/login")
    public ReturnData login(HttpServletRequest request, String account, String password)
    {
        User user = userService.findUserByAccountAndPassword(account, password);
        if (user != null)
        {
            HttpSession session = request.getSession();
            session.setAttribute("loginUserId", user.getUserId());
            redisTemplate.opsForValue().set("loginUser:" + user.getUserId(), session.getId());
 
            return new ReturnData(StatusCode.REQUEST_SUCCESS, user, "登錄成功!");
        }
        else
        {
            throw new MyException(StatusCode.ACCOUNT_OR_PASSWORD_ERROR, "賬戶名或密碼錯誤!");
        }
    }
 
    @RequestMapping(value = "/getUserInfo")
    public ReturnData get(long userId)
    {
        User user = userService.findUserByUserId(userId);
        if (user != null)
        {
            return new ReturnData(StatusCode.REQUEST_SUCCESS, user, "查詢成功!");
        }
        else
        {
            throw new MyException(StatusCode.USER_NOT_EXIST, "用戶不存在!");
        }
    }
}

六、效果

我在瀏覽器上登錄,然后獲取用戶信息,再在postman上登錄相同的賬號,瀏覽器再獲取用戶信息,就會提示401錯誤了,瀏覽器需要重新登錄才能獲取得到用戶信息,同樣,postman上登錄的賬號就失效了。

瀏覽器:

postman:

七、核心原理詳解

分布式session需要解決兩個難點(diǎn):1、正確配置redis讓springboot把session托管到redis服務(wù)器。2、唯一登錄。

1、redis:

redis需要能正確啟動到出現(xiàn)如下效果才證明redis正常配置并啟動

同時還要保證配置正確

@EnableCaching
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 30)//session過期時間(秒)
@Configuration
public class RedisSessionConfig
{
    @Bean
    public static ConfigureRedisAction configureRedisAction()
    {
        //讓springSession不再執(zhí)行config命令
        return ConfigureRedisAction.NO_OP;
    }
}

springboot啟動后能在redis上查到緩存的session才能說明整個redis+springboot配置成功!

2、唯一登錄:

1、用戶登錄時,在redis中記錄該userId對應(yīng)的sessionId,并將userId保存到session中。

HttpSession session = request.getSession();
session.setAttribute("loginUserId", user.getUserId());
redisTemplate.opsForValue().set("loginUser:" + user.getUserId(), session.getId());

2、訪問接口時,會在RedisSessionInterceptor攔截器中的preHandle()中捕獲,然后根據(jù)該請求發(fā)起者的session中保存的userId去redis查當(dāng)前已登錄的sessionId,若查到的sessionId與訪問者的sessionId相等,那么說明請求合法,放行。否則拋出401異常給全局異常捕獲器去返回給客戶端401狀態(tài)。

唯一登錄經(jīng)過我的驗(yàn)證后滿足需求,暫時沒有出現(xiàn)問題,也希望大家能看看有沒有問題,有的話給我點(diǎn)好的建議!

到此這篇關(guān)于SpringBoot+SpringSession+Redis實(shí)現(xiàn)session共享及唯一登錄示例的文章就介紹到這了,更多相關(guān)SpringBoot 唯一登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring Boot中使用Actuator的/info端點(diǎn)輸出Git版本信息

    Spring Boot中使用Actuator的/info端點(diǎn)輸出Git版本信息

    這篇文章主要介紹了Spring Boot中使用Actuator的/info端點(diǎn)輸出Git版本信息,需要的朋友可以參考下
    2017-06-06
  • Java中的強(qiáng)引用,軟引用,弱引用,虛引用的作用介紹

    Java中的強(qiáng)引用,軟引用,弱引用,虛引用的作用介紹

    這篇文章主要介紹了Java中的強(qiáng)引用,軟引用,弱引用,虛引用的作用,下文內(nèi)容具有一定的知識參考價值,需要的小伙伴可以參考一下,希望對你有所幫助
    2022-02-02
  • Windows安裝Maven并配置環(huán)境變量

    Windows安裝Maven并配置環(huán)境變量

    Maven是一個基于Java的項(xiàng)目管理工具,可以管理項(xiàng)目的構(gòu)建、測試和部署,本文就來介紹一下Windows安裝Maven并配置環(huán)境變量,感興趣的可以了解一下
    2024-11-11
  • Java中的Timer與TimerTask源碼及使用解析

    Java中的Timer與TimerTask源碼及使用解析

    這篇文章主要介紹了Java中的Timer與TimerTask源碼及使用解析,在Java中,經(jīng)常使用Timer來定時調(diào)度任務(wù),Timer調(diào)度任務(wù)有一次性調(diào)度和循環(huán)調(diào)度,循環(huán)調(diào)度有分為固定速率調(diào)度(fixRate)和固定時延調(diào)度(fixDelay),需要的朋友可以參考下
    2023-10-10
  • 使用java項(xiàng)目搭建一個netty服務(wù)

    使用java項(xiàng)目搭建一個netty服務(wù)

    這篇文章主要為大家詳細(xì)介紹了如何使用java項(xiàng)目搭建一個netty服務(wù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-10-10
  • SpringBoot中實(shí)現(xiàn)接收文件和對象

    SpringBoot中實(shí)現(xiàn)接收文件和對象

    這篇文章主要介紹了SpringBoot實(shí)現(xiàn)接收文件和對象,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • java 獲取數(shù)據(jù)庫連接的實(shí)現(xiàn)代碼

    java 獲取數(shù)據(jù)庫連接的實(shí)現(xiàn)代碼

    本篇文章是對在java中獲取數(shù)據(jù)庫連接的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • 詳解spring mvc對異步請求的處理

    詳解spring mvc對異步請求的處理

    spring mvc3.2及以上版本增加了對請求的異步處理,是在servlet3的基礎(chǔ)上進(jìn)行封裝的,有興趣的可以了解一下。
    2017-01-01
  • 淺談JSP與Servlet傳值及對比(總結(jié))

    淺談JSP與Servlet傳值及對比(總結(jié))

    下面小編就為大家?guī)硪黄獪\談JSP與Servlet傳值及對比(總結(jié))。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • 解決idea 通過build project 手動觸發(fā)熱部署失敗的問題

    解決idea 通過build project 手動觸發(fā)熱部署失敗的問題

    在debug運(yùn)行項(xiàng)目的過程中,并且保證(不添加方法,不修改方法名)一定的規(guī)則的情況下,可以通過build project 來手動熱部署項(xiàng)目,本文給大家介紹解決idea 通過build project 手動觸發(fā)熱部署失敗的問題,感興趣的朋友一起看看吧
    2023-12-12

最新評論