这篇文章主要为大家展示了“SpringBoot如何实现token登录”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“SpringBoot如何实现token登录”这篇文章吧。
为什么引入token机制?
在进行登录验证时,我们需要session或cookie会话进行验证,客户端包括浏览器、app、微信小程序、公众号,只有浏览器有session和cookie机制,当我们脱离浏览器用app等向服务端发请求就没有session和cookie机制,这时我们就需要使用token令牌进行登录验证。
代码实现
先建个util包,并创建TokenUtil类用于生成token
TokenUtil类代码
package com.qcby.util; import com.qcby.entity.User; import java.util.HashMap;import java.util.Map;import java.util.UUID; public class TokenUtil { private static Map<String, User> tokenMap=new HashMap<>(); public static String generateToken(User user){ //生成唯一不重复的字符串 String token = UUID.randomUUID().toString(); tokenMap.put(token,user); return token; } public static boolean verify(String token){ return tokenMap.containsKey(token); } public static User getUser(String token){ return tokenMap.get(token); }}
LoginInterceptor类代码
package com.qcby.interceptor; import com.qcby.util.TokenUtil;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.method.HandlerMethod;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession; public class LoginInterceptor implements HandlerInterceptor { //Controller逻辑执行之前 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("preHandle...."); String uri = request.getRequestURI(); System.out.println("当前路径"+uri); if (!(handler instanceof HandlerMethod)) { return true; } String token=request.getHeader("qcby-token"); if (!TokenUtil.verify(token)) { // 未登录跳转到登录界面 throw new RuntimeException("no login!"); } else { return true; } } //Controller逻辑执行完毕但是视图解析器还未进行解析之前 @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println("postHandle...."); } //Controller逻辑和视图解析器执行完毕 @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { System.out.println("afterCompletion...."); }}
实现类
@RestController@RequestMapping("user")public class UserController { @Autowired private UserService userService; @ApiOperation("用户登录接口") @RequestMapping(value="login",method = {RequestMethod.GET,RequestMethod.POST}) public Map<String,Object>login(User user){ Map<String,Object> map=new HashMap<>(); map.put("code",0); if(StringUtils.isEmpty(user.getUsername())||StringUtils.isEmpty(user.getPassword())){ map.put("msg","用户或密码为空!"); return map; } QueryWrapper<User> queryWrapper=new QueryWrapper<>(); queryWrapper.eq("username",user.getUsername()) .eq("password",user.getPassword()); User user1=userService.getOne(queryWrapper); if(user1!=null){ String token= TokenUtil.generateToken(user1); map.put("cod",1); map.put("data",user1); map.put("token",token); }else { map.put("msg","用户名或密码错误!"); } return map; }}
拦截器类可以参考这篇文章,这个token验证就是在拦截器上改动的。
SpringBoot拦截器实现登录拦截
需要有postman验证
以上是“SpringBoot如何实现token登录”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网行业资讯频道!