spring-凯发k8网页登录

http session的管理通常是由容器来做,但如果是在paas环境下,服务器不能做变更,则需要由web应用来做处理http session。

同样,如果是分布式的环境下,session的管理也会带来性能问题。

spring推出了处理session的框架:spring-session。


spring会重写http session的那一套,使用session也同样还是用
req.getsession().setattribute(attributename, attributevalue);


此框架使用redis作为http session的持久化容器。此框架只对使用的当前web应用下的http session进行集中管理,同一web容器下的其他web应用还是会使用容器的session管理那一套。

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/xsd/maven-4.0.0.xsd">
    <modelversion>4.0.0modelversion>
    <groupid>com.paulgroupid>
    <artifactid>spring-sessionartifactid>
    <packaging>warpackaging>
        <version>1.0version>
    <repositories>
        <repository>
            <id>spring-snapshotid>
            <url>https://repo.spring.io/libs-milestoneurl>
        repository>
        <repository>
            <id>clojars.orgid>
            <url>http://clojars.org/repourl>
        repository>
    repositories>
    <dependencies>
        <dependency>
            <groupid>org.springframework.sessiongroupid>
            <artifactid>spring-sessionartifactid>
            <version>1.0.0.rc1version>
        dependency>
        <dependency>
            <groupid>org.springframeworkgroupid>
            <artifactid>spring-webartifactid>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupid>org.springframework.datagroupid>
            <artifactid>spring-data-redisartifactid>
            <version>1.3.0.releaseversion>
        dependency>
        <dependency>
            <groupid>redis.clientsgroupid>
            <artifactid>jedisartifactid>
            <version>2.4.1version>
        dependency>
        <dependency>
            <groupid>org.apache.commonsgroupid>
            <artifactid>commons-pool2artifactid>
            <version>2.2version>
        dependency>
        <dependency>
            <groupid>redis.embeddedgroupid>
            <artifactid>embedded-redisartifactid>
            <version>0.3version>
        dependency>
        <dependency>
            <groupid>javax.servletgroupid>
            <artifactid>javax.servlet-apiartifactid>
            <version>3.1.0version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupid>jstlgroupid>
            <artifactid>jstlartifactid>
            <version>1.2version>
        dependency>
    dependencies>
    <build>
        <plugins>
            <plugin>
                <groupid>org.apache.tomcat.mavengroupid>
                <artifactid>tomcat7-maven-pluginartifactid>
                <version>2.0version>
                <configuration>
                    <path>/path>
                configuration>
            plugin>
            <plugin>
                <artifactid>maven-compiler-pluginartifactid>
                <version>3.1version>
                <configuration>
                    <source>1.7source>
                    <target>1.7target>
                configuration>
            plugin>
        plugins>
    build>
    <properties>
        <spring.version>4.1.0.releasespring.version>
    properties>
project>


在eclipse中运行tomcat的文件:spring-session-tomcat-run.launch
xml version="1.0" encoding="utf-8" standalone="no"?>
<launchconfiguration type="org.eclipse.m2e.maven2launchconfigurationtype">
<booleanattribute key="m2_debug_output" value="false"/>
<stringattribute key="m2_goals" value="tomcat7:run"/>
<booleanattribute key="m2_non_recursive" value="false"/>
<booleanattribute key="m2_offline" value="false"/>
<stringattribute key="m2_profiles" value=""/>
<listattribute key="m2_properties"/>
<stringattribute key="m2_runtime" value="embedded"/>
<booleanattribute key="m2_skip_tests" value="false"/>
<intattribute key="m2_threads" value="1"/>
<booleanattribute key="m2_update_snapshots" value="false"/>
<booleanattribute key="m2_workspace_resolution" value="false"/>
<stringattribute key="org.eclipse.jdt.launching.vm_arguments" value="-dspring.profiles.active=dev"/>
<stringattribute key="org.eclipse.jdt.launching.working_directory" value="${workspace_loc:/spring-session}"/>
launchconfiguration>



启动内置redis服务器的文件:embeddedredisconfiguration.java
package com.paul.springsesseion;

