SpringMvc Reids Session 共享

原创
2017/08/11 15:47
阅读数 234

SpringMvc Reids Session 共享

场景说明:

        我在给项目添加Druid 对Web 应用进行监控的时候发现登录永远调登录页面,Ajax 返回success也还是无法登录,经过多方排查发现问题所在,本地开发环境是Win10-64系统单台开发,不存在这样的问题的,现网环境是两台服务器然后加负载均衡,聪明的同学我想应该知道问题所在了,废话不多说提枪上码咯。

    第一步添加Jar 包(Maven-pom.xml)   

<dependency>
	<groupId>org.springframework.session</groupId>
	<artifactId>spring-session-data-redis</artifactId>
	<version>1.0.1.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework.session</groupId>
	<artifactId>spring-session</artifactId>
	<version>1.0.1.RELEASE</version>
</dependency>
<dependency>
	<groupId>com.orange.redis-embedded</groupId>
	<artifactId>embedded-redis</artifactId>
	<version>0.6</version>
</dependency>

    第二步 添加拦截器(web.xml)

    <!-- Spring Session 共享拦截 -->
	<filter>
		<filter-name>springSessionRepositoryFilter</filter-name>
		<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>springSessionRepositoryFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<welcome-file-list>
		<welcome-file>/views/index.jsp</welcome-file>
	</welcome-file-list>

    第三步 加入Spring 管理(applicationContext.xml)

    <!-- redis -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    </bean>

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.host}" />
        <property name="port" value="${redis.port}" />
        <!--<property name="password" value="${redis.pass}" />-->
        <property name="timeout" value="${redis.timeout}" />
        <property name="poolConfig" ref="jedisPoolConfig" />
        <property name="usePool" value="true" />
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
    </bean>
    <!-- 将session放入redis -->
    <bean id="redisHttpSessionConfiguration"
          class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
        <property name="maxInactiveIntervalInSeconds" value="1800" />
    </bean>

    完成以上配置就实现了Redis 存储Session 实现了Session共享功能。

 

--------------------------------------------------------------华丽分割线--------------------------------------------------------------

Druid 登录问题排查

下面分享下 Druid 登录自动跳登录页面的过程吧。

首先楼主在网上各种查找各种搜看见人家说好了好多但是答案都不是那么的明确,忽然又一篇博客说了这样的一句说他在登录的时候返回 error 没有登录成功,这时我想我可以跟HTTP 请求过去看看啊。然后开始跟HTTP 的过程。

Network URL 跟踪结果

submitLogin:http://localhost:8080/druid/submitLogin 

有URL 好办事了

开始看代码了 web.xml 开始

com.alibaba.druid.support.http.StatViewServlet

ResourceServlet->service()方法中油这样的一段代码

        if ("/submitLogin".equals(path)) {
            String usernameParam = request.getParameter(PARAM_NAME_USERNAME);
            String passwordParam = request.getParameter(PARAM_NAME_PASSWORD);
            if (username.equals(usernameParam) && password.equals(passwordParam)) {
                request.getSession().setAttribute(SESSION_USER_KEY, username);
                response.getWriter().print("success");
            } else {
                response.getWriter().print("error");
            }
            return;
        }

Druid 是通过Session 储存用户的,综合上面的说明可以简单的出问题出在哪里了。

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
1 收藏
1
分享
返回顶部
顶部