1. Implementation of Distributed Session with Edits

In the framework of micro-service, we need to support distributed Session. Distributed Session can be implemented by Redis or database. This paper introduces the implementation of Redis.

II. Installation of Redis

Download address: https://github.com/MSOpenTech...
Select the appropriate version to install.

Go to the installation directory and start Redis.

III. pom Files



IV. yml Configuration

The yml configuration is as follows:

# Redis server address
# Redis Server Connection Port

Config configuration

The config configuration is as follows:

@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400 * 30)
public class SessionConfig {


6. Setting Session after landing

Set up Session information after successful landing. The code is as follows:

public class LoginController {

    public String login(@RequestBody String userId, HttpSession  session) throws Exception {
        session.setAttribute(Constants.SESSION_USER_ID, userId);
        return "Login success.";

Session Authentication Filter

Failing to log in successfully and set up Session, you need to jump to the error page. The code example is as follows:

public class SessionFilter extends OncePerRequestFilter {

    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {

        if (isNeedAuth(request)) {
            Object userIdObject = request.getSession().getAttribute(Constants.SESSION_USER_ID);
            if (null == userIdObject) {

        filterChain.doFilter(request, response);

In order to achieve all the steps of Redis Session, the complete code is added to the public address and reply to: webfullstack, get the address of the warehouse.


Site: http://javashizhan.com/

Public address:

<font color="#F4B183"> Join the knowledge planet, participate in the discussion, and share more actual combat code! </font>