import org.springframework.beans.factory.disposablebean;
import org.springframework.beans.factory.initializingbean;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;

import redis.clients.jedis.protocol;
import redis.embedded.redisserver;

/**
 * runs an embedded redis instance. this is only necessary since we do not want
 * users to have to setup a redis instance. in a production environment, this
 * would not be used since a redis server would be setup.
 *
 * 
@author rob winch
 
*/
@configuration
public class embeddedredisconfiguration {

    @bean
    public redisserverbean redisserver() {
        return new redisserverbean();
    }

    class redisserverbean implements initializingbean, disposablebean {
        private redisserver redisserver;


        @override
        public void afterpropertiesset() throws exception {
            redisserver = new redisserver(protocol.default_port);
            redisserver.start();
        }

        @override
        public void destroy() throws exception {
            if(redisserver != null) {
                redisserver.stop();
            }
        }
    }
}


配置文件:config.java
package com.paul.springsesseion;

import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.context.annotation.import;
import org.springframework.data.redis.connection.jedis.jedisconnectionfactory;
import org.springframework.session.data.redis.config.annotation.web.http.enableredishttpsession;

/**
 * 
@author rob winch
 
*/
@import(embeddedredisconfiguration.class)
@configuration
@enableredishttpsession
public class config {

    @bean
    public jedisconnectionfactory connectionfactory() {
        return new jedisconnectionfactory();
    }
}


初始化配置文件:initializer.java
package com.paul.springsesseion;

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

/**
 * 
@author rob winch
 
*/
public class initializer extends abstracthttpsessionapplicationinitializer {

    public initializer() {
        super(config.class);
    }
}



存取http session:sessionservlet.java
package com.paul.springsesseion;

import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import java.io.ioexception;

/**
 * 
@author rob winch
 
*/
@webservlet("/session")
public class sessionservlet extends httpservlet {

    @override
    protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
        string attributename = req.getparameter("attributename");
        string attributevalue = req.getparameter("attributevalue");
        req.getsession().setattribute(attributename, attributevalue);
        resp.sendredirect(req.getcontextpath()   "/");
    }

    private static final long serialversionuid = 2878267318695777395l;
}


页面:index.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
doctype html>
<html lang="en">
<head>
    <title>session attributestitle>
    <link rel="stylesheet" href="assets/bootstrap.min.css">
    <style type="text/css">
        body 
{
            padding
: 1em;
        
}
    
style>
head>
<body>
    <div class="container">
        <h1>descriptionh1>
        <p>this application demonstrates how to use a redis instance to back your session. notice that there is no jsessionid cookie. we are also able to customize the way of identifying what the requested session id is.p>

        <h1>try ith1>

        <form class="form-inline" role="form" action="./session" method="post">
            <label for="attributevalue">attribute namelabel>
            <input id="attributevalue" type="text" name="attributename"/>
            <label for="attributevalue">attribute valuelabel>
            <input id="attributevalue" type="text" name="attributevalue"/>
            <input type="submit" value="set attribute"/>
        form>

        <hr/>

        <table class="table table-striped">
            <thead>
            <tr>
                <th>attribute nameth>
                <th>attribute valueth>
            tr>
            thead>
            <tbody>
            <c:foreach items="${sessionscope}" var="attr">
                <tr>
                    <td><c:out value="${attr.key}"/>td>
                    <td><c:out value="${attr.value}"/>td>
                tr>
            c:foreach>
            tbody>
        table>
    div>
body>
html>


参考:

posted on 2014-11-19 18:23 paulwong 阅读(6312) 评论(1)     所属分类: spring分布式redis

feedback

# spring-session框架能否做url重写? 2016-06-08 11:14

在tomcat 自生的session管理中通过下面主要的方式
${basepath}/servlet/upload;jsessionid=${sessionid}?
做url重写,如果改为spring session管理,能否做url重写?
如果可以的话,参数该怎么表达?   回复     



只有注册用户后才能发表评论。


网站导航:
              
 
网站地图