整合JPA、Redis实现分布式的Session共享

一.SpringBoot整合JPA、Redis实现分布式的Session共享

1. 创建web项目

我们按照之前的经验,创建一个web程序,并将之改造成Spring Boot项目,具体过程略。

2.添加依赖包

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- 改造:添加sql相关的依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <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>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.39</version>
        </dependency>

3.创建application.yml配置文件

server:
  port: 8080
  servlet:
    #设置session过期时间
    session:
      timeout: "PT10M"
spring:
  application:
    name: spring-session-jpa
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: syc
    url: jdbc:mysql://localhost:3306/spring-security?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false&serverTimezone=UTC
  redis:
    host: localhost
    port: 6379
    database: 0
    jedis:
      pool:
        max-idle: 8
        min-idle: 0
        max-active: 8
  session:
    #设置session存储类型
    store-type: redis
  jpa:
    database: mysql
    show-sql: true #开发阶段,打印要执行的sql语句.
    hibernate:
      ddl-auto: update

4.创建User实体类

package com.yyg.boot.entity;
 
import ch.qos.logback.classic.db.names.TableName;
import lombok.Data;
import lombok.ToString;
import org.hibernate.annotations.GenericGenerator;
 
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
 
/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/29
 * @Description Description
 */
@Entity
@Table(name="user")
@Data
@ToString
public class User implements Serializable {
 
    @Id
    @GeneratedValue(generator = "idGenerator",strategy = GenerationType.IDENTITY)
    private Long id;
 
    @Column(name = "username")
    @NotBlank(message = "username can not be empty")
    private String username;
 
    @Column(name = "password")
    @NotBlank(message = "password can not be empty")
    private String password;
 
}

5.创建UserRepository仓库类

package com.yyg.boot.repository;
 
import com.yyg.boot.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
 
/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/29
 * @Description Description
 */
public interface UserRepository extends JpaRepository<User,Long> {
 
    User findByUsername(String username);
 
    User findByUsernameAndPassword(String username,String password);
 
}

6.创建Service实现类

6.1 创建UserService接口

package com.yyg.boot.service;
 
import com.yyg.boot.entity.User;
 
/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/29
 * @Description Description
 */
public interface UserService {
    /**
     * save user
     */
    User saveUser(User user);
 
    /**
     * find user by account
     */
    User findByUsername(String username);
 
    /**
     * user login
     */
    User login(String username, String password);
 
}

6.2 创建UserServiceImpl实现类

package com.yyg.boot.service.impl;
 
import com.yyg.boot.entity.User;
import com.yyg.boot.repository.UserRepository;
import com.yyg.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/29
 * @Description Description
 */
@Service
public class UserServiceImpl implements UserService {
 
    @Autowired
    private UserRepository userRepository;
 
    @Override
    public User saveUser(User user) {
        return userRepository.save(user);
    }
 
    @Override
    public User findByUsername(String username) {
        return userRepository.findByUsername(username);
    }
 
    @Override
    public User login(String username, String password) {
        return userRepository.findByUsernameAndPassword(username, password);
    }
 
}

7.创建UserController接口

package com.yyg.boot.web;
 
import com.yyg.boot.entity.User;
import com.yyg.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpSession;
 
/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/29
 * @Description Description
 */
@RestController
@RequestMapping("/user")
public class UserController {
 
    @Autowired
    private UserService userService;
 
    /**
     * save user
     */
    @PostMapping(value = "/save")
    public User save(@RequestBody User user) {
        return userService.saveUser(user);
    }
 
    /**
     * find user by username
     */
    @GetMapping(value = "/find/{username}")
    public User findUser(@PathVariable String username) {
        return userService.findByUsername(username);
    }
 
    /**
     * user login
     */
    @GetMapping(value = "/login")
    public User login(String username, String password, HttpSession session) {
        User user = userService.login(username, password);
        if(user!=null){
            session.setAttribute(session.getId(),user);
        }
        return user;
    }
 
    @GetMapping(value = "/logout")
    public String logout(HttpSession session){
        session.removeAttribute(session.getId());
        return "user logout success";
    }
 
}

8.添加拦截器,判断session是否有效

package com.yyg.boot.config;
 
import com.alibaba.fastjson.JSON;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
 
/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/28
 * @Description 开启Redis Http Session
 */
@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800)
public class RedisHttpSessionConfiguration implements WebMvcConfigurer {
 
    /**
     * 添加过滤接口
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SecurityInterceptor())
                //排除拦截
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/user/logout")
                //拦截路径
                .addPathPatterns("/**");
    }
 
    /**
     * 判断session是否为空,从而进行拦截放行
     */
    @Configuration
    public class SecurityInterceptor implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
            HttpSession session = request.getSession();
            if (session.getAttribute(session.getId()) != null) {
                return true;
            }
            response.getWriter().write(JSON.toJSONString("please login first"));
            return false;
        }
    }
 
}

9.创建入口类

package com.yyg.boot;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/28
 * @Description Description
 */
@SpringBootApplication
public class SpringSessionJpaApplication {
 
    public static void main(String[] args){
        springapplication.run - 这个网站可出售。 - 最佳的springapplication 来源和相关信息。(SpringSessionJpaApplication.class,args);
    }
 
}

10.完整项目结构

11.启动项目,进行测试

我们在未登录的情况下,直接根据用户名查询某个用户信息,会看到浏览器提示我们先要进行登录。

然后测试save接口,会发现也是提示先进行登录才可以。

此时我们进行登录,登录成功

然后我们再进行save或find接口的测试,会发现此时这些接口都已经可以正常访问了。

数据库中已经多了一个新的数据。

然后我们再退出登录

然后重新测试查询接口,会发现已经无法查询用户信息了,提示需要进行登录才可以。

再重新登录后,我们可以再启动8081这个新的进程,然后查询tom这个用户信息,会发现也查询到了tom的用户信息,说明我们已经实现了session的共享。

redis中也有了保存的session信息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值