jwt实现登录验证,spring boot jwt认证

  jwt实现登录验证,spring boot jwt认证

  1:首先,我们需要在项目中导入两个依赖项:

  依赖关系groupId com . auth 0/groupId artifactId Java-jwt/artifactId version 3 . 10 . 3/version/dependency依赖关系groupIdcn.hutool/grouartifacthutool-all/artifactId version 5 . 7 . 20/version/dependency 2:那我就说说需求:前端向后端发送一个表单-数据类型的数据,然后现在数据库中有一个管理员用户(见下图二)。

  (1):我们的思路是先从前端获取值,然后用前端的用户名在数据库中查找。我使用了一个封装的实体类:UserDto来存储来自前端的值:

  (2):然后我们会根据用户名找到的数据返回一个实体类,我们会用get方法得到他的盐:(也就是数据库里的盐)

  (3):接下来,我们将前端传来的密码和数据库中找到的salt加起来,形成一个新的字符串,然后用SHA-256对新的字符串进行加密,将加密后的密码与数据库中的密码进行比较,看是否相等。这里,上图也是圈出来的。至于这句话:String S1=hex utils . tohexstring(message digest . getinstance( sha-)就是用SHA-256加密。

  如果相等,则返回前端数据:下图是前端需要响应的代码示例,所以我们用HashMap封装中间返回的数据,外面的代码和msg使用一个封装类:

  3.涉及到的一些工具类和跨域:tokenUtils:创建sign in Token,我们介绍的第一个是数据库中的实体类,这里一定不能混淆。数据库中数据的实体类和前端传来的参数的封装实体类是:AdminLogin:然后第二个参数expires指的是到期时间,我们可以自己设置。还有其他需要我们自己修改的地方:

  .用claim (username ,user . getloginname())//来存储数据。With claim (password ,user.getPassword ()) user.get此处

  EXPIRE_TIME= 60*1000;//token到期时间60s private static final String TOKEN_SECRET="l122adasw532df"; //密钥盐 /** * 创建一个token * @param user * @return */ public static String sign(AdminLogin user,Date expires){ String token=null; try { token = JWT.create() .withIssuer("auth0")//发行人 .withClaim("username",user.getLoginName()) //存放数据 .withClaim("password",user.getPassword()) .withExpiresAt(expires)//过期时间 .sign(Algorithm.HMAC256(TOKEN_SECRET)); } catch (IllegalArgumentExceptionJWTCreationException je) { } return token; } /** * 对token进行验证 * @param token * @return */ public static Boolean verify(String token){ try { JWTVerifier jwtVerifier=JWT.require(Algorithm.HMAC256(TOKEN_SECRET)).withIssuer("auth0").build();//创建token验证器 DecodedJWT decodedJWT=jwtVerifier.verify(token); System.out.println("认证通过:"); System.out.println("username: " + TokenUtil.getUsername(token)); System.out.println("过期时间: " + decodedJWT.getExpiresAt()); } catch (IllegalArgumentException JWTVerificationException e) { //抛出错误即为验证不通过 return false; } return true; } /** * 获取用户名 */ public static String getUsername(String token) { try{ DecodedJWT jwt=JWT.decode(token); return jwt.getClaim("username").asString(); }catch (JWTDecodeException e) { return null; } }}跨域:

  

package com.example.check.config;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.CorsRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/** * 跨域配置 */@Configurationpublic class WebMvcConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedHeaders("Content-Type","X-Requested-With","accept,Origin","Access-Control-Request-Method","Access-Control-Request-Headers","token") .allowedMethods("*") .allowedOriginPatterns("*") .allowCredentials(true); }}

 

  然后我们打开前端界面:点击登录后就可以在后台看到已经传过去的参数:

  

 

  以上就是SpringBoot集成JWT实现登陆验证的方法详解的详细内容,更多关于SpringBoot JWT登陆验证的资料请关注盛行IT其它相关文章!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: