前后端分离,不用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;