1、pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2、application.properties
# 应用名称
spring.application.name=token2038
# 应用服务 WEB 访问端口
server.port=2038
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.database=1
3、RedisService
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
@Service
public class RedisService {
@Autowired
RedisTemplate redisTemplate;
public void set(String key,Object value){
//更改在redis里面查看key编码问题
RedisSerializer redisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(redisSerializer);
ValueOperations<String,Object> vo = redisTemplate.opsForValue();
vo.set(key,value);
}
public Object get(String key){
ValueOperations<String,Object> vo = redisTemplate.opsForValue();
return vo.get(key);
}
public boolean delete(String key){
return redisTemplate.delete(key);
}
}
4、LoginService
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.UUID;
@Service
public class LoginService {
@Autowired
RedisService redisService;
/**
* 进行登录操作,如果用户名和密码正确,使用UUID一个字符串作为token
* @param username
* @param password
* @return
*/
public String login(String username,String password){
if(username.equals("liu")&&password.equals("123")){
String token = UUID.randomUUID().toString();
redisService.set(token,username);
return username+"登录成功,token是:"+token;
}else {
return "用户名或密码错误";
}
}
/**
* 进行注销操作,实质是删除redis和token中的缓存
* @param httpServletRequest
* @return
*/
public String logout(HttpServletRequest httpServletRequest){
String token = httpServletRequest.getHeader("token");
boolean delete = redisService.delete(token);
if (delete){
return "注销成功";
}else {
return "注销失败";
}
}
}
5、LoginController
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/login")
public class LoginController {
@Autowired
LoginService loginService;
@RequestMapping("/login")
public String login(String username,String password){
return loginService.login(username,password);
}
@RequestMapping("/logout")
public String logout(HttpServletRequest httpServletRequest){
return loginService.logout(httpServletRequest);
}
}
6、AuthInterceptor
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;
@Component
public class AuthInterceptor implements HandlerInterceptor {
@Autowired
RedisService redisService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String token = request.getHeader("token");
if (StringUtils.isEmpty(token)) {
response.getWriter().print("用户未登录");
return false;
}
Object loginStatues = redisService.get(token);
if (Objects.isNull(loginStatues)) {
response.getWriter().print("token错误");
return false;
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
}
}
7、AuthConifg
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class AuthConifg implements WebMvcConfigurer {
@Autowired
AuthInterceptor authInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authInterceptor).addPathPatterns("/test/**")
.excludePathPatterns("/login/**");
}
}
8、Token2038Application
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Token2038Application {
public static void main(String[] args) {
SpringApplication.run(Token2038Application.class, args);
}
}
参考:https://www.cnblogs.com/lyd447113735/p/14890546.html
标签:return,springboot,redis,springframework,token,org,import,String From: https://www.cnblogs.com/smallfa/p/16809077.html