基于redis的Spring session配置

5 篇文章 0 订阅
本文介绍了一个使用Spring Session和Redis实现会话管理的示例应用。该应用通过配置pom.xml引入所需依赖,并通过web.xml、dispatcher-servlet.xml等文件进行部署及配置。此外,还展示了如何使用Config.java配置Redis连接并初始化HttpSession。
摘要由CSDN通过智能技术生成

1、pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.qh</groupId>
  <artifactId>project</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>project Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <repositories>
		<repository>
		    <id>opencast-public</id>
		    <url>http://mvnrepository.com/artifact/org.springframework</url>
		  </repository>
		<repository>
			<id>repository.jboss.org</id>
			<name>JBoss Public Maven Repository Group</name>
			<url>https://repository.jboss.org/nexus/content/groups/public/</url>
			<layout>default</layout>
			<releases>
				<enabled>true</enabled>
				<updatePolicy>never</updatePolicy>
			</releases>
			<snapshots>
				<enabled>true</enabled>
				<updatePolicy>never</updatePolicy>
			</snapshots>
		</repository>
		<repository>
			<id>codehaus</id>
			<name>codehaus</name>
			<url>http://repository.codehaus.org/</url>
		</repository>

		<repository>
			<id>sonatype</id>
			<name>Sonatype Repository</name>
			<url>http://repository.sonatype.org/content/groups/public/</url>
		</repository>

		<repository>
			<id>deprecated</id>
			<name>JBoss Deprecated</name>
			<url>https://repository.jboss.org/nexus/content/repositories/deprecated</url>
		</repository>
	</repositories>
	<dependencies>
		<!-- Spring core & mvc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>4.1.6.RELEASE</version>
		</dependency>
		<dependency>
             <groupId>org.springframework.session</groupId>
             <artifactId>spring-session-data-redis</artifactId>
             <version>1.0.2.RELEASE</version>
        </dependency>
		
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-core-asl</artifactId>
			<version>1.4.3</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-asl</artifactId>
			<version>1.4.3</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
			<version>2.4.2</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.4.2</version>
		</dependency>
		
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.7</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.7</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency><!---->
	</dependencies>
	<build>
		<finalName>project</finalName>
	</build>
</project>

2、web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>
3、dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ctx="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
	default-autowire="byName"
	xsi:schemaLocation="
		   http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<ctx:component-scan base-package="com.qh" />
	<ctx:annotation-config />
	<mvc:annotation-driven />

	<mvc:resources mapping="/**" location="/" />

	<bean
		class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">

		<property name="mediaTypes">
			<map>
				<entry key="atom" value="application/atom+xml" />
				<entry key="html" value="text/html" />
				<entry key="json" value="application/json" />
			</map>
		</property>

		<property name="viewResolvers">
			<list>
				<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
				<bean
					class="org.springframework.web.servlet.view.InternalResourceViewResolver">
					<property name="prefix" value="/" />
					<property name="suffix" value=".html" />
				</bean>
			</list>
		</property>

		<property name="defaultViews">
			<list>
				<bean
					class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
			</list>
		</property>
	</bean>
</beans>
3、Config.java

/**
 * 
 */
package com.qh.project.springsession.support;

import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;



@EnableRedisHttpSession
public class Config {
	@Bean
    public JedisConnectionFactory connectionFactory() {
		JedisConnectionFactory connection = new JedisConnectionFactory();
		connection.setPort(6379);
		connection.setHostName("10.3.8.97");
		return connection;
    }
}

4、Initializer.java

/**
 * 
 */
package com.qh.project.springsession.support;

import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer;


public class Initializer extends AbstractHttpSessionApplicationInitializer {
	public Initializer() {
		super(Config.class);
	}
}
5 SessionController.java

/**
 * 
 */
package com.qh.project.springsession.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
@RequestMapping("/session")
public class SessionController{
	
	@RequestMapping(value = "/write")
	@ResponseBody
	public Object writeSession(HttpServletRequest request,HttpServletResponse response){
		Map<Object,Object> user = new HashMap<>();
		user.put("userName", request.getParameter("userName"));
		request.getSession().setAttribute("user", user);
		Map<Object,Object> res = new HashMap<>();
		res.put("success", true);
		return res;
	}
	
	@RequestMapping(value = "/read")
	@ResponseBody
	public Object readSession(HttpServletRequest request,HttpServletResponse response){
		Map<Object,Object> res = new HashMap<>();
		res.put("success", true);
		res.put("user", request.getSession().getAttribute("user"));
		return res;
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sleeper01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值