springsecurity设置用户登录状态过期,让其重新登录

手动程序让session过期,使用户重新登录

//判断是否过期
Object expireDate = redisTemplate.opsForValue().get(username+"expireDate");
if(expireDate != null && expireDate != ""){
    SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
        Date date = dateFormat.parse(expireDate.toString());
        Date currentTime = new Date();
        if(!currentTime.before(date)){
            //redisTemplate.delete(username+"expireDate");
            removeSession(username);
        }
    }catch (Exception e){
        throw new BadRequestException(e.getMessage());
    }

}
private void removeSession(String username) {
    for (Object userDetail : sessionRegistry.getAllPrincipals()) {
        String userName = ((org.springframework.security.core.userdetails.User) userDetail).getUsername();
        if (userName.equals(username)) {
            removeSession(userDetail);
        }
    }
}

private void removeSession(Object principal) {
    List<SessionInformation> sessionInformations = sessionRegistry.getAllSessions(principal, false);
    for (SessionInformation sessionInformation : sessionInformations) {
        sessionInformation.expireNow();
    }
}
发布了27 篇原创文章 · 获赞 0 · 访问量 807

猜你喜欢

转载自blog.csdn.net/u013232219/article/details/104486955