SpringBoot 前后端Token方式调用接口+ Redis+Vue

前后端分离,不用session采用token方式,前后端相关代码调整

vue+vue-resource设置请求头(带上token)

第一步:加载相关pom.xml

    	
			com.alibaba
			fastjson
			1.2.60
		

        
		
			org.springframework.boot
			spring-boot-starter-data-redis
		
		
			org.springframework.boot
			spring-boot-starter-cache
		
		
		
			org.springframework.session
			spring-session-data-redis
		
		

第二步: 创建配置类 RedisConfig.java

package com.example.demo.config;


import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;


@Configuration

public class RedisConfig {

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate template = new RedisTemplate<>();
        //使用fastjson序列化
        FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
        // value值的序列化采用fastJsonRedisSerializer
        template.setValueSerializer(fastJsonRedisSerializer);
        template.setHashValueSerializer(fastJsonRedisSerializer);
        // key的序列化采用StringRedisSerializer
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(fastJsonRedisSerializer);
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

 业务代码


 1、编写登录控制器代码  LoginController.java

 

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@Controller
@RestController
@RequestMapping("/login")
public class LoginController {

    String NamespaceStr = "spring:demo:token:";

    @Autowired
    RedisTemplate redisTemplate;


    @PostMapping(value = "/submit", name = "登录提交")
    public void submit(@RequestParam("username") String username, @RequestParam("password") String password) {
        if (username.equals("admin") && password.equals("123")) {
            Map userInfo = new HashMap() {{
                put("username", "admin");
                put("group_name", "超级管理员");
                put("login_time", new Date());
            }};
            ValueOperations operations = redisTemplate.opsForValue();
            String guid = GetGUID();
            operations.set(NamespaceStr + guid, userInfo, 3600 * 3, TimeUnit.SECONDS);  //userInfo保存3小时
            Map retData = new HashMap() {{
                put("token", guid);
                put("userInfo", userInfo);
            }};
            System.out.println(retData);
        } else  {
            System.out.println("账户或密码错误!");
        }
    }

    public static String GetGUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }
}

2、用RedisDesktopManager 工具查看 redis

此时在redis看到的数据并非乱码,是因为在RedisConfig.java 做了序列化处理。

SpringBoot 前后端Token方式调用接口+ Redis+Vue_第1张图片

前端代码


vue+vue-resource设置请求头(带上token)
全局改变:Vue.http.headers.common['token'] = store.state.token; 

 SpringBoot 前后端Token方式调用接口+ Redis+Vue_第2张图片

SpringBoot 前后端Token方式调用接口+ Redis+Vue_第3张图片

你可能感兴趣的:(Springboot)