您好,登錄后才能下訂單哦!
springBoot中怎么配置Redis管理session,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
1.引入maven依賴
<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>
2.寫(xiě)入關(guān)鍵配置
spring.session.store-type=redis #配置由redis管理session #redis連接信息 spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.timeout=600000 spring.redis.password=
3.加入關(guān)鍵配置代碼
@Configuration @EnableRedisHttpSession(maxInactiveIntervalInSeconds=60*60*4,redisNamespace = "spring:session:tjzz:application")//設(shè)置session過(guò)期時(shí)間,設(shè)置存儲(chǔ)路徑 @ConfigurationProperties(prefix = "spring.redis") public class SessionConfig { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.timeout}") private int timeout; @Value("${spring.redis.password}") private String password; /* * 配置redis序列化方式 * */ @Bean(name = "springSessionDefaultRedisSerializer") public GenericJackson2JsonRedisSerializer getGenericJackson2JsonRedisSerializer() { return new GenericJackson2JsonRedisSerializer(); } /* * cookie Base64解碼 * */ @Bean public DefaultCookieSerializer getDefaultCookieSerializer(){ DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer(); cookieSerializer.setUseBase64Encoding(false); return cookieSerializer; } }
4.測(cè)試類
@RestController public class SessionController { @RequestMapping(value = "/get-session-id.action",method = RequestMethod.GET) public String getSessionId(){ HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); HttpSession session = request.getSession(); String sessionId=""; Object temp=session.getAttribute("USER");//session中存儲(chǔ)userCode的key為USER System.out.println(temp.toString()); sessionId=session.getId(); return sessionId; } }
看完上述內(nèi)容,你們掌握springBoot中怎么配置Redis管理session的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。