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

原文地址:http://www.cnblogs.com/smallfa/p/16809077.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性