springboot -- jwt

LoginInterceptor

LoginInterceptor

/**

* 登陆拦截

*/

@Component

@Slf4j

public class LoginInterceptor implements HandlerInterceptor {

    @Override

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        if (request.getMethod().equals("OPTIONS")) {

            log.info("11111");

            response.setStatus(HttpServletResponse.SC_OK);

            return true;

        }

        System.out.println("进入登陆拦截器...");

        String token=request.getHeader("token");

        System.out.println("***检查令牌"+token);

        Claims claims= Jwts.parser().setSigningKey("Mr_xiao").parseClaimsJws(token).getBody();

        System.out.println(claims);

        // 出错了会报异常

        return true;

    }

    @Override

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

}


/**

* 支持跨域

*/

@Configuration

public class WebConfig implements WebMvcConfigurer {

    @Autowired

    LoginInterceptor login_lj;

    @Override

    public void addCorsMappings(CorsRegistry registry) {

        registry.addMapping("/**")

                .allowedOriginPatterns("*")

                .allowedMethods("*")

                .allowCredentials(true)

                .maxAge(3600)

                .allowedHeaders("*");

    }

    @Override

    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(login_lj)

                .addPathPatterns("/**")

                .excludePathPatterns("/swagger-ui/**")

                .excludePathPatterns("/user/login");

    }

}



public class JwtUtil {

    public static String maketoken(Object obj){

        Date now=new Date();    //当前时间

        long guoqi=now.getTime()+1000*120; //过期时间2分钟

        Date expire=new Date(guoqi);    //转型

        JwtBuilder builder= Jwts.builder().setId("001")

                .setIssuedAt(now)  //创建时间

                .setExpiration(expire)  //过期时间

                .claim("login",obj)    //可以存对象[Object]

                .signWith(SignatureAlgorithm.HS256,"Mr_xiao");  //设置签名  用HS256算法,设置密钥为Mr_xiao

        String token=builder.compact();

        return token;

    }

}

@ApiOperation(value = "用户登录", notes = "登录")

    @GetMapping("/login")

    public R login(@ApiParam(value = "用户名", required = true) String tel, @ApiParam(value = "用户名", required = true) String idcode) {

        System.out.println("前台数据:"+tel+"pwd="+idcode);

        LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(tel), User::getTel, tel);

        User u1 = userService.getOne(lambdaQueryWrapper);

        System.out.println("user="+u1);

        String token= JwtUtil.maketoken(u1); //封装令牌

        if(u1!=null){

            return R.success(token);

        }else {

            return R.error("登陆失败");

        }

    }

https://blog.csdn.net/m_xiaozhilei/article/details/121740064

你可能感兴趣的:(springboot -- jwt)