Code Monkey home page Code Monkey logo

jwt's Introduction

jwt

springboot整合jwt

一、导入jwt依赖

<dependency>
	<groupId>com.auth0</groupId>
    <artifactId>java-jwt</artifactId>
    <version>3.18.2</version>
</dependency>

二、新建工具类

在这里插入图片描述

public class JWTUtils {
    //用于加密header和payload形成signature,引号内自己指定
    private static final String SIGN = "qwe123!@#";

    //用于获取token
    public static String getToken(Map<String,String> map){
        //用于指定token过期日期,此处指定为3天
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE,3);

        JWTCreator.Builder builder = JWT.create();
        //map用于指定payload内容
        map.forEach((k,v)->{
            builder.withClaim(k,v);
        });
        String token = builder.withExpiresAt(instance.getTime())//指定token过期时间
                .sign(Algorithm.HMAC256(SIGN));//指定算法

        return token;
    }

    //用于验证token并返回token中的信息
    public static DecodedJWT verify(String token){
        return JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
    }
}

二、新建拦截器

在这里插入图片描述

新建拦截器实现HandlerInterceptor类并重些preHandle函数。

public class JWTInterceptors implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取header中的token
        String token = request.getHeader("token");
        Map<String,Object> map = new HashMap<>();
        try {
            DecodedJWT verify = JWTUtils.verify(token);
            return true;
        }catch (SignatureVerificationException e){
            //无效签名
            e.printStackTrace();
            map.put("msg","无效签名");
        }catch (TokenExpiredException e){
            //token过期
            e.printStackTrace();
            map.put("msg","token过期");
        }catch(AlgorithmMismatchException e){
            //token算法不一致
            e.printStackTrace();
            map.put("msg","token算法不一致");
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg","token无效");
        }
        map.put("state",false);
        //将map转为json (通过jackson)
        String json = new ObjectMapper().writeValueAsString(map);
        //将错误信息写入响应体
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().println(json);
        return false;
    }
}

三、配置工具类

在这里插入图片描述

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new JWTInterceptors())
                .addPathPatterns("/**")//需要jwt验证的路径
                .excludePathPatterns("/user/login");//不需要jwt验证的路径
    }
}

大功告成

测试

新建jwt数据库,user表 简易实现登录功能 调用login接口获取token 在这里插入图片描述 调用其他接口原则上必须在header中指定token,若不指定将返回错误信息 在这里插入图片描述 传入正确token则返回正确信息 在这里插入图片描述

jwt's People

Contributors

dilidili131 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.