Commit a9a041f8 by naan1993

集成spring session

parent 92e9e66e
...@@ -40,6 +40,10 @@ ...@@ -40,6 +40,10 @@
<!--spring boot依赖--> <!--spring boot依赖-->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId> <artifactId>spring-boot-starter-aop</artifactId>
</dependency> </dependency>
<dependency> <dependency>
...@@ -60,11 +64,6 @@ ...@@ -60,11 +64,6 @@
<artifactId>spring-boot-starter-jdbc</artifactId> <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId> <artifactId>spring-boot-starter-validation</artifactId>
</dependency> </dependency>
...@@ -110,6 +109,15 @@ ...@@ -110,6 +109,15 @@
<!--其他依赖--> <!--其他依赖-->
<dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId> <groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId> <artifactId>mybatis-plus</artifactId>
<version>${mybatis-plus.version}</version> <version>${mybatis-plus.version}</version>
......
package com.stylefeng.guns.config;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
/**
* spring session配置
*
* @author fengshuonan
* @date 2017-07-13 21:05
*/
@EnableRedisHttpSession
public class SpringSessionConfig {
}
package com.stylefeng.guns.config.web; package com.stylefeng.guns.config.web;
import com.stylefeng.guns.config.properties.GunsProperties;
import com.stylefeng.guns.core.shiro.ShiroDbRealm; import com.stylefeng.guns.core.shiro.ShiroDbRealm;
import org.apache.shiro.cache.CacheManager; import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.ehcache.EhCacheManager; import org.apache.shiro.cache.ehcache.EhCacheManager;
...@@ -10,10 +9,8 @@ import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSource ...@@ -10,10 +9,8 @@ import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSource
import org.apache.shiro.spring.web.ShiroFilterFactoryBean; import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager; import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.Cookie;
import org.apache.shiro.web.servlet.ShiroHttpSession;
import org.apache.shiro.web.servlet.SimpleCookie; import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager; import org.apache.shiro.web.session.mgt.ServletContainerSessionManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator; import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.config.MethodInvokingFactoryBean; import org.springframework.beans.factory.config.MethodInvokingFactoryBean;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean; import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
...@@ -37,33 +34,41 @@ public class ShiroConfig { ...@@ -37,33 +34,41 @@ public class ShiroConfig {
* 安全管理器 * 安全管理器
*/ */
@Bean @Bean
public DefaultWebSecurityManager securityManager(CookieRememberMeManager rememberMeManager, CacheManager cacheShiroManager, DefaultWebSessionManager defaultWebSessionManager) { public DefaultWebSecurityManager securityManager(CookieRememberMeManager rememberMeManager, CacheManager cacheShiroManager, ServletContainerSessionManager servletContainerSessionManager) {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(this.shiroDbRealm()); securityManager.setRealm(this.shiroDbRealm());
securityManager.setCacheManager(cacheShiroManager); securityManager.setCacheManager(cacheShiroManager);
securityManager.setRememberMeManager(rememberMeManager); securityManager.setRememberMeManager(rememberMeManager);
securityManager.setSessionManager(defaultWebSessionManager); securityManager.setSessionManager(servletContainerSessionManager);
return securityManager; return securityManager;
} }
/** /**
* session管理器 * spring session管理器
*/ */
@Bean @Bean
public DefaultWebSessionManager defaultWebSessionManager(CacheManager cacheShiroManager, GunsProperties gunsProperties) { public ServletContainerSessionManager servletContainerSessionManager() {
DefaultWebSessionManager sessionManager = new DefaultWebSessionManager(); return new ServletContainerSessionManager();
sessionManager.setCacheManager(cacheShiroManager);
sessionManager.setSessionValidationInterval(gunsProperties.getSessionValidationInterval() * 1000);
sessionManager.setGlobalSessionTimeout(gunsProperties.getSessionInvalidateTime() * 1000);
sessionManager.setDeleteInvalidSessions(true);
sessionManager.setSessionValidationSchedulerEnabled(true);
Cookie cookie = new SimpleCookie(ShiroHttpSession.DEFAULT_SESSION_ID_NAME);
cookie.setName("shiroCookie");
cookie.setHttpOnly(true);
sessionManager.setSessionIdCookie(cookie);
return sessionManager;
} }
///**
// * session管理器
// */
//@Bean
//public DefaultWebSessionManager defaultWebSessionManager(CacheManager cacheShiroManager, GunsProperties gunsProperties) {
// DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
// sessionManager.setCacheManager(cacheShiroManager);
// sessionManager.setSessionValidationInterval(gunsProperties.getSessionValidationInterval() * 1000);
// sessionManager.setGlobalSessionTimeout(gunsProperties.getSessionInvalidateTime() * 1000);
// sessionManager.setDeleteInvalidSessions(true);
// sessionManager.setSessionValidationSchedulerEnabled(true);
// Cookie cookie = new SimpleCookie(ShiroHttpSession.DEFAULT_SESSION_ID_NAME);
// cookie.setName("shiroCookie");
// cookie.setHttpOnly(true);
// sessionManager.setSessionIdCookie(cookie);
// return sessionManager;
//}
/** /**
* 缓存管理器 使用Ehcache实现 * 缓存管理器 使用Ehcache实现
......
...@@ -27,6 +27,10 @@ beetl: ...@@ -27,6 +27,10 @@ beetl:
################### spring配置 ################### ################### spring配置 ###################
spring: spring:
redis:
host: localhost
port: 6379
password:
profiles: profiles:
active: dev active: dev
mvc: mvc:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment