springboot security session redis使用(8)

1. 设置session的过期时间

server:
  servlet:
    session:
      timeout: 60

2.设置基于session的单点登录,超时后登录的网址,超时后的登录状态,

http.sessionManagement().invalidSessionUrl("/invalid").maximumSessions(1).maxSessionsPreventsLogin(false).expiredSessionStrategy(new CustomExpiredSessionStrategy());

CustomExpiredSessionStrategy

package com.hanhuide.core.handler;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hanhuide.core.model.CustomResponseBody;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class CustomExpiredSessionStrategy implements SessionInformationExpiredStrategy {
    private ObjectMapper objectMapper = new ObjectMapper();
    private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException, ServletException {
        event.getResponse().setContentType("application/json;charset=UTF-8");
//        CustomResponseBody body = new CustomResponseBody();
//        body.setStatus(0000);
//        body.setMsg("您已在其他地方登录,请检查,时间为{" + event.getSessionInformation().getLastRequest() + "}");
//        event.getResponse().getWriter().write(JSON.toJSONString(body));
        redirectStrategy.sendRedirect(event.getRequest(), event.getResponse(), "/login");
    }
}

3.

package com.hanhuide.core.controller;

import com.hanhuide.core.mapper.CeshiMapper;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.hanhuide.core.model.SysUser;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: maven
 * @description:
 * @author: 韩惠德
 * @create: 2019-12-24 16:41
 * @version: 1.0
 **/
@RestController
@Slf4j
public class Contrller11 {
    @Resource
    private CeshiMapper ceshiMapper;

    @ApiOperation(value = "测试数据源", notes = "测试数据源")
    @GetMapping("system")
    public List<SysUser> ceshi() {
        return ceshiMapper.findAll();
    }

    @ApiOperation(value = "测试数据源2", notes = "测试数据源2")
    @GetMapping("/system/menu")
    public List<SysUser> ceshi2() {
        return ceshiMapper.findAll2();
    }

    @RequestMapping("/invalid")
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public String invalid() {
        return "Session 已过期,请重新登录";
    }
}

 刷新火狐浏览器 直接跳转到登录页面

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
SpringBoot是一个开源的Java框架,简化了基于Java的应用程序的开发过程。它提供了各种开箱即用的功能和工具,使开发人员能够更加容易地构建可靠、高效和可扩展的应用程序。 JWT(JSON Web Token)是一种用于在网络应用间传递认证和授权信息的开放标准。使用JWT可以实现无状态的身份验证,避免了使用传统的session和cookie的麻烦。 SecuritySpring框架提供的身份验证和授权的模块,它提供了一套强大且灵活的安全机制可以应用于Spring应用中。 Redis是一个开源的内存数据存储系统,常用于缓存、消息队列等场景。它支持多种数据结构,如字符串、列表、哈希、集合等,并提供了丰富的数据操作命令。 脚手架是一个预先配置好的项目模板,可以帮助开发人员快速搭建项目骨架,包括项目结构、依赖库、配置文件等。 基于SpringBoot、JWT、SecurityRedis的脚手架可以提供一个快速开发安全可靠的Java应用的基础框架。 脚手架的主要功能包括: 1. 用户注册、登录和注销功能,使用JWT进行身份验证和授权。 2. 用户角色和权限管理,保护资源的访问权限。 3. 使用Redis作为缓存,提高系统性能和响应速度。 4. 集成其他常用的功能模块,如日志记录、错误处理、数据库操作等。 5. 提供项目结构、配置文件和依赖库的预配置,开发人员可以基于此快速开始项目开发。 使用该脚手架可以大大减少项目搭建的时间和精力,提高开发效率和代码质量。开发人员可以专注于业务逻辑的实现,而无需关心底层的技术细节。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值