欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

SpringBoot集成JWT怎么生成token和校驗

這篇文章主要講解了“SpringBoot集成JWT怎么生成token和校驗”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“SpringBoot集成JWT怎么生成token和校驗”吧!

在吳中等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計、成都做網(wǎng)站 網(wǎng)站設(shè)計制作按需開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站設(shè)計,全網(wǎng)營銷推廣,成都外貿(mào)網(wǎng)站建設(shè),吳中網(wǎng)站建設(shè)費用合理。

封裝JTW生成token和校驗方法

public class JwtTokenUtil {

  //公用密鑰-保存在服務(wù)端,客戶端是不會知道密鑰的,以防被攻擊
  public static String SECRET = "ThisIsASecret";

  //生成Troke
  public static String createToken(String username) {
    //簽發(fā)時間
    //Date iatDate = new Date();
    //過地時間 1分鐘后過期
    //Calendar nowTime = Calendar.getInstance();
    //nowTime.add(Calendar.MINUTE, 1);
    //Date expiresDate = nowTime.getTime();
    Map<String, Object> map = new HashMap();
    map.put("alg", "HS256");
    map.put("typ", "JWT");
    String token = JWT.create()
          .withHeader(map)
          //.withClaim( "name","Free碼生") //設(shè)置 載荷 Payload
          //.withClaim("age","12")
          //.withClaim( "org","測試")
          //.withExpiresAt(expiresDate)//設(shè)置過期時間,過期時間要大于簽發(fā)時間
          //.withIssuedAt(iatDate)//設(shè)置簽發(fā)時間
          .withAudience(username) //設(shè)置 載荷 簽名的觀眾
          .sign(Algorithm.HMAC256(SECRET));//加密
    System.out.println("后臺生成token:" + token);
    return token;
  }

  //校驗TOKEN
  public static boolean verifyToken(String token) throws UnsupportedEncodingException{
    JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET)).build();
    try {
      verifier.verify(token);
      return true;
    } catch (Exception e){
      return false;
    }
  }

  //獲取Token信息
  public static DecodedJWT getTokenInfo(String token) throws UnsupportedEncodingException{
    JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SECRET)).build();
    try{
      return verifier.verify(token);
    } catch(Exception e){
      throw new RuntimeException(e);
    }
  }

}

新建自定義注解:@UserLoginToken

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface UserLoginToken {
  boolean required() default true;
}

關(guān)于攔截器配置:

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(authenticationInterceptor())
        .addPathPatterns("/**");  // 攔截所有請求,通過判斷是否有 @LoginRequired 注解 決定是否需要登錄
  }
  @Bean
  public AuthenticationInterceptor authenticationInterceptor() {
    return new AuthenticationInterceptor();
  }
}
public class AuthenticationInterceptor implements HandlerInterceptor {
  @Autowired
  UserService userService;
  @Override
  public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) throws Exception {
    String token = httpServletRequest.getHeader("token");// 從 http 請求頭中取出 token
    // 如果不是映射到方法直接通過
    if(!(object instanceof HandlerMethod)){
      return true;
    }
    HandlerMethod handlerMethod=(HandlerMethod)object;
    Method method=handlerMethod.getMethod();
    //檢查是否有passtoken注釋,有則跳過認(rèn)證
    if (method.isAnnotationPresent(PassToken.class)) {
      PassToken passToken = method.getAnnotation(PassToken.class);
      if (passToken.required()) {
        return true;
      }
    }
    //檢查有沒有需要用戶權(quán)限的注解
    if (method.isAnnotationPresent(UserLoginToken.class)) {
      UserLoginToken userLoginToken = method.getAnnotation(UserLoginToken.class);
      if (userLoginToken.required()) {
        // 執(zhí)行認(rèn)證
        if (token == null) {
          throw new RuntimeException("無token,請重新登錄");
        }
        // 驗證 token
        if(JwtTokenUtil.verifyToken(token)){
          return true;
        }else {
          throw new RuntimeException("401");
        }
      }
    }
    return true;
  }
  @Override
  public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
  }
  @Override
  public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
  }
}

登錄:

在Controller上登錄方法不用添加@UserLoginToken自定義注解,其余獲取后臺數(shù)據(jù)方法加上@UserLoginToken自定義注解,目的驗證token是否有效,是則返回數(shù)據(jù),否則提示401無權(quán)限。

測試:

@Controller
@RequestMapping(path = "/api")
public class IndexController {

  private String prefix = "index/";

  @GetMapping("/index")
  public String index()
  {
    return prefix + "index";
  }

  @UserLoginToken
  @PostMapping("/test")
  @ResponseBody
  public Object test(){
    Map<String,Object> map = new HashMap<>();
    map.put("code","200");
    map.put("message","你已通過驗證了");
    return map;
  }
}

HTTP請求帶上登陸成功后生成token,返回成功:

SpringBoot集成JWT怎么生成token和校驗

HTTP請求帶上無效token或不帶token,返回失?。?/p>

SpringBoot集成JWT怎么生成token和校驗

感謝各位的閱讀,以上就是“SpringBoot集成JWT怎么生成token和校驗”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對SpringBoot集成JWT怎么生成token和校驗這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

本文題目:SpringBoot集成JWT怎么生成token和校驗
文章來源:http://aaarwkj.com/article10/iijhdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作云服務(wù)器、關(guān)鍵詞優(yōu)化、網(wǎng)頁設(shè)計公司、品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)
丰满高潮少妇在线观看| 亚洲精品一区二区av| 精品传媒国产在线观看| 在线精品91国产在线观看| 国内精品自拍亚洲视频| 国产麻豆剧传媒精品av| 久久99久久久久久精品| 国产免费不卡午夜福利在线| 亚洲综合日韩精品国产av| 亚洲av综合色区一区| 日本加勒比系列在线播放| 国产一级无码免费视频| 亚洲av乱码一区二区三四五六七| 日韩亚洲欧美成人一区| 亚洲男人天堂超碰在线| 午夜福利主播一区二区| 国产在线第一页第二页| 国产91黑丝在线播放| 中国毛片一区二区三区| 日本一区二区高清在线观看| 日本不卡一区二区在线播放| 亚洲中文字幕乱码熟女在线| 黄色午夜福利在线观看| 天天精品国产av九九久久久| 免费看av网站一区二区| 久久精品人妻少妇一区二区| 狠狠久久五月综合色和啪| 亚洲欧洲另类美女久久精品| 亚洲热妇热女久久精品| 午夜高清影院免费观看| 亚洲风情亚av在线播放| 亚洲av一本岛在线播放| 星空无限传媒国产最新| 人妻少妇系列一区二区| 欧美日韩免费高清视视频| 這裏隻有无码人妻久久| 久久综合亚洲鲁鲁五月天| 在线最新亚洲日本韩国| 中文字幕成人资源网站| 日韩一区二区精品网站| 爱爱网爱综合日日干夜夜操|