1 /** 2 * JWT 測試controller 3 * 4 * @author wangmeng 5 * @date 2019/9/2 6 */ 7 @RestController 8 @RequestMapping(value = "/user", produces = { "application/json; charset=UTF-8" }) 9 public class JWTController { 10 11 12 private static final String SECRET = "wangmengtest.@163.com"; 13 14 15 @RequestMapping("/login/{username}/{password}") 16 public Map login(@PathVariable String username, @PathVariable String password) { 17 Map result = new HashMap(); 18 if (username.equals("admin") && password.equals("123456")) { 19 String jwt = Jwts.builder(). 20 setSubject(username). 21 signWith(SignatureAlgorithm.HS512, SECRET). 22 compact(); 23 result.put("token", jwt); 24 } else { 25 result.put("message", "帳號密碼錯誤"); 26 } 27 28 29 return result; 30 } 31 32 33 @RequestMapping("/goods/{token}") 34 public Map verifyToken(@PathVariable String token) { 35 Map result = new HashMap(); 36 Jws<Claims> claimsJws = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token); 37 result.put("username", claimsJws.getBody().getSubject()); 38 return result; 39 } 40 }
原文出處:https://www.cnblogs.com/wang-meng/p/11452620.htmlhtml