初始化加鉴权登录
commit
b1be20acea
|
@ -0,0 +1,40 @@
|
||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea/modules.xml
|
||||||
|
.idea/jarRepositories.xml
|
||||||
|
.idea/compiler.xml
|
||||||
|
.idea/libraries/
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
/.idea
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
|
@ -0,0 +1,8 @@
|
||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
|
@ -0,0 +1,13 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding">
|
||||||
|
<file url="file://$PROJECT_DIR$/spider-auth/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/spider-common/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/spider-gateway/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/spider-modules/spider-system/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/spider-modules/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/spider-modules/src/main/resources" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
|
<component name="MavenProjectsManager">
|
||||||
|
<option name="originalFiles">
|
||||||
|
<list>
|
||||||
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,69 @@
|
||||||
|
<?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">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.spider</groupId>
|
||||||
|
<artifactId>spider-car-cloud</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
<modules>
|
||||||
|
<module>spider-common</module>
|
||||||
|
<module>spider-gateway</module>
|
||||||
|
<module>spider-auth</module>
|
||||||
|
<module>spider-modules</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<!-- 规定SpringBoot版本 -->
|
||||||
|
<!-- 父级pom文件 主要用于规定项目依赖的各个版本,用于进行项目版本约束 -->
|
||||||
|
<parent>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<version>2.6.2</version>
|
||||||
|
<relativePath/>
|
||||||
|
</parent>
|
||||||
|
<!-- 依赖声明 -->
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<!-- SpringCloud 微服务 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-dependencies</artifactId>
|
||||||
|
<version>2021.0.0</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringCloud Alibaba 微服务 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||||
|
<version>2021.1</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
<!-- Alibaba Nacos 配置 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.nacos</groupId>
|
||||||
|
<artifactId>nacos-client</artifactId>
|
||||||
|
<version>2.0.4</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.spider</groupId>
|
||||||
|
<artifactId>spider-common</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,33 @@
|
||||||
|
<?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">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.spider</groupId>
|
||||||
|
<artifactId>spider-car-cloud</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>spider-auth</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- 项目公共 依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.spider</groupId>
|
||||||
|
<artifactId>spider-common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringBoot Web-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,33 @@
|
||||||
|
package com.spider.auth;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||||
|
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 📝 TODO 权限认证
|
||||||
|
* 🕟 2024/1/11 10:22
|
||||||
|
* 👦 Lxj
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableFeignClients(basePackages = "com.spider.**")
|
||||||
|
public class SpiderAuthApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SpiderAuthApplication.class,args);
|
||||||
|
System.out.println("蜘蛛权限认证启动成功\n" +
|
||||||
|
"# __ __ \n" +
|
||||||
|
"# | \\ | \\ \n" +
|
||||||
|
"# _______ ______ \\$$ ____| $$ ______ ______ \n" +
|
||||||
|
"# / \\ / \\ | \\ / $$ / \\ / \\ \n" +
|
||||||
|
"# | $$$$$$$| $$$$$$\\| $$| $$$$$$$| $$$$$$\\| $$$$$$\\\n" +
|
||||||
|
"# \\$$ \\ | $$ | $$| $$| $$ | $$| $$ $$| $$ \\$$\n" +
|
||||||
|
"# _\\$$$$$$\\| $$__/ $$| $$| $$__| $$| $$$$$$$$| $$ \n" +
|
||||||
|
"# | $$| $$ $$| $$ \\$$ $$ \\$$ \\| $$ \n" +
|
||||||
|
"# \\$$$$$$$ | $$$$$$$ \\$$ \\$$$$$$$ \\$$$$$$$ \\$$ \n" +
|
||||||
|
"# | $$ \n" +
|
||||||
|
"# | $$ \n" +
|
||||||
|
"# \\$$ \n");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,47 @@
|
||||||
|
package com.spider.auth.controller;
|
||||||
|
|
||||||
|
import com.spider.auth.service.AuthService;
|
||||||
|
import com.spider.common.domain.User;
|
||||||
|
import com.spider.common.domain.request.LoginReq;
|
||||||
|
import com.spider.common.domain.response.JwtResponse;
|
||||||
|
import com.spider.common.result.Result;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import javax.servlet.ServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 📝 TODO
|
||||||
|
* 🕟 2024/1/19 9:55
|
||||||
|
* 👦 Lxj
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
public class AuthController {
|
||||||
|
|
||||||
|
private final AuthService authService;
|
||||||
|
|
||||||
|
public AuthController(AuthService authService) {
|
||||||
|
this.authService = authService;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 鉴权登录
|
||||||
|
* @param loginReq
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PostMapping("/login")
|
||||||
|
public Result<JwtResponse> login(@RequestBody LoginReq loginReq){
|
||||||
|
return authService.login(loginReq);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户信息
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@GetMapping("/info")
|
||||||
|
public Result<String> info(){
|
||||||
|
return authService.info();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.spider.auth.service;
|
||||||
|
|
||||||
|
import com.spider.common.domain.User;
|
||||||
|
import com.spider.common.domain.request.LoginReq;
|
||||||
|
import com.spider.common.domain.response.JwtResponse;
|
||||||
|
import com.spider.common.result.Result;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 📝 TODO
|
||||||
|
* 🕟 2024/1/19 9:55
|
||||||
|
* 👦 Lxj
|
||||||
|
*/
|
||||||
|
public interface AuthService {
|
||||||
|
|
||||||
|
Result<JwtResponse> login(LoginReq loginReq);
|
||||||
|
|
||||||
|
Result<String> info();
|
||||||
|
}
|
|
@ -0,0 +1,108 @@
|
||||||
|
package com.spider.auth.service.impl;
|
||||||
|
|
||||||
|
import com.alibaba.nacos.common.utils.UuidUtils;
|
||||||
|
import com.spider.auth.service.AuthService;
|
||||||
|
import com.spider.common.constant.JwtConstants;
|
||||||
|
import com.spider.common.constant.TokenConstants;
|
||||||
|
import com.spider.common.domain.User;
|
||||||
|
import com.spider.common.domain.request.LoginReq;
|
||||||
|
import com.spider.common.domain.response.JwtResponse;
|
||||||
|
import com.spider.common.remote.UserRemote;
|
||||||
|
import com.spider.common.result.Result;
|
||||||
|
import com.spider.common.utils.JsonUtil;
|
||||||
|
import com.spider.common.utils.JwtUtils;
|
||||||
|
import com.spider.common.utils.StringUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 📝 TODO
|
||||||
|
* 🕟 2024/1/19 9:55
|
||||||
|
* 👦 Lxj
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class AuthServiceImpl implements AuthService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserRemote userRemote;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RedisTemplate<String,Object> redisTemplate;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private HttpServletRequest request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录
|
||||||
|
* @param loginReq
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Result<JwtResponse> login(LoginReq loginReq) {
|
||||||
|
|
||||||
|
//判空
|
||||||
|
if(StringUtils.isNull(loginReq)){
|
||||||
|
return Result.error("请输入用户名密码");
|
||||||
|
}
|
||||||
|
|
||||||
|
//查询用户信息
|
||||||
|
Result<User> userInfo = userRemote.info(loginReq.getUserAccount());
|
||||||
|
|
||||||
|
User user = userInfo.getData();
|
||||||
|
|
||||||
|
if(null == user){
|
||||||
|
return Result.error(userInfo.getMsg());
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.success(this.createToken(user));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户信息
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Result<String> info() {
|
||||||
|
String userAccount = request.getHeader(JwtConstants.DETAILS_USERNAME);
|
||||||
|
|
||||||
|
if(null == userAccount){
|
||||||
|
return Result.error("获取用户信息异常");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.success(userAccount);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成token
|
||||||
|
* @param user
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public JwtResponse createToken(User user){
|
||||||
|
HashMap<String, Object> hashMap = new HashMap<>();
|
||||||
|
|
||||||
|
String userKey = UuidUtils.generateUuid();
|
||||||
|
hashMap.put(JwtConstants.USER_KEY,userKey);
|
||||||
|
hashMap.put(JwtConstants.DETAILS_USER_ID,user.getUserId());
|
||||||
|
hashMap.put(JwtConstants.DETAILS_USERNAME,user.getUserAccount());
|
||||||
|
|
||||||
|
String token = JwtUtils.createToken(hashMap);
|
||||||
|
|
||||||
|
redisTemplate.opsForValue().set(
|
||||||
|
TokenConstants.LOGIN_TOKEN_KEY+userKey,
|
||||||
|
JsonUtil.parseJSONObject(user),
|
||||||
|
TokenConstants.EXPIRATION,
|
||||||
|
TimeUnit.MINUTES
|
||||||
|
);
|
||||||
|
|
||||||
|
return JwtResponse.builder().token(token)
|
||||||
|
.existTime(TokenConstants.EXPIRATION+""+TimeUnit.MINUTES)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
# Tomcat
|
||||||
|
server:
|
||||||
|
port: 9080
|
||||||
|
# Spring
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
# 应用名称
|
||||||
|
name: spider-auth
|
||||||
|
profiles:
|
||||||
|
# 环境配置
|
||||||
|
active: dev
|
||||||
|
main:
|
||||||
|
# 允许使用循环引用
|
||||||
|
allow-circular-references: true
|
||||||
|
# 允许定义相同的bean对象 去覆盖原有的
|
||||||
|
allow-bean-definition-overriding: true
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
# 服务注册地址
|
||||||
|
server-addr: 1.117.62.128:8848
|
||||||
|
config:
|
||||||
|
# 配置中心地址
|
||||||
|
server-addr: 1.117.62.128:8848
|
||||||
|
# 配置文件格式
|
||||||
|
file-extension: yml
|
||||||
|
# 共享配置
|
||||||
|
shared-configs:
|
||||||
|
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
|
@ -0,0 +1,128 @@
|
||||||
|
<?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">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.spider</groupId>
|
||||||
|
<artifactId>spider-car-cloud</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>spider-common</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- bootstrap 启动器 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-bootstrap</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringCloud Alibaba Nacos -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringCloud Alibaba Nacos Config -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringCloud Alibaba Sentinel -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- 负载均衡-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringCloud Openfeign -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- JWT -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
|
<artifactId>jjwt</artifactId>
|
||||||
|
<version>0.9.1</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- Alibaba Fastjson -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>fastjson</artifactId>
|
||||||
|
<version>1.2.80</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringBoot Boot Redis -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- Druid -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>druid-spring-boot-starter</artifactId>
|
||||||
|
<version>1.2.8</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Mysql Connector -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>5.1.32</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Mybatis 依赖配置 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis.spring.boot</groupId>
|
||||||
|
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||||
|
<version>2.2.2</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Hibernate Validator -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-validation</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- Apache Lang3 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- lombok依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- SpringBoot 测试框架 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.swagger</groupId>
|
||||||
|
<artifactId>swagger-annotations</artifactId>
|
||||||
|
<version>1.5.20</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.fastjson2</groupId>
|
||||||
|
<artifactId>fastjson2</artifactId>
|
||||||
|
<version>2.0.43</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,49 @@
|
||||||
|
package com.spider.common.config;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson2.JSON;
|
||||||
|
import com.alibaba.fastjson2.JSONReader;
|
||||||
|
import com.alibaba.fastjson2.JSONWriter;
|
||||||
|
import com.alibaba.fastjson2.filter.Filter;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||||
|
import org.springframework.data.redis.serializer.SerializationException;
|
||||||
|
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Redis使用FastJson序列化
|
||||||
|
*
|
||||||
|
* @author ruoyi
|
||||||
|
*/
|
||||||
|
public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T> {
|
||||||
|
public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
|
||||||
|
|
||||||
|
static final Filter AUTO_TYPE_FILTER = JSONReader.autoTypeFilter(
|
||||||
|
"org.springframework", "com");
|
||||||
|
|
||||||
|
private Class<T> clazz;
|
||||||
|
|
||||||
|
public FastJson2JsonRedisSerializer(Class<T> clazz) {
|
||||||
|
super();
|
||||||
|
this.clazz = clazz;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] serialize (T t) throws SerializationException {
|
||||||
|
if (t == null) {
|
||||||
|
return new byte[0];
|
||||||
|
}
|
||||||
|
return JSON.toJSONString(t, JSONWriter.Feature.WriteClassName).getBytes(DEFAULT_CHARSET);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T deserialize (byte[] bytes) throws SerializationException {
|
||||||
|
if (bytes == null || bytes.length <= 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String str = new String(bytes, DEFAULT_CHARSET);
|
||||||
|
|
||||||
|
return JSON.parseObject(str, clazz, AUTO_TYPE_FILTER);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
package com.spider.common.config;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||||
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
||||||
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class RedisConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
|
||||||
|
RedisTemplate<String, Object> template = new RedisTemplate<>();
|
||||||
|
template.setConnectionFactory(factory);
|
||||||
|
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new
|
||||||
|
Jackson2JsonRedisSerializer(Object.class);
|
||||||
|
ObjectMapper om = new ObjectMapper();
|
||||||
|
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
||||||
|
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
||||||
|
jackson2JsonRedisSerializer.setObjectMapper(om);
|
||||||
|
|
||||||
|
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
|
||||||
|
// key采用String的序列化方式
|
||||||
|
template.setKeySerializer(stringRedisSerializer);
|
||||||
|
// hash的key也采用String的序列化方式
|
||||||
|
template.setHashKeySerializer(stringRedisSerializer);
|
||||||
|
// value序列化方式采用jackson
|
||||||
|
template.setValueSerializer(jackson2JsonRedisSerializer);
|
||||||
|
// hash的value序列化方式采用jackson
|
||||||
|
template.setHashValueSerializer(jackson2JsonRedisSerializer);
|
||||||
|
template.afterPropertiesSet();
|
||||||
|
|
||||||
|
return template;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.spider.common.constant;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 系统常量
|
||||||
|
* @author DongZl
|
||||||
|
*/
|
||||||
|
public class Constants {
|
||||||
|
/**
|
||||||
|
* 成功标记
|
||||||
|
*/
|
||||||
|
public static final Integer SUCCESS = 200;
|
||||||
|
public static final String SUCCESS_MSG = "操作成功";
|
||||||
|
/**
|
||||||
|
* 失败标记
|
||||||
|
*/
|
||||||
|
public static final Integer ERROR = 500;
|
||||||
|
public static final String ERROR_MSG = "操作异常";
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.spider.common.constant;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: Jwt常量
|
||||||
|
*/
|
||||||
|
public class JwtConstants {
|
||||||
|
/**
|
||||||
|
* 用户ID字段
|
||||||
|
*/
|
||||||
|
public static final String DETAILS_USER_ID = "user_id";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户名字段
|
||||||
|
*/
|
||||||
|
public static final String DETAILS_USERNAME = "username";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户标识
|
||||||
|
*/
|
||||||
|
public static final String USER_KEY = "user_key";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 令牌秘钥
|
||||||
|
*/
|
||||||
|
public final static String SECRET = "abcdefghijklmnopqrstuvwxyz";
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.spider.common.constant;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 📝 TODO 服务名
|
||||||
|
* 🕟 2024/1/19 9:44
|
||||||
|
* 👦 Lxj
|
||||||
|
*/
|
||||||
|
public class ServerConstants {
|
||||||
|
|
||||||
|
public static final String SYSTEM_NAME = "spider-system";
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.spider.common.constant;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 令牌常量
|
||||||
|
*/
|
||||||
|
public class TokenConstants {
|
||||||
|
/**
|
||||||
|
* 缓存有效期,默认720(分钟)
|
||||||
|
*/
|
||||||
|
public final static long EXPIRATION = 720;
|
||||||
|
/**
|
||||||
|
* 缓存刷新时间,默认120(分钟)
|
||||||
|
*/
|
||||||
|
public final static long REFRESH_TIME = 120;
|
||||||
|
/**
|
||||||
|
* 权限缓存前缀
|
||||||
|
*/
|
||||||
|
public final static String LOGIN_TOKEN_KEY = "login_tokens:";
|
||||||
|
/**
|
||||||
|
* token标识
|
||||||
|
*/
|
||||||
|
public static final String TOKEN = "token";
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
package com.spider.common.domain.request;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 📝 TODO
|
||||||
|
* 🕟 2024/1/12 9:40
|
||||||
|
* 👦 Lxj
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
public class LoginReq {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户名
|
||||||
|
*/
|
||||||
|
private String userAccount;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 密码
|
||||||
|
*/
|
||||||
|
private String userPwd;
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.spider.common.domain.response;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class JwtResponse {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* token
|
||||||
|
*/
|
||||||
|
private String token;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 过期时间
|
||||||
|
*/
|
||||||
|
private String existTime;
|
||||||
|
}
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.spider.common.remote;
|
||||||
|
|
||||||
|
import com.spider.common.constant.ServerConstants;
|
||||||
|
import com.spider.common.domain.User;
|
||||||
|
import com.spider.common.result.Result;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 📝 TODO
|
||||||
|
* 🕟 2024/1/19 9:43
|
||||||
|
* 👦 Lxj
|
||||||
|
*/
|
||||||
|
@FeignClient(
|
||||||
|
name = ServerConstants.SYSTEM_NAME
|
||||||
|
)
|
||||||
|
public interface UserRemote {
|
||||||
|
/**
|
||||||
|
* 查询用户信息
|
||||||
|
* @param userAccount
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PostMapping("/user/info/{userAccount}")
|
||||||
|
Result<User> info(@PathVariable("userAccount") String userAccount);
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
package com.spider.common.result;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 列表返回结果集
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class PageResult<T> implements Serializable {
|
||||||
|
/**
|
||||||
|
* 总条数
|
||||||
|
*/
|
||||||
|
private long total;
|
||||||
|
/**
|
||||||
|
* 结果集合
|
||||||
|
*/
|
||||||
|
private List<T> list;
|
||||||
|
public PageResult() {
|
||||||
|
}
|
||||||
|
public PageResult(long total, List<T> list) {
|
||||||
|
this.total = total;
|
||||||
|
this.list = list;
|
||||||
|
}
|
||||||
|
public static <T> PageResult<T> toPageResult(long total, List<T> list){
|
||||||
|
|
||||||
|
return new PageResult(total , list);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> Result<PageResult<T>> toResult(long total, List<T> list){
|
||||||
|
return Result.success(PageResult.toPageResult(total,list));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,53 @@
|
||||||
|
package com.spider.common.result;
|
||||||
|
|
||||||
|
import com.spider.common.constant.Constants;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 响应信息主体
|
||||||
|
* @author DongZl
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class Result<T> implements Serializable {
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
/** 成功 */
|
||||||
|
public static final int SUCCESS = Constants.SUCCESS;
|
||||||
|
/** 失败 */
|
||||||
|
public static final int FAIL = Constants.ERROR;
|
||||||
|
private int code;
|
||||||
|
private String msg;
|
||||||
|
private T data;
|
||||||
|
public static <T> Result<T> success() {
|
||||||
|
return restResult(null, SUCCESS, Constants.SUCCESS_MSG);
|
||||||
|
}
|
||||||
|
public static <T> Result<T> success(T data) {
|
||||||
|
return restResult(data, SUCCESS, Constants.SUCCESS_MSG);
|
||||||
|
}
|
||||||
|
public static <T> Result<T> success(T data, String msg) {
|
||||||
|
return restResult(data, SUCCESS, msg);
|
||||||
|
}
|
||||||
|
public static <T> Result<T> error() {
|
||||||
|
return restResult(null, FAIL, Constants.ERROR_MSG);
|
||||||
|
}
|
||||||
|
public static <T> Result<T> error(String msg) {
|
||||||
|
return restResult(null, FAIL, msg);
|
||||||
|
}
|
||||||
|
public static <T> Result<T> error(T data) {
|
||||||
|
return restResult(data, FAIL, Constants.ERROR_MSG);
|
||||||
|
}
|
||||||
|
public static <T> Result<T> error(T data, String msg) {
|
||||||
|
return restResult(data, FAIL, msg);
|
||||||
|
}
|
||||||
|
public static <T> Result<T> error(int code, String msg) {
|
||||||
|
return restResult(null, code, msg);
|
||||||
|
}
|
||||||
|
private static <T> Result<T> restResult(T data, int code, String msg) {
|
||||||
|
Result<T> apiResult = new Result<>();
|
||||||
|
apiResult.setCode(code);
|
||||||
|
apiResult.setData(data);
|
||||||
|
apiResult.setMsg(msg);
|
||||||
|
return apiResult;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,172 @@
|
||||||
|
package com.spider.common.utils;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||||
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||||
|
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||||
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description 由于习惯了用fastjson处理JSON数据,突然改成用jackson,有些不适应,所以打算用jackson封装出类似fastjson里的方法进行使用
|
||||||
|
* @ClassName JsonUtil
|
||||||
|
* @Author yuhuofei
|
||||||
|
* @Date 2023/8/19 14:36
|
||||||
|
* @Version 1.0
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class JsonUtil {
|
||||||
|
|
||||||
|
private static ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
// 时间日期格式
|
||||||
|
private static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
||||||
|
|
||||||
|
//以静态代码块初始化
|
||||||
|
static {
|
||||||
|
//对象的所有字段全部列入序列化
|
||||||
|
objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
|
||||||
|
//取消默认转换timestamps形式
|
||||||
|
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
|
||||||
|
//忽略空Bean转json的错误
|
||||||
|
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
|
||||||
|
//所有的日期格式都统一为以下的格式,即yyyy-MM-dd HH:mm:ss
|
||||||
|
objectMapper.setDateFormat(new SimpleDateFormat(STANDARD_FORMAT));
|
||||||
|
//忽略 在json字符串中存在,但在java对象中不存在对应属性的情况。防止错误
|
||||||
|
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**===========================以下是从JSON中获取对象====================================*/
|
||||||
|
public static <T> T parseObject(String jsonString, Class<T> object) {
|
||||||
|
T t = null;
|
||||||
|
try {
|
||||||
|
t = objectMapper.readValue(jsonString, object);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
log.error("JsonString转为自定义对象失败:{}", e.getMessage());
|
||||||
|
}
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T parseObject(File file, Class<T> object) {
|
||||||
|
T t = null;
|
||||||
|
try {
|
||||||
|
t = objectMapper.readValue(file, object);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("从文件中读取json字符串转为自定义对象失败:{}", e.getMessage());
|
||||||
|
}
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
//将json数组字符串转为指定对象List列表或者Map集合
|
||||||
|
public static <T> T parseJSONArray(String jsonArray, TypeReference<T> reference) {
|
||||||
|
T t = null;
|
||||||
|
try {
|
||||||
|
t = objectMapper.readValue(jsonArray, reference);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
log.error("JSONArray转为List列表或者Map集合失败:{}", e.getMessage());
|
||||||
|
}
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**=================================以下是将对象转为JSON=====================================*/
|
||||||
|
public static String toJSONString(Object object) {
|
||||||
|
String jsonString = null;
|
||||||
|
try {
|
||||||
|
jsonString = objectMapper.writeValueAsString(object);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
log.error("Object转JSONString失败:{}", e.getMessage());
|
||||||
|
}
|
||||||
|
return jsonString;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] toByteArray(Object object) {
|
||||||
|
byte[] bytes = null;
|
||||||
|
try {
|
||||||
|
bytes = objectMapper.writeValueAsBytes(object);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
log.error("Object转ByteArray失败:{}", e.getMessage());
|
||||||
|
}
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void objectToFile(File file, Object object) {
|
||||||
|
try {
|
||||||
|
objectMapper.writeValue(file, object);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
log.error("Object写入文件失败:{}", e.getMessage());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**=============================以下是与JsonNode相关的=======================================*/
|
||||||
|
//JsonNode和JSONObject一样,都是JSON树形模型,只不过在jackson中,存在的是JsonNode
|
||||||
|
public static JsonNode parseJSONObject(String jsonString) {
|
||||||
|
JsonNode jsonNode = null;
|
||||||
|
try {
|
||||||
|
jsonNode = objectMapper.readTree(jsonString);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
log.error("JSONString转为JsonNode失败:{}", e.getMessage());
|
||||||
|
}
|
||||||
|
return jsonNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static JsonNode parseJSONObject(Object object) {
|
||||||
|
JsonNode jsonNode = objectMapper.valueToTree(object);
|
||||||
|
return jsonNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String toJSONString(JsonNode jsonNode) {
|
||||||
|
String jsonString = null;
|
||||||
|
try {
|
||||||
|
jsonString = objectMapper.writeValueAsString(jsonNode);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
log.error("JsonNode转JSONString失败:{}", e.getMessage());
|
||||||
|
}
|
||||||
|
return jsonString;
|
||||||
|
}
|
||||||
|
|
||||||
|
//JsonNode是一个抽象类,不能实例化,创建JSON树形模型,得用JsonNode的子类ObjectNode,用法和JSONObject大同小异
|
||||||
|
public static ObjectNode newJSONObject() {
|
||||||
|
return objectMapper.createObjectNode();
|
||||||
|
}
|
||||||
|
|
||||||
|
//创建JSON数组对象,就像JSONArray一样用
|
||||||
|
public static ArrayNode newJSONArray() {
|
||||||
|
return objectMapper.createArrayNode();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**===========以下是从JsonNode对象中获取key值的方法,个人觉得有点多余,直接用JsonNode自带的取值方法会好点,出于纠结症,还是补充进来了*/
|
||||||
|
public static String getString(JsonNode jsonObject, String key) {
|
||||||
|
String s = jsonObject.get(key).asText();
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Integer getInteger(JsonNode jsonObject, String key) {
|
||||||
|
Integer i = jsonObject.get(key).asInt();
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Boolean getBoolean(JsonNode jsonObject, String key) {
|
||||||
|
Boolean bool = jsonObject.get(key).asBoolean();
|
||||||
|
return bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static JsonNode getJSONObject(JsonNode jsonObject, String key) {
|
||||||
|
JsonNode json = jsonObject.get(key);
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,106 @@
|
||||||
|
package com.spider.common.utils;
|
||||||
|
|
||||||
|
import com.spider.common.constant.JwtConstants;
|
||||||
|
import io.jsonwebtoken.Claims;
|
||||||
|
import io.jsonwebtoken.Jwts;
|
||||||
|
import io.jsonwebtoken.SignatureAlgorithm;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: Jwt工具类
|
||||||
|
* @author DongZl
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class JwtUtils {
|
||||||
|
|
||||||
|
public static String secret = JwtConstants.SECRET;
|
||||||
|
/**
|
||||||
|
* 从数据声明生成令牌
|
||||||
|
*
|
||||||
|
* @param claims 数据声明
|
||||||
|
* @return 令牌
|
||||||
|
*/
|
||||||
|
public static String createToken(Map<String, Object> claims){
|
||||||
|
String token = Jwts.builder().setClaims(claims).signWith(SignatureAlgorithm.HS512, secret).compact();
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 从令牌中获取数据声明
|
||||||
|
*
|
||||||
|
* @param token 令牌
|
||||||
|
* @return 数据声明
|
||||||
|
*/
|
||||||
|
public static Claims parseToken(String token){
|
||||||
|
return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 根据令牌获取用户标识
|
||||||
|
*
|
||||||
|
* @param token 令牌
|
||||||
|
* @return 用户ID
|
||||||
|
*/
|
||||||
|
public static String getUserKey(String token){
|
||||||
|
Claims claims = parseToken(token);
|
||||||
|
return getValue(claims, JwtConstants.USER_KEY);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 根据令牌获取用户标识
|
||||||
|
*
|
||||||
|
* @param claims 身份信息
|
||||||
|
* @return 用户ID
|
||||||
|
*/
|
||||||
|
public static String getUserKey(Claims claims){
|
||||||
|
return getValue(claims, JwtConstants.USER_KEY);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 根据令牌获取用户ID
|
||||||
|
*
|
||||||
|
* @param token 令牌
|
||||||
|
* @return 用户ID
|
||||||
|
*/
|
||||||
|
public static String getUserId(String token){
|
||||||
|
Claims claims = parseToken(token);
|
||||||
|
return getValue(claims, JwtConstants.DETAILS_USER_ID);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 根据身份信息获取用户ID
|
||||||
|
*
|
||||||
|
* @param claims 身份信息
|
||||||
|
* @return 用户ID
|
||||||
|
*/
|
||||||
|
public static String getUserId(Claims claims){
|
||||||
|
return getValue(claims, JwtConstants.DETAILS_USER_ID);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 根据令牌获取用户名
|
||||||
|
*
|
||||||
|
* @param token 令牌
|
||||||
|
* @return 用户名
|
||||||
|
*/
|
||||||
|
public static String getUserName(String token){
|
||||||
|
Claims claims = parseToken(token);
|
||||||
|
return getValue(claims, JwtConstants.DETAILS_USERNAME);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 根据身份信息获取用户名
|
||||||
|
*
|
||||||
|
* @param claims 身份信息
|
||||||
|
* @return 用户名
|
||||||
|
*/
|
||||||
|
public static String getUserName(Claims claims){
|
||||||
|
return getValue(claims, JwtConstants.DETAILS_USERNAME);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 根据身份信息获取键值
|
||||||
|
*
|
||||||
|
* @param claims 身份信息
|
||||||
|
* @param key 键
|
||||||
|
* @return 值
|
||||||
|
*/
|
||||||
|
public static String getValue(Claims claims, String key){
|
||||||
|
Object obj = claims.get(key);
|
||||||
|
return obj == null ? "" : obj.toString();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,68 @@
|
||||||
|
package com.spider.common.utils;
|
||||||
|
|
||||||
|
import org.springframework.util.AntPathMatcher;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 字符串处理工具类
|
||||||
|
*/
|
||||||
|
public class StringUtils extends org.apache.commons.lang3.StringUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* * 判断一个对象是否为空
|
||||||
|
*
|
||||||
|
* @param object Object
|
||||||
|
* @return true:为空 false:非空
|
||||||
|
*/
|
||||||
|
public static boolean isNull(Object object) {
|
||||||
|
return object == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* * 判断一个Collection是否为空, 包含List,Set,Queue
|
||||||
|
*
|
||||||
|
* @param coll 要判断的Collection
|
||||||
|
* @return true:为空 false:非空
|
||||||
|
*/
|
||||||
|
public static boolean isEmpty(Collection<?> coll) {
|
||||||
|
return isNull(coll) || coll.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查找指定字符串是否匹配指定字符串列表中的任意一个字符串
|
||||||
|
*
|
||||||
|
* @param str 指定字符串
|
||||||
|
* @param strs 需要检查的字符串数组
|
||||||
|
* @return 是否匹配
|
||||||
|
*/
|
||||||
|
public static boolean matches(String str, List<String> strs) {
|
||||||
|
if (isEmpty(str) || isEmpty(strs)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (String pattern : strs) {
|
||||||
|
if (isMatch(pattern, str))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断url是否与规则配置:
|
||||||
|
* ? 表示单个字符;
|
||||||
|
* * 表示一层路径内的任意字符串,不可跨层级;
|
||||||
|
* ** 表示任意层路径;
|
||||||
|
*
|
||||||
|
* @param pattern 匹配规则
|
||||||
|
* @param url 需要匹配的url
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static boolean isMatch(String pattern, String url) {
|
||||||
|
AntPathMatcher matcher = new AntPathMatcher();
|
||||||
|
return matcher.match(pattern, url);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,2 @@
|
||||||
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||||
|
com.spider.common.config.RedisConfig
|
|
@ -0,0 +1,60 @@
|
||||||
|
<?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">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.spider</groupId>
|
||||||
|
<artifactId>spider-car-cloud</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>spider-gateway</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- 公共依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.spider</groupId>
|
||||||
|
<artifactId>spider-common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringCloud Gateway -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- SpringCloud Alibaba Sentinel Gateway -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- 引入阿里巴巴sentinel限流 依赖-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.csp</groupId>
|
||||||
|
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>${project.artifactId}</finalName>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>repackage</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,36 @@
|
||||||
|
package com.spider.gateway;
|
||||||
|
|
||||||
|
import com.spider.gateway.config.GatewaySentinelConfig;
|
||||||
|
import com.spider.gateway.config.IgnoreWhiteConfig;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 📝 TODO 网关
|
||||||
|
* 🕟 2024/1/11 9:48
|
||||||
|
* 👦 Lxj
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
@Import({GatewaySentinelConfig.class, IgnoreWhiteConfig.class})
|
||||||
|
public class SpiderGatewayApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
SpringApplication.run(SpiderGatewayApplication.class,args);
|
||||||
|
System.out.println("蜘蛛网关启动成功\n" +
|
||||||
|
"# __ __ \n" +
|
||||||
|
"# | \\ | \\ \n" +
|
||||||
|
"# _______ ______ \\$$ ____| $$ ______ ______ \n" +
|
||||||
|
"# / \\ / \\ | \\ / $$ / \\ / \\ \n" +
|
||||||
|
"# | $$$$$$$| $$$$$$\\| $$| $$$$$$$| $$$$$$\\| $$$$$$\\\n" +
|
||||||
|
"# \\$$ \\ | $$ | $$| $$| $$ | $$| $$ $$| $$ \\$$\n" +
|
||||||
|
"# _\\$$$$$$\\| $$__/ $$| $$| $$__| $$| $$$$$$$$| $$ \n" +
|
||||||
|
"# | $$| $$ $$| $$ \\$$ $$ \\$$ \\| $$ \n" +
|
||||||
|
"# \\$$$$$$$ | $$$$$$$ \\$$ \\$$$$$$$ \\$$$$$$$ \\$$ \n" +
|
||||||
|
"# | $$ \n" +
|
||||||
|
"# | $$ \n" +
|
||||||
|
"# \\$$ \n");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,71 @@
|
||||||
|
package com.spider.gateway.config;
|
||||||
|
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
|
||||||
|
import com.alibaba.csp.sentinel.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler;
|
||||||
|
import org.springframework.beans.factory.ObjectProvider;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.http.codec.ServerCodecConfigurer;
|
||||||
|
import org.springframework.web.reactive.result.view.ViewResolver;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecation: 网关限流控件
|
||||||
|
* @author DongZl
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class GatewaySentinelConfig {
|
||||||
|
/**
|
||||||
|
* 查看解析器
|
||||||
|
*/
|
||||||
|
private final List<ViewResolver> viewResolvers;
|
||||||
|
/**
|
||||||
|
* 服务器编解码器配置
|
||||||
|
*/
|
||||||
|
private final ServerCodecConfigurer serverCodecConfigurer;
|
||||||
|
public GatewaySentinelConfig(ObjectProvider<List<ViewResolver>> viewResolversProvider,
|
||||||
|
ServerCodecConfigurer serverCodecConfigurer) {
|
||||||
|
this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
|
||||||
|
this.serverCodecConfigurer = serverCodecConfigurer;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Sentinel 网关块异常处理程序
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||||
|
public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
|
||||||
|
// 给 Spring Cloud Gateway 注册块异常处理程序。
|
||||||
|
return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化网关配置
|
||||||
|
*/
|
||||||
|
@PostConstruct
|
||||||
|
public void doInit() {
|
||||||
|
initGatewayRules();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 配置限流规则
|
||||||
|
*/
|
||||||
|
private void initGatewayRules() {
|
||||||
|
Set<GatewayFlowRule> rules = new HashSet<>();
|
||||||
|
rules.add(new GatewayFlowRule("cloud-user")
|
||||||
|
// 限流阈值
|
||||||
|
.setCount(1)
|
||||||
|
// 统计时间窗口,单位是秒,默认是 1 秒
|
||||||
|
.setIntervalSec(5)
|
||||||
|
);
|
||||||
|
//添加到限流规则当中
|
||||||
|
GatewayRuleManager.loadRules(rules);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
package com.spider.gateway.config;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description: 放行白名单配置
|
||||||
|
* @author DongZl
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@RefreshScope
|
||||||
|
@ConfigurationProperties(prefix = "ignore")
|
||||||
|
@Data
|
||||||
|
@Log4j2
|
||||||
|
public class IgnoreWhiteConfig {
|
||||||
|
/**
|
||||||
|
* 放行白名单配置,网关不校验此处的白名单
|
||||||
|
*/
|
||||||
|
private List<String> whites = new ArrayList<>();
|
||||||
|
public void setWhites(List<String> whites) {
|
||||||
|
log.info("加载网关路径白名单:{}", JSONObject.toJSONString(whites));
|
||||||
|
this.whites = whites;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,87 @@
|
||||||
|
package com.spider.gateway.filter;
|
||||||
|
|
||||||
|
import com.spider.common.constant.JwtConstants;
|
||||||
|
import com.spider.common.constant.TokenConstants;
|
||||||
|
import com.spider.common.utils.JwtUtils;
|
||||||
|
import com.spider.common.utils.StringUtils;
|
||||||
|
import com.spider.gateway.config.IgnoreWhiteConfig;
|
||||||
|
import com.spider.gateway.utils.GatewayUtils;
|
||||||
|
import io.jsonwebtoken.Claims;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||||
|
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 📝 过滤器
|
||||||
|
* 👦 Lxj
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Log4j2
|
||||||
|
public class AuthFilter implements GlobalFilter, Ordered {
|
||||||
|
/**
|
||||||
|
* redis操作
|
||||||
|
*/
|
||||||
|
private final StringRedisTemplate redisTemplate;
|
||||||
|
/**
|
||||||
|
* 白名单
|
||||||
|
*/
|
||||||
|
private final IgnoreWhiteConfig ignoreWhite;
|
||||||
|
public AuthFilter(StringRedisTemplate redisTemplate, IgnoreWhiteConfig ignoreWhite) {
|
||||||
|
this.redisTemplate = redisTemplate;
|
||||||
|
this.ignoreWhite = ignoreWhite;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||||
|
// 请求作用域
|
||||||
|
ServerHttpRequest request = exchange.getRequest();
|
||||||
|
// 请求头
|
||||||
|
HttpHeaders headers = request.getHeaders();
|
||||||
|
// 请求方式
|
||||||
|
HttpMethod method = request.getMethod();
|
||||||
|
// header操作对象
|
||||||
|
ServerHttpRequest.Builder mutate = request.mutate();
|
||||||
|
String uri = request.getURI().getPath();
|
||||||
|
log.info("请求日志:uri:[{}] , 请求方式:[{}]", uri, method);
|
||||||
|
// 跳过不需要验证的路径
|
||||||
|
if (StringUtils.matches(uri, ignoreWhite.getWhites())) {
|
||||||
|
return chain.filter(exchange);
|
||||||
|
}
|
||||||
|
//获取token判空
|
||||||
|
String token = headers.getFirst(TokenConstants.TOKEN);
|
||||||
|
if (StringUtils.isEmpty(token)) {
|
||||||
|
return GatewayUtils.errorResponse(exchange, "令牌不能为空");
|
||||||
|
}
|
||||||
|
//获取载荷判空
|
||||||
|
Claims claims = JwtUtils.parseToken(token);
|
||||||
|
if (claims == null) {
|
||||||
|
return GatewayUtils.errorResponse(exchange, "令牌已过期或验证不正确!");
|
||||||
|
}
|
||||||
|
//获取用户标识
|
||||||
|
String userKey = JwtUtils.getUserKey(claims);
|
||||||
|
boolean isLogin = redisTemplate.hasKey(TokenConstants.LOGIN_TOKEN_KEY + userKey);
|
||||||
|
if (!isLogin) {
|
||||||
|
return GatewayUtils.errorResponse(exchange, "登录状态已过期");
|
||||||
|
}
|
||||||
|
String userid = JwtUtils.getUserId(claims);
|
||||||
|
String username = JwtUtils.getUserName(claims);
|
||||||
|
// 设置用户信息到请求
|
||||||
|
GatewayUtils.addHeader(mutate, JwtConstants.USER_KEY, userKey);
|
||||||
|
GatewayUtils.addHeader(mutate, JwtConstants.DETAILS_USER_ID, userid);
|
||||||
|
GatewayUtils.addHeader(mutate, JwtConstants.DETAILS_USERNAME, username);
|
||||||
|
// 内部请求来源参数清除
|
||||||
|
GatewayUtils.removeHeader(mutate, TokenConstants.TOKEN);
|
||||||
|
return chain.filter(exchange.mutate().request(mutate.build()).build());
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,73 @@
|
||||||
|
package com.spider.gateway.utils;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.spider.common.result.Result;
|
||||||
|
import com.spider.common.utils.StringUtils;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
import org.springframework.core.io.buffer.DataBuffer;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author DongZl
|
||||||
|
* @description: 网关处理工具类
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
public class GatewayUtils {
|
||||||
|
/**
|
||||||
|
* 添加请求头参数
|
||||||
|
* @param mutate 修改对象
|
||||||
|
* @param key 键
|
||||||
|
* @param value 值
|
||||||
|
*/
|
||||||
|
public static void addHeader(ServerHttpRequest.Builder mutate, String key, Object value) {
|
||||||
|
if (StringUtils.isEmpty(key)){
|
||||||
|
log.warn("添加请求头参数键不可以为空");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (value == null) {
|
||||||
|
log.warn("添加请求头参数:[{}]值为空",key);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String valueStr = value.toString();
|
||||||
|
mutate.header(key, valueStr);
|
||||||
|
log.info("添加请求头参数成功 - 键:[{}] , 值:[{}]", key , value);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 删除请求头参数
|
||||||
|
* @param mutate 修改对象
|
||||||
|
* @param key 键
|
||||||
|
*/
|
||||||
|
public static void removeHeader(ServerHttpRequest.Builder mutate, String key) {
|
||||||
|
if (StringUtils.isEmpty(key)){
|
||||||
|
log.warn("删除请求头参数键不可以为空");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
mutate.headers(httpHeaders -> httpHeaders.remove(key)).build();
|
||||||
|
log.info("删除请求头参数 - 键:[{}]",key);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 错误结果响应
|
||||||
|
* @param exchange 响应上下文
|
||||||
|
* @param msg 响应消息
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static Mono<Void> errorResponse(ServerWebExchange exchange, String msg) {
|
||||||
|
ServerHttpResponse response = exchange.getResponse();
|
||||||
|
//设置HTTP响应头状态
|
||||||
|
response.setStatusCode(HttpStatus.OK);
|
||||||
|
//设置HTTP响应头文本格式
|
||||||
|
response.getHeaders().add(HttpHeaders.CONTENT_TYPE, "application/json");
|
||||||
|
//定义响应内容
|
||||||
|
Result<?> result = Result.error(msg);
|
||||||
|
String resultJson = JSONObject.toJSONString(result);
|
||||||
|
log.error("[鉴权异常处理]请求路径:[{}],异常信息:[{}],响应结果:[{}]", exchange.getRequest().getPath(), msg, resultJson);
|
||||||
|
DataBuffer dataBuffer = response.bufferFactory().wrap(resultJson.getBytes());
|
||||||
|
//进行响应
|
||||||
|
return response.writeWith(Mono.just(dataBuffer));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
# Tomcat
|
||||||
|
server:
|
||||||
|
port: 80
|
||||||
|
# Spring
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
# 应用名称
|
||||||
|
name: spider-gateway
|
||||||
|
profiles:
|
||||||
|
# 环境配置
|
||||||
|
active: dev
|
||||||
|
main:
|
||||||
|
# 允许使用循环引用
|
||||||
|
allow-circular-references: true
|
||||||
|
# 允许定义相同的bean对象 去覆盖原有的
|
||||||
|
allow-bean-definition-overriding: true
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
# 服务注册地址
|
||||||
|
server-addr: 1.117.62.128:8848
|
||||||
|
config:
|
||||||
|
# 配置中心地址
|
||||||
|
server-addr: 1.117.62.128:8848
|
||||||
|
# 配置文件格式
|
||||||
|
file-extension: yml
|
||||||
|
# 共享配置
|
||||||
|
shared-configs:
|
||||||
|
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
|
@ -0,0 +1,25 @@
|
||||||
|
<?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">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.spider</groupId>
|
||||||
|
<artifactId>spider-car-cloud</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>spider-modules</artifactId>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
|
<modules>
|
||||||
|
<module>spider-system</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,33 @@
|
||||||
|
<?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">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.spider</groupId>
|
||||||
|
<artifactId>spider-modules</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>spider-modules-system</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.spider</groupId>
|
||||||
|
<artifactId>spider-common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- SpringBoot Web-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,31 @@
|
||||||
|
package com.spider.system;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 📝 TODO
|
||||||
|
* 🕟 2024/1/11 11:29
|
||||||
|
* 👦 Lxj
|
||||||
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableFeignClients
|
||||||
|
public class SpiderSystemApplication {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SpiderSystemApplication.class,args);
|
||||||
|
System.out.println("蜘蛛系统启动成功\n" +
|
||||||
|
"# __ __ \n" +
|
||||||
|
"# | \\ | \\ \n" +
|
||||||
|
"# _______ ______ \\$$ ____| $$ ______ ______ \n" +
|
||||||
|
"# / \\ / \\ | \\ / $$ / \\ / \\ \n" +
|
||||||
|
"# | $$$$$$$| $$$$$$\\| $$| $$$$$$$| $$$$$$\\| $$$$$$\\\n" +
|
||||||
|
"# \\$$ \\ | $$ | $$| $$| $$ | $$| $$ $$| $$ \\$$\n" +
|
||||||
|
"# _\\$$$$$$\\| $$__/ $$| $$| $$__| $$| $$$$$$$$| $$ \n" +
|
||||||
|
"# | $$| $$ $$| $$ \\$$ $$ \\$$ \\| $$ \n" +
|
||||||
|
"# \\$$$$$$$ | $$$$$$$ \\$$ \\$$$$$$$ \\$$$$$$$ \\$$ \n" +
|
||||||
|
"# | $$ \n" +
|
||||||
|
"# | $$ \n" +
|
||||||
|
"# \\$$ \n");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.spider.system.controller;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 📝 TODO
|
||||||
|
* 🕟 2024/1/19 10:50
|
||||||
|
* 👦 Lxj
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/car")
|
||||||
|
public class CarController {
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
package com.spider.system.controller;
|
||||||
|
|
||||||
|
import com.spider.common.domain.User;
|
||||||
|
import com.spider.common.result.Result;
|
||||||
|
import com.spider.system.service.UserService;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 📝 TODO
|
||||||
|
* 🕟 2024/1/19 9:45
|
||||||
|
* 👦 Lxj
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/user")
|
||||||
|
public class UserController {
|
||||||
|
|
||||||
|
private final UserService userService;
|
||||||
|
|
||||||
|
public UserController(UserService userService) {
|
||||||
|
this.userService = userService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询用户信息
|
||||||
|
* @param userAccount
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@PostMapping("/info/{userAccount}")
|
||||||
|
public Result<User> info(@PathVariable("userAccount") String userAccount){
|
||||||
|
return userService.getUserByAccount(userAccount);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
package com.spider.system.mapper;
|
||||||
|
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 📝 TODO
|
||||||
|
* 🕟 2024/1/19 10:51
|
||||||
|
* 👦 Lxj
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface CarMapper {
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.spider.system.mapper;
|
||||||
|
|
||||||
|
import com.spider.common.domain.User;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 📝 TODO
|
||||||
|
* 🕟 2024/1/19 9:45
|
||||||
|
* 👦 Lxj
|
||||||
|
*/
|
||||||
|
@Mapper
|
||||||
|
public interface UserMapper{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据用户名查找用户信息
|
||||||
|
* @param userAccount
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
User selectUserByAccount(@Param("userAccount") String userAccount);
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.spider.system.service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 📝 TODO
|
||||||
|
* 🕟 2024/1/19 10:50
|
||||||
|
* 👦 Lxj
|
||||||
|
*/
|
||||||
|
public interface CarService {
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
package com.spider.system.service;
|
||||||
|
|
||||||
|
import com.spider.common.domain.User;
|
||||||
|
import com.spider.common.result.Result;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 📝 TODO
|
||||||
|
* 🕟 2024/1/19 9:45
|
||||||
|
* 👦 Lxj
|
||||||
|
*/
|
||||||
|
public interface UserService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户信息
|
||||||
|
* @param userAccount
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Result<User> getUserByAccount(String userAccount);
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.spider.system.service.impl;
|
||||||
|
|
||||||
|
import com.spider.system.service.CarService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 📝 TODO
|
||||||
|
* 🕟 2024/1/19 10:50
|
||||||
|
* 👦 Lxj
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class CarServiceImpl implements CarService {
|
||||||
|
}
|
|
@ -0,0 +1,42 @@
|
||||||
|
package com.spider.system.service.impl;
|
||||||
|
|
||||||
|
import com.spider.common.domain.User;
|
||||||
|
import com.spider.common.result.Result;
|
||||||
|
import com.spider.common.utils.StringUtils;
|
||||||
|
import com.spider.system.mapper.UserMapper;
|
||||||
|
import com.spider.system.service.UserService;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 📝 TODO
|
||||||
|
* 🕟 2024/1/19 9:45
|
||||||
|
* 👦 Lxj
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class UserServiceImpl implements UserService{
|
||||||
|
|
||||||
|
private final UserMapper userMapper;
|
||||||
|
|
||||||
|
|
||||||
|
public UserServiceImpl(UserMapper userMapper) {
|
||||||
|
this.userMapper = userMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户信息
|
||||||
|
* @param userAccount
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Result<User> getUserByAccount(String userAccount) {
|
||||||
|
|
||||||
|
User user = userMapper.selectUserByAccount(userAccount);
|
||||||
|
|
||||||
|
if(StringUtils.isNull(user)){
|
||||||
|
return Result.error("获取失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.success(user);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
# Tomcat
|
||||||
|
server:
|
||||||
|
port: 9201
|
||||||
|
# Spring
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
# 应用名称
|
||||||
|
name: spider-system
|
||||||
|
profiles:
|
||||||
|
# 环境配置
|
||||||
|
active: dev
|
||||||
|
main:
|
||||||
|
# 允许使用循环引用
|
||||||
|
allow-circular-references: true
|
||||||
|
# 允许定义相同的bean对象 去覆盖原有的
|
||||||
|
allow-bean-definition-overriding: true
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
discovery:
|
||||||
|
# 服务注册地址
|
||||||
|
server-addr: 1.117.62.128:8848
|
||||||
|
config:
|
||||||
|
# 配置中心地址
|
||||||
|
server-addr: 1.117.62.128:8848
|
||||||
|
# 配置文件格式
|
||||||
|
file-extension: yml
|
||||||
|
# 共享配置
|
||||||
|
shared-configs:
|
||||||
|
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
|
@ -0,0 +1,15 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<!DOCTYPE mapper
|
||||||
|
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<!--
|
||||||
|
1.在mybats的开发中namespace有特殊的意思,一定要是对应接口的全限定名
|
||||||
|
通过namespace可以简历mapper.xml和接口之间的关系(名字不重要,位置不重要)
|
||||||
|
-->
|
||||||
|
<mapper namespace="com.spider.system.mapper.UserMapper">
|
||||||
|
|
||||||
|
|
||||||
|
<select id="selectUserByAccount" resultType="com.spider.common.domain.User">
|
||||||
|
select * from t_user where user_account = #{userAccount}
|
||||||
|
</select>
|
||||||
|
</mapper>
|
Loading…
Reference in New Issue