# springboot-session-redis **Repository Path**: linestyle007/springboot-session-redis ## Basic Information - **Project Name**: springboot-session-redis - **Description**: springboot session一致性 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-05-29 - **Last Updated**: 2021-11-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README spring-boot-session一致性 ## 准备工作 ### 配置nginx #### 安装nginx [下载地址]: http://nginx.org/en/download.html #### 环境配置 * 配置负载均衡 ```scala #最好使用服务器的私有IP以获得更好的性能和安全性。 http { upstream backend { server 127.0.0.1:9001; server 127.0.0.1:9002; } #该服务器接受到端口80的所有流量并将其传递给上游upstream 。 #请注意,upstream名称和proxy_pass需要匹配。 server { listen 8080; location / { proxy_pass http://backend; } } } ``` ### 配置redis [下载地址]: https://redis.io/download ### 配置tomcat [下载地址]: https://tomcat.apache.org/download-8.cgi ```scala 1.下载tomcat,copy两份 => tomcat-端口 /opt/tomcat-9001 /opt/tomcat-9002 2.启动和停止tomcat # start /opt/tomcat-9001/startup.sh /opt/tomcat-9002/startup.sh # shutdown /opt/tomcat-9001/shutdown.sh /opt/tomcat-9002/shutdown.sh ``` ### 配置springboot #### 配置pom ```xml org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-redis org.springframework.session spring-session-data-redis org.springframework.boot spring-boot-starter-thymeleaf ``` #### 配置redis ```properties #Redis服务器地址 spring.redis.host=127.0.0.1 #Redis服务器连接端口 spring.redis.port=6379 #Redis服务器连接密码(默认为空) spring.redis.password= #Session保存在Redis里 spring.session.store-type=Redis ``` #### 配置thymeleaf ```properties #thymelea spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.mode=HTML5 spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.cache=false ``` #### 配置登录拦截器 ```java class LoginInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { try { //统一拦截(查询当前session是否存在user)(这里user会在每次登陆成功后,写入session) User user=(User)request.getSession().getAttribute("user"); if(user != null){ return true; } response.sendRedirect(request.getContextPath()+"/login"); } catch (IOException e) { e.printStackTrace(); } return false;//如果设置为false时,被请求时,拦截器执行到此处将不会继续操作 //如果设置为true时,请求将会继续执行后面的操作 } } @Configuration class MvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { // 注册TestInterceptor拦截器 InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor()); // 所有路径都被拦截 registration.addPathPatterns("/**"); // 添加不拦截路径(登录,首页) registration.excludePathPatterns( "/login", //登录 "/", //首页 ); } } ``` #### 配置war包部署 * pom.xml * 配置tomcat运行方式 1.pom.xml ```xml org.springframework.boot spring-boot-starter-tomcat provided war app ... ``` 2.配置tomcat运行方式 ```java @SpringBootApplication public class SpringbootSessionRedisApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(SpringbootSessionRedisApplication.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringbootSessionRedisApplication.class); } } ``` ## 实现步骤 ### 打war包 ```scala 1.idea生成war包 2.copy到tomcat/webapps下 cp /Users/liuyao/IdeaProjects/springboot-session-redis/target/app.war /opt/tomcat9001/webapps/app.war cp /Users/liuyao/IdeaProjects/springboot-session-redis/target/app.war /opt/tomcat9002/webapps/app.war ``` ### 启动服务 ```scala 1.启动nginx $ brew services start nginx 2.启动redis $ brew services start redis 3.启动tomcat $ ./opt/tomcat9001/bin/startup.sh $ ./opt/tomcat9002/bin/startup.sh ``` ### 验证 1. 输入网址:http://localhost:8080/app/main 2. 登录:zhangsan/123 3. 新开一个窗口:http://localhost:8080/app/main 4. 退出,再验证另外一个窗口是否正常