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

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

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

第一步:加载相关pom.xml

    	<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.60</version>
		</dependency>

        <!-- redis -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-cache</artifactId>
		</dependency>
		<!-- Redis Session -->
		<dependency>
			<groupId>org.springframework.session</groupId>
			<artifactId>spring-session-data-redis</artifactId>
		</dependency>
		<!-- Redis Session -->

第二步: 创建配置类 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<Object, Object> 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<String, Object> redisTemplate;


    @PostMapping(value = "/submit", name = "登录提交")
    public void submit(@RequestParam("username") String username, @RequestParam("password") String password) {
        if (username.equals("admin") && password.equals("123")) {
            Map<String, Object> userInfo = new HashMap<String, Object>() {{
                put("username", "admin");
                put("group_name", "超级管理员");
                put("login_time", new Date());
            }};
            ValueOperations<String, Object> operations = redisTemplate.opsForValue();
            String guid = GetGUID();
            operations.set(NamespaceStr + guid, userInfo, 3600 * 3, TimeUnit.SECONDS);  //userInfo保存3小时
            Map<String, Object> retData = new HashMap<String, Object>() {{
                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 做了序列化处理。

前端代码


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

 1003225-20190618180146634-1377257010.png

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值