tlias管理系统--JWT--生成&校验
This commit is contained in:
@@ -69,6 +69,12 @@
|
|||||||
<version>2.3.3</version>
|
<version>2.3.3</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!--JWT依赖-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
|
<artifactId>jjwt</artifactId>
|
||||||
|
<version>0.9.1</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package com.inmind.controller;
|
||||||
|
|
||||||
|
import com.inmind.pojo.Result;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.servlet.http.Cookie;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import javax.servlet.http.HttpSession;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HttpSession演示
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@RestController
|
||||||
|
public class SessionController {
|
||||||
|
|
||||||
|
//设置Cookie
|
||||||
|
@GetMapping("/c1")
|
||||||
|
public Result cookie1(HttpServletResponse response){
|
||||||
|
response.addCookie(new Cookie("login_username","inmind")); //设置Cookie/响应Cookie
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
//获取Cookie
|
||||||
|
@GetMapping("/c2")
|
||||||
|
public Result cookie2(HttpServletRequest request){
|
||||||
|
Cookie[] cookies = request.getCookies();
|
||||||
|
for (Cookie cookie : cookies) {
|
||||||
|
if(cookie.getName().equals("login_username")){
|
||||||
|
System.out.println("login_username: "+cookie.getValue()); //输出name为login_username的cookie
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@GetMapping("/s1")
|
||||||
|
public Result session1(HttpSession session){//session是服务器根据指定的客户端生成的对应的会话对象
|
||||||
|
log.info("HttpSession-s1: {}", session.hashCode());
|
||||||
|
|
||||||
|
session.setAttribute("loginUser", "tom"); //往session中存储数据
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/s2")
|
||||||
|
public Result session2(HttpServletRequest request){
|
||||||
|
HttpSession session = request.getSession();
|
||||||
|
log.info("HttpSession-s2: {}", session.hashCode());
|
||||||
|
|
||||||
|
Object loginUser = session.getAttribute("loginUser"); //从session中获取数据
|
||||||
|
log.info("loginUser: {}", loginUser);
|
||||||
|
return Result.success(loginUser);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,14 @@
|
|||||||
package com.inmind;
|
package com.inmind;
|
||||||
|
|
||||||
|
import io.jsonwebtoken.Claims;
|
||||||
|
import io.jsonwebtoken.Jwts;
|
||||||
|
import io.jsonwebtoken.SignatureAlgorithm;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
@@ -25,4 +31,26 @@ class TliasWebManagementApplicationTests {
|
|||||||
System.out.println(ossAccessKeyId);
|
System.out.println(ossAccessKeyId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testJWT(){//生成JWT
|
||||||
|
Map<String, Object> claim = new HashMap<>();
|
||||||
|
claim.put("id",1);
|
||||||
|
claim.put("name", "zhangsan");
|
||||||
|
String jwt = Jwts.builder()
|
||||||
|
.signWith(SignatureAlgorithm.HS256, "inmind")//签名算法
|
||||||
|
.setClaims(claim)//定义内容数据(载荷)
|
||||||
|
.setExpiration(new Date(System.currentTimeMillis()))
|
||||||
|
.compact();
|
||||||
|
System.out.println(jwt);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void parseJWT(){//解析JWT,最重要的就是输入正确的签名密钥
|
||||||
|
String jwt = "eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiemhhbmdzYW4iLCJpZCI6MSwiZXhwIjoxNzY0NDg3MzIzfQ.Ay2t3bLEX8QPl2MxhsdxoeEzKL-x5ROs4mRTBtuPZWw\n";
|
||||||
|
Claims claims = Jwts.parser()
|
||||||
|
.setSigningKey("inmind")
|
||||||
|
.parseClaimsJws(jwt).getBody();
|
||||||
|
System.out.println(claims);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user