初始化git 基础代码
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module version="4">
|
||||
<component name="FacetManager">
|
||||
<facet type="Spring" name="Spring">
|
||||
<configuration />
|
||||
</facet>
|
||||
</component>
|
||||
</module>
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>heima-leadnews-gateway</artifactId>
|
||||
<groupId>com.heima</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>heima-leadnews-wemedia-gateway</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.heima.wemedia.gateway;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
public class WemediaGatewayAplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(WemediaGatewayAplication.class,args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.heima.wemedia.gateway.filter;
|
||||
|
||||
|
||||
import com.heima.wemedia.gateway.util.AppJwtUtil;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class AuthorizeFilter implements Ordered, GlobalFilter {
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
//1.获取request和response对象
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
ServerHttpResponse response = exchange.getResponse();
|
||||
|
||||
//2.判断是否是登录
|
||||
if(request.getURI().getPath().contains("/login")){
|
||||
//放行
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
//3.获取token
|
||||
String token = request.getHeaders().getFirst("token");
|
||||
|
||||
//4.判断token是否存在
|
||||
if(StringUtils.isBlank(token)){
|
||||
response.setStatusCode(HttpStatus.UNAUTHORIZED);
|
||||
return response.setComplete();
|
||||
}
|
||||
|
||||
//5.判断token是否有效
|
||||
try {
|
||||
Claims claimsBody = AppJwtUtil.getClaimsBody(token);
|
||||
//是否是过期
|
||||
int result = AppJwtUtil.verifyToken(claimsBody);
|
||||
if(result == 1 || result == 2){
|
||||
response.setStatusCode(HttpStatus.UNAUTHORIZED);
|
||||
return response.setComplete();
|
||||
}
|
||||
//获取用户信息
|
||||
Object userId = claimsBody.get("id");
|
||||
//存储header中
|
||||
ServerHttpRequest serverHttpRequest = request.mutate().headers(httpHeaders -> {
|
||||
httpHeaders.add("userId", userId + "");
|
||||
}).build();
|
||||
//重置请求
|
||||
exchange.mutate().request(serverHttpRequest);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
//6.放行
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
/**
|
||||
* 优先级设置 值越小 优先级越高
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package com.heima.wemedia.gateway.util;
|
||||
|
||||
import io.jsonwebtoken.*;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.util.*;
|
||||
|
||||
public class AppJwtUtil {
|
||||
|
||||
// TOKEN的有效期一天(S)
|
||||
private static final int TOKEN_TIME_OUT = 3_600;
|
||||
// 加密KEY
|
||||
private static final String TOKEN_ENCRY_KEY = "MDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjY";
|
||||
// 最小刷新间隔(S)
|
||||
private static final int REFRESH_TIME = 300;
|
||||
|
||||
// 生产ID
|
||||
public static String getToken(Long id) {
|
||||
Map<String, Object> claimMaps = new HashMap<>();
|
||||
claimMaps.put("id", id);
|
||||
long currentTime = System.currentTimeMillis();
|
||||
return Jwts.builder()
|
||||
.setId(UUID.randomUUID().toString())
|
||||
.setIssuedAt(new Date(currentTime)) //签发时间
|
||||
.setSubject("system") //说明
|
||||
.setIssuer("heima") //签发者信息
|
||||
.setAudience("app") //接收用户
|
||||
.compressWith(CompressionCodecs.GZIP) //数据压缩方式
|
||||
.signWith(SignatureAlgorithm.HS512, generalKey()) //加密方式
|
||||
.setExpiration(new Date(currentTime + TOKEN_TIME_OUT * 1000)) //过期时间戳
|
||||
.addClaims(claimMaps) //cla信息
|
||||
.compact();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取token中的claims信息
|
||||
*
|
||||
* @param token
|
||||
* @return
|
||||
*/
|
||||
private static Jws<Claims> getJws(String token) {
|
||||
return Jwts.parser()
|
||||
.setSigningKey(generalKey())
|
||||
.parseClaimsJws(token);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取payload body信息
|
||||
*
|
||||
* @param token
|
||||
* @return
|
||||
*/
|
||||
public static Claims getClaimsBody(String token) throws ExpiredJwtException {
|
||||
return getJws(token).getBody();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取hearder body信息
|
||||
*
|
||||
* @param token
|
||||
* @return
|
||||
*/
|
||||
public static JwsHeader getHeaderBody(String token) {
|
||||
return getJws(token).getHeader();
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否过期
|
||||
*
|
||||
* @param claims
|
||||
* @return -1:有效,0:有效,1:过期,2:过期
|
||||
*/
|
||||
public static int verifyToken(Claims claims) throws Exception {
|
||||
if (claims == null) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
claims.getExpiration().before(new Date());
|
||||
// 需要自动刷新TOKEN
|
||||
if ((claims.getExpiration().getTime() - System.currentTimeMillis()) > REFRESH_TIME * 1000) {
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 由字符串生成加密key
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static SecretKey generalKey() {
|
||||
byte[] encodedKey = Base64.getEncoder().encode(TOKEN_ENCRY_KEY.getBytes());
|
||||
SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
|
||||
return key;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* Map map = new HashMap();
|
||||
map.put("id","11");*/
|
||||
System.out.println(AppJwtUtil.getToken(1102L));
|
||||
Jws<Claims> jws = AppJwtUtil.getJws("eyJhbGciOiJIUzUxMiIsInppcCI6IkdaSVAifQ.H4sIAAAAAAAAADWLQQqEMAwA_5KzhURNt_qb1KZYQSi0wi6Lf9942NsMw3zh6AVW2DYmDGl2WabkZgreCaM6VXzhFBfJMcMARTqsxIG9Z888QLui3e3Tup5Pb81013KKmVzJTGo11nf9n8v4nMUaEY73DzTabjmDAAAA.4SuqQ42IGqCgBai6qd4RaVpVxTlZIWC826QA9kLvt9d-yVUw82gU47HDaSfOzgAcloZedYNNpUcd18Ne8vvjQA");
|
||||
Claims claims = jws.getBody();
|
||||
System.out.println(claims.get("id"));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
server:
|
||||
port: 51602
|
||||
spring:
|
||||
application:
|
||||
name: leadnews-wemedia-gateway
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: 192.168.200.130:8848
|
||||
config:
|
||||
server-addr: 192.168.200.130:8848
|
||||
file-extension: yml
|
||||
Reference in New Issue
Block a user