(12) Custom session Management

Source: Internet
Author: User
Tags redis redis version

Easyopen1.4.0 started to support custom session management. Create session

After successful login, create session and return to SessionID

Custom session
    @PostMapping ("Managedsessionlogin") public
    String Managedsessionlogin (httpservletrequest Request) {
        //Assuming the login succeeds, create a SessionID return to the client
        sessionmanager SessionManager = Apicontext.getapiconfig (). Getsessionmanager ();
        HttpSession session = Sessionmanager.getsession (null);
        Session.setattribute ("username", "Tom");

        return Session.getid ();
    }
Use session

The client needs to pass the Access_token parameter with a value of SessionID

Request Parameters:

{
    "Access_token": "4191fb1d8356495d98eccf91c615a530",
    "App_key": "Test",
    "data": "{}",
    "sign": " F7ab6bc059dfca93ca2328c9baf236ba ",
    " Sign_method ":" MD5 ",
    " name ":" Manager.session.get ",
    " format ": "JSON",
    "version": "",
    "timestamp": "2018-03-13 13:48:45"
}

Service side via HttpSession session = Apicontext.getmanagedsession (); Get session

@Api (name = "Manager.session.get") public
    Object managersetsession () {
        HttpSession session = Apicontext.getmanagedsession ();

        System.out.println (Session.getid ());

        Object user = Session.getattribute ("username");

        return user;
    }
using Redis to manage session

EasyOpen uses Google's guava cache for session management by default, but there is a problem with the cluster, so EasyOpen also provides a redis version. The configuration is as follows: Pom Add Redis dependency

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId> Spring-boot-starter-data-redis</artifactid>
        </dependency>
Adding Redis Parameters
################ #redis基础配置 #################
spring.redis.database=0
spring.redis.host=10.1.11.48
Spring.redis.password=0987654321rfvujmtgbyhn
spring.redis.port=6379
# Connection time-out unit MS (MS)
spring.redis.timeout=3000

################ #redis线程池设置 #################
# The maximum idle connection in the connection pool, the default value is also 8.
spring.redis.pool.max-idle=500
#连接池中的最小空闲连接, the default value is also 0.
spring.redis.pool.min-idle=50
# If the assignment is 1, it means no limit, and if the pool is already assigned a maxactive Jedis instance, the pool's status is exhausted (exhausted).
spring.redis.pool.max-active=2000
# The maximum time to wait for an available connection, in milliseconds, and the default value is-1, which means never time out. If the wait time is exceeded, the Jedisconnectionexception spring.redis.pool.max-wait=1000 is thrown directly
Set Apiconfig
 @Controller @RequestMapping (value = "/api") public class Indexcontroller extends Apicontroller {@Autowired private redistemplate redistemplate;//1 declaring Redis template @Override protected void Initapiconfig (Apiconfig apiconfig) {//config key value pair map<string, string> appsecretstore = new hashmap<
        String, string> ();

        Appsecretstore.put ("Test", "123456");

        2 configuration SessionManager Apiconfig.setsessionmanager (new Redissessionmanager (redistemplate));
    Apiconfig.addappsecret (Appsecretstore); }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.