【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
**开源地址:https://docs.qq.com/doc/DSmxTbFJ1cmN1R2dB **
Jws<Claims> claimsJws
= Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token);
Claims claims = claimsJws.getBody();
return (String)claims.get("userName");
}
public static void main(String[] args) {
String token = JwtHelper.createToken(1L, "55");
System.out.println(token);
System.out.println(JwtHelper.getUserId(token));
System.out.println(JwtHelper.getUserName(token));
}
}
登录模块调用方法,传入id,name值即可。
**使用redis做单点登录**
引入依赖
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
配置文件
#配置redis服务
spring.redis.host=127.0.0.1
spring.redis.port=6379
#配置使用redis那个数据库
spring.redis.database=1
#配置redis连接池
spring.redis.jedis.pool.max-active=20
#最大空闲数
spring.redis.jedis.pool.max-idle=10
spring.redis.jedis.pool.min-idle=2
创建配置类,采用重载方式
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
//key value序列化方式
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
//对hash序列化
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
//注入连接工厂
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
serviceImpl中
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
@Autowired
private UserMapper userMapper;
@Autowired
private RedisTemplate redisTemplate;
@Override
public RespBean doLogin(LoginVo loginVo , HttpServletRequest request, HttpServletResponse response) {
String mobile = loginVo.getMobile();
String password = loginVo.getPassword();
if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
return RespBean.error(RespBeanEnum.LOGINVO_ERROR);
}
if (!ValidatorUtil.isMobile(mobile)){
return RespBean.error(RespBeanEnum.LOGINVO_ERROR);
}
User user = userMapper.selectById(mobile);
if (null == user){
return RespBean.error(RespBeanEnum.LOGINVO_ERROR);
}
if (!MD5Util.formPassToDBPass(password,user.getSlat()).equals(user.getPassword())){
return RespBean.error(RespBeanEnum.LOGINVO_ERROR);
}
//生成cookie
String ticket = UUIDUtil.uuid();
//用户信息存入redis
redisTemplate.opsForValue().set("user:"+ticket,user);
// request.getSession().setAttribute(ticket,user);
CookieUtil.setCookie(request,response,"ticket",ticket);
return RespBean.success();
}
}
service添加新方法,获取用户
//获取用户值
@Override
public User getUserByCookie(String userTicket, HttpServletRequest request, HttpServletResponse response) {
User user = (User)redisTemplate.opsForValue().get("user:" + userTicket);
if (user != null){
CookieUtil.setCookie(request,response,"userTicket",userTicket);
}
return user;
}
此时即可完成单点登录。
还可以优化一下,因为每次都需要判断用户是否登录,所以在进行接收参数之前就进行判断
//自定义用户参数
@Component
public class UserArgumentResolver implements HandlerMethodArgumentResolver {
@Autowired
private IUserService userService;
//判断类型是不是uaer类型
@Override
public boolean supportsParameter(MethodParameter parameter) {
//获取参数类型
Class<?> clazz = parameter.getParameterType();
return clazz == User.class;
}
//进行ticket判断
@Override
public Object resolveArgument(MethodParameter parameter,
ModelAndViewContainer mavContainer,
NativeWebRequest webRequest,
WebDataBinderFactory binderFactory) throws Exception {
HttpServletRequest request =
webRequest.getNativeRequest(HttpServletRequest.class);
HttpServletResponse response =
webRequest.getNativeResponse(HttpServletResponse.class);
String ticket = CookieUtil.getCookieValue(request, "userTicket");
if (StringUtils.isEmpty(ticket)) {
return null;
}
//获取redis数据
return userService.getUserByCookie(ticket, request, response);
}
}
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {