首页 > 数据库 >springboot +redis+token

springboot +redis+token

时间:2022-10-20 11:26:04浏览次数:52  
标签:return springboot redis springframework token org import String

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

相关文章

  • 前后端分离数组传递问题(springboot)(Vue)
    前后端分离数组传递问题昨天与前端对接时,我后端需要List的数据,就是找不到参数,我看了前端代码也没发现问题。绝问题解决过程我的后端代码:@Transactional@PostM......
  • 断点SpringBoot-断点续传-大文件断点上传
    ​ 一、功能性需求与非功能性需求要求操作便利,一次选择多个文件和文件夹进行上传;支持PC端全平台操作系统,Windows,Linux,Mac支持文件和文件夹的批量下载,断点续传。刷......
  • 1、初始SpringBoot
    什么是springSpring是一个开源框架,2003年兴起的一个轻量级的java开发框架,作者:RodJohnson.Spring是为了解决企业级应用开发的复杂性而创建的,简化开发       ......
  • SpringBoot项目部署
    我们要想在linux系统上运行这个项目,就要保证他运行所用的端口没有被占用,不然运行就会报错查看端口使用情况netstat-anp|grep9999可以看到这个端口被占用了(没被占用的......
  • PHP REDIS GEO 经纬度
    本文是使用redis代替数据库金纬度查询,由于数据库金纬度度让数据库去做运算影响性能所以下面就介绍了用redis去使用redis中提供了geo类,使用就行了 $redis=newre......
  • 手写自定义springboot-starter,感受框架的魅力和原理
    一、前言Springboot的自动配置原理,面试中经常问到,一直看也记不住,不如手写一个starter,加深一下记忆。看了之后发现大部分的starter都是这个原理,实践才会记忆深刻。核心思......
  • Redis 设置密码
    描述本人图省事本机用的rediswindows版本的,其余配置内容一样。配置文件区别修改配置文件使用分两种情况当前redis不是服务启动的用:redis.windows.conf文件当前redis是以......
  • SpringBoot+MybatisPlus--使用
    1、在entity包下面创建数据实体类,添加注解@Data,如果和数据库名字不一样的话,还需要+@TableField注解。字段名字不一样也需要添加此注解@TableName(value="user")publi......
  • SpringBoot对接口请求参数(@RequestBody 和 @ Request Param)进行解密过滤
      /***@Description:拦截所有请求过滤器,并将请求类型是HttpServletRequest类型的请求替换为自定义*/@javax.servlet.annotation.WebFilter(filterName="Web......
  • SpringBoot+MybatisPlus--文件上传
    文件上传时,对页面的form表单有如下要求: 采用post方式提交数据   method="post"采用multipart格式上传文件  enctype="multipart/form-data"使用inp......