登录集成shiro

main
刘泽璋 2024-06-26 20:43:13 +08:00
parent cd64663e08
commit 0bdc8c88f9
135 changed files with 2913 additions and 265 deletions

View File

@ -7,11 +7,14 @@
<sourceOutputDir name="target/generated-sources/annotations" /> <sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" /> <sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" /> <outputRelativeToContentRoot value="true" />
<module name="etl-common" />
<module name="el-data-source-server" /> <module name="el-data-source-server" />
<module name="etl-jwt-manage" />
<module name="el-data-source-common" />
<module name="etl-auth-server" />
<module name="etl-common" />
<module name="etl-gateway" /> <module name="etl-gateway" />
<module name="etl-heihei" /> <module name="etl-heihei" />
<module name="el-data-source-common" /> <module name="etl-groovy" />
</profile> </profile>
</annotationProcessing> </annotationProcessing>
<bytecodeTargetLevel> <bytecodeTargetLevel>

View File

@ -1,6 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="Encoding"> <component name="Encoding">
<file url="file://$PROJECT_DIR$/etl-auth/etl-auth-common/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/etl-auth/etl-auth-common/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/etl-auth/etl-auth-server/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/etl-auth/etl-auth-server/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/etl-auth/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/etl-auth/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/etl-common/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/etl-common/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/etl-common/src/main/resources" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/etl-common/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/etl-data-source/el-data-source-common/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/etl-data-source/el-data-source-common/src/main/java" charset="UTF-8" />
@ -11,8 +17,12 @@
<file url="file://$PROJECT_DIR$/etl-data-source/src/main/resources" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/etl-data-source/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/etl-gateway/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/etl-gateway/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/etl-gateway/src/main/resources" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/etl-gateway/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/etl-groovy/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/etl-groovy/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/etl-heihei/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/etl-heihei/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/etl-heihei/src/main/resources" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/etl-heihei/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/etl-jwt-manage/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/etl-jwt-manage/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/etl-mysql/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/etl-mysql/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/etl-mysql/src/main/resources" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/etl-mysql/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />

View File

@ -9,6 +9,10 @@
<option value="$PROJECT_DIR$/etl-data-source/pom.xml" /> <option value="$PROJECT_DIR$/etl-data-source/pom.xml" />
<option value="$PROJECT_DIR$/etl-data-source/el-data-source-server/pom.xml" /> <option value="$PROJECT_DIR$/etl-data-source/el-data-source-server/pom.xml" />
<option value="$PROJECT_DIR$/etl-data-source/el-data-source-common/pom.xml" /> <option value="$PROJECT_DIR$/etl-data-source/el-data-source-common/pom.xml" />
<option value="$PROJECT_DIR$/etl-groovy/pom.xml" />
<option value="$PROJECT_DIR$/etl-auth/pom.xml" />
<option value="$PROJECT_DIR$/etl-jwt-manage/pom.xml" />
<option value="$PROJECT_DIR$/etl-auth/etl-auth-common/pom.xml" />
</list> </list>
</option> </option>
<option name="ignoredFiles"> <option name="ignoredFiles">

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,55 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bwie</groupId>
<artifactId>etl-auth</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<packaging>pom</packaging>
<artifactId>etl-auth-common</artifactId>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.13</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>com.bwie</groupId>
<artifactId>etl-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.6.6</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,33 @@
package com.auth.common.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("t_data_source")
public class PathPermission {
@TableId(value = "id",type = IdType.AUTO)
private Integer id;
@ApiModelProperty(value = "权限代号")
private String permissionCode;
@ApiModelProperty(value = "路由层次")
private String hierarchy;
@ApiModelProperty(value = "路由")
private String path;
@ApiModelProperty(value = "功能描述")
private String description;
@ApiModelProperty(value = "状态 0-废弃 1-正在使用")
private boolean status;
}

View File

@ -0,0 +1,24 @@
package com.auth.common.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@TableName("t_role_source")
@AllArgsConstructor
@NoArgsConstructor
public class RolesPermission {
@TableId(value = "id",type = IdType.AUTO)
private Integer id;
@ApiModelProperty(value = "权限代号")
private String permissionCode;
@ApiModelProperty(value = "角色")
private String role;
}

View File

@ -0,0 +1,26 @@
package com.auth.common.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@TableName("t_user")
public class UserAccount {
@TableId(value = "id",type = IdType.AUTO)
private Integer id;
@ApiModelProperty("角色")
private String roles;
@ApiModelProperty("名称")
private String username;
@ApiModelProperty("密码/")
private String password;
// @ApiModelProperty("uuid")
// private String UUID;
}

View File

@ -0,0 +1,13 @@
package com.auth.common.enums;
public class PermissionConstants {
public static final String ROLES = "roles";
public static final String CODE_LIST = "codeList";
public static final String ROLE = "role";
public static final String PERMISSION_CODE = "permission_code";
public static final String USER_CACHE_KEY = "user:username:";
public static final String USER_KEY = "user:key:";
public static final String USER_NAME = "username";
public static final String USER_ID = "userId";
}

View File

@ -0,0 +1,154 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bwie</groupId>
<artifactId>etl-auth</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>etl-auth-server</artifactId>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.13</spring-boot.version>
<spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>com.bwie</groupId>
<artifactId>etl-auth-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--shiro-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.bwie</groupId>
<artifactId>etl-jwt-manage</artifactId>
<version>1.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.bwie</groupId>
<artifactId>etl-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.4.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.1.7</version>
</dependency>
<!--shiro-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.auth.server.EtlAuthServerApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,25 @@
package com.auth.server;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@SpringBootApplication
@MapperScan("com.auth.server.mapper")
@ComponentScan(basePackages = {"com.etl.jwt.util", "com.etl.jwt.config",
"com.auth.server.controller","com.auth.server.service","com.auth.server.service.impl","com.auth.server.config"})
public class EtlAuthServerApplication {
public static void main(String[] args) {
SpringApplication.run(EtlAuthServerApplication.class, args);
}
@Bean
public BCryptPasswordEncoder encryptPasswordEncoder(){
return new BCryptPasswordEncoder();
}
}

View File

@ -0,0 +1,44 @@
package com.auth.server.config;
import org.apache.http.util.TextUtils;
import org.apache.shiro.crypto.hash.HashRequest;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/*
* MD5
*/
public class MD5s {
public static String md5(String string) {
if (TextUtils.isEmpty(string)) {
return "";
}
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
byte[] bytes = md5.digest(string.getBytes());
String result = "";
for (byte b : bytes) {
String temp = Integer.toHexString(b & 0xff);
if (temp.length() == 1) {
temp = "0" + temp;
}
result += temp;
}
return result;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
HashRequest request = new HashRequest.Builder()
.setAlgorithmName("MD5") // 使用 SHA-256 算法
.setSource(string.getBytes()) // 要散列的密码(作为字节数组)
.build();
//passwordService.encryptPassword(request);
return "";
}
}

View File

@ -0,0 +1,18 @@
//package com.auth.server.config;
//import com.auth.server.interceptor.UserInterceptor;
//import com.auth.server.mapper.UserMangeMapper;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//@Configuration
//public class MvcConfig implements WebMvcConfigurer {
// @Autowired
// private UserMangeMapper userMangeMapper;
// @Override
// public void addInterceptors(InterceptorRegistry registry) {
// //添加拦截器,排除/路径和 /login路径
// registry.addInterceptor(new UserInterceptor(userMangeMapper))
// .excludePathPatterns("/","/user/login");
// }
//}

View File

@ -0,0 +1,69 @@
package com.auth.server.config;
import com.auth.server.util.UserRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.LinkedHashMap;
import java.util.Map;
@Configuration
public class ShiroConfig {
//此处用于实现授权功能,配置需要拦截的接口
//此处用于实现授权功能,配置需要拦截的接口
@Bean
public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("getDefaultWebSecurityManager") DefaultWebSecurityManager defaultWebSecurityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
//拦截页面
Map<String, String> filterMap = new LinkedHashMap<>();
//登录/登出,所有人的权限
filterMap.put("/user/login", "anon");
filterMap.put("/user/logout", "anon");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
//未登录页面跳转
shiroFilterFactoryBean.setLoginUrl("/user/show");
//未有权限页面跳转
shiroFilterFactoryBean.setUnauthorizedUrl("/user/unauthorized");
return shiroFilterFactoryBean;
}
//注入对应的userRealm类
@Bean
public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm) {
DefaultWebSecurityManager SecurityManager = new DefaultWebSecurityManager();
SecurityManager.setRealm(userRealm);
return SecurityManager;
}
@Bean
public UserRealm userRealm() {
UserRealm userRealm = new UserRealm();
//注册MD5加密
userRealm.setCredentialsMatcher(hashedCredentialsMatcher());
return userRealm;
}
/**
* shiro
* @return HashedCredentialsMatcher
*/
@Bean
public HashedCredentialsMatcher hashedCredentialsMatcher(){
HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
// 使用md5 算法进行加密
hashedCredentialsMatcher.setHashAlgorithmName("MD5");
// 设置散列次数: 意为加密几次
hashedCredentialsMatcher.setHashIterations(2);
return hashedCredentialsMatcher;
}
}

View File

@ -0,0 +1,84 @@
package com.auth.server.controller;
import com.alibaba.fastjson2.JSON;
import com.alibaba.nacos.api.model.v2.Result;
import com.auth.common.entity.UserAccount;
import com.auth.common.enums.PermissionConstants;
import com.auth.server.service.UserManageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.etl.common.enums.ResponseCodeEnum;
import com.etl.common.result.CommonResult;
import com.etl.jwt.util.JwtTokenUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
* controller
*/
@RestController
@Slf4j
@Api(tags = "登录-API")
@RequestMapping("/user")
public class LoginControler {
@Autowired
private UserManageService userManageService;
/**
* (使)
*
* @param user
* @return
*/
@ApiOperation(value = "用户登录")
@ApiOperationSupport(author = "liz")
@RequestMapping(value = "/login", method = RequestMethod.POST)
public Result<Object> userLogin(@RequestBody UserAccount user) {
return userManageService.userLogin(user);
}
/**
* JWT,
*
* header
*/
/**
* JWT access_token access_token使
* JWT 使 JWT access_token access_token
* 使refresh tokentoken
* <p>
* refresh_token access_token access_token refresh_token
* redis
*
* @param token
* @return
*/
@RequestMapping(value = "/token/refresh", method = RequestMethod.GET)
@ApiOperation(value = "刷新令牌")
@ApiOperationSupport(author = "liz")
public CommonResult<Object> refreshToken(@RequestHeader(value = "${auth.jwt.header}") String token) {
return userManageService.refreshToken(token);
}
}

View File

@ -0,0 +1,52 @@
package com.auth.server.controller;
import com.auth.common.entity.PathPermission;
import com.auth.common.entity.RolesPermission;
import com.auth.common.enums.PermissionConstants;
import com.auth.server.service.PathService;
import com.auth.server.service.PermissionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* controller
*/
@RestController
@Api(tags ="权限-API")
@RequestMapping("/permission")
public class PermissionController {
@Autowired
private PermissionService permissionService;
@Resource
private PathService pathService;
@PostMapping("/permission/add")
public boolean permissionAdd(@RequestBody Map<String,Object> map) {
String roles = (String) map.get(PermissionConstants.ROLES);
List<String> codeList = (List<String>) map.get(PermissionConstants.CODE_LIST);
return permissionService.permissionAdd(roles, codeList);
}
@GetMapping("/get")
public List<String> pathGet(@RequestParam("roles") String roles) {
RolesPermission permission = permissionService.getOne(new QueryWrapper<RolesPermission>().eq(PermissionConstants.ROLE,roles));
String codes = StringUtils.strip(permission.getPermissionCode(), "[]");
List<String> list = Arrays.asList(codes.split(","));
List<String> pathList = new ArrayList<>();
for(String code:list){
String api = pathService.getOne(new QueryWrapper<PathPermission>().eq(PermissionConstants.PERMISSION_CODE,code.trim())).getPath();
pathList.add(api);
}
return pathList;
}
}

View File

@ -0,0 +1,31 @@
//package com.auth.server.interceptor;
//import com.auth.server.mapper.UserMangeMapper;
//import org.springframework.web.servlet.HandlerInterceptor;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
//public class UserInterceptor implements HandlerInterceptor {
// private UserMangeMapper userMangeMapper;
// public UserInterceptor(UserMangeMapper userMapper){
// this.userMangeMapper=userMapper;
// }
// @Override
// public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// HttpSession session = request.getSession();
// String username = (String) session.getAttribute("username");
// //数据库
// String UUID=userMangeMapper.getUUID(username);
// //session
// String uuid = (String)session.getAttribute("uuid");
// System.out.println("uuid = " + uuid);
// System.out.println("UUID = " + UUID);
// if(UUID.equals(uuid)){
// return true;
// }else {
// System.out.println("拦截"+request.getRequestURI());
// response.sendRedirect("/login");
// response.setStatus(401);
// return false;
// }
// }
//}

View File

@ -0,0 +1,8 @@
package com.auth.server.mapper;
import com.auth.common.entity.PathPermission;
import com.auth.common.entity.RolesPermission;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface PathMapper extends BaseMapper<PathPermission> {
}

View File

@ -0,0 +1,8 @@
package com.auth.server.mapper;
import com.auth.common.entity.RolesPermission;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface PermissionMapper extends BaseMapper<RolesPermission> {
}

View File

@ -0,0 +1,12 @@
package com.auth.server.mapper;
import com.auth.common.entity.UserAccount;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMangeMapper extends BaseMapper<UserAccount> {
}

View File

@ -0,0 +1,9 @@
package com.auth.server.service;
import com.auth.common.entity.PathPermission;
import com.auth.common.entity.RolesPermission;
import com.auth.common.entity.UserAccount;
import com.baomidou.mybatisplus.extension.service.IService;
public interface PathService extends IService<PathPermission> {
}

View File

@ -0,0 +1,11 @@
package com.auth.server.service;
import com.auth.common.entity.RolesPermission;
import com.auth.common.entity.UserAccount;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
public interface PermissionService extends IService<RolesPermission> {
boolean permissionAdd(String roles, List<String> codeList);
}

View File

@ -0,0 +1,15 @@
package com.auth.server.service;
import com.alibaba.nacos.api.model.v2.Result;
import com.auth.common.entity.UserAccount;
import com.baomidou.mybatisplus.extension.service.IService;
import com.etl.common.result.CommonResult;
public interface UserManageService extends IService<UserAccount> {
Result<Object> userLogin(UserAccount user);
CommonResult<Object> refreshToken(String token);
}

View File

@ -0,0 +1,16 @@
package com.auth.server.service.impl;
import com.auth.common.entity.PathPermission;
import com.auth.common.entity.RolesPermission;
import com.auth.server.mapper.PathMapper;
import com.auth.server.mapper.PermissionMapper;
import com.auth.server.service.PathService;
import com.auth.server.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class PathServiceImpl extends ServiceImpl<PathMapper, PathPermission> implements PathService {
}

View File

@ -0,0 +1,61 @@
package com.auth.server.service.impl;
import com.auth.common.entity.RolesPermission;
import com.auth.common.enums.PermissionConstants;
import com.auth.server.mapper.PermissionMapper;
import com.auth.server.service.PermissionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
@Service
@Slf4j
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, RolesPermission> implements PermissionService {
@Autowired
private PermissionMapper permissionMapper;
@Override
public boolean permissionAdd(String roles, List<String> codeList) {
RolesPermission rolesPermission = new RolesPermission();
rolesPermission.setRole(roles);
int result = 0;
RolesPermission role = getRoleByName(roles);
if (role != null) {
result = updateRolePermission(rolesPermission, role, codeList);
} else {
result = insertRolePermission(rolesPermission, codeList);
}
return result > 0;
}
private RolesPermission getRoleByName(String roleName) {
return permissionMapper.selectOne(new QueryWrapper<RolesPermission>().eq(PermissionConstants.ROLE, roleName));
}
private int updateRolePermission(RolesPermission rolesPermission, RolesPermission role, List<String> codeList) {
//去掉头尾括号,并转为列表
List<String> list = new java.util.ArrayList<>(Collections.singletonList(
StringUtils.strip(role.getPermissionCode(), "[]")));
//将新数据添加至列表
list.addAll(codeList);
rolesPermission.setPermissionCode(list.toString());
rolesPermission.setId(role.getId());
return permissionMapper.updateById(rolesPermission);
}
private int insertRolePermission(RolesPermission rolesPermission, List<String> codeList) {
if (codeList != null) {
rolesPermission.setPermissionCode(codeList.toString());
}
return permissionMapper.insert(rolesPermission);
}
}

View File

@ -0,0 +1,168 @@
package com.auth.server.service.impl;
import com.alibaba.fastjson2.JSON;
import com.alibaba.nacos.api.model.v2.Result;
import com.auth.common.entity.UserAccount;
import com.auth.common.enums.PermissionConstants;
import com.auth.server.config.MD5s;
import com.auth.server.mapper.UserMangeMapper;
import com.auth.server.service.UserManageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etl.common.enums.ResponseCodeEnum;
import com.etl.common.result.CommonResult;
import com.etl.jwt.util.JwtTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static java.lang.System.nanoTime;
@Service
@Slf4j
public class UserManageServiceImpl extends ServiceImpl<UserMangeMapper, UserAccount> implements UserManageService {
@Resource
private StringRedisTemplate stringRedisTemplate;
@Resource
private JwtTokenUtil jwtTokenUtil;
@Autowired
private UserMangeMapper UserMangeMapper;
@Override
public Result<Object> userLogin(UserAccount user) {
long startTime = nanoTime();
// 获取用户名
String username = user.getUsername();
// 获取密码
String password = user.getPassword();
// 参数校验
Result result = checkUser(username, password);
if (result != null) {
return result;
}
// 创建SimpleAccountRealm并添加账户信息
SimpleAccountRealm simpleAccountRealm = new SimpleAccountRealm();
// 确保这里添加的用户名和密码与用户提供的匹配
simpleAccountRealm.addAccount(username, password);
// 配置SecurityManager并设置Realm
DefaultSecurityManager securityManager = new DefaultSecurityManager(simpleAccountRealm);
securityManager.setRealm(simpleAccountRealm);
SecurityUtils.setSecurityManager(securityManager);
//shiro验证
Subject subject = SecurityUtils.getSubject();
//根据用户名密码生成一个令牌
AuthenticationToken token = new UsernamePasswordToken(username, password);
try {
//执行登录操作
subject.login(token);
//将用户信息存入redis
saveUserInfoToRedis(username, user);
} catch (UnknownAccountException e) {
log.info("登录用户不存在:{}", e);
return new Result<>(416, "用户不存在", username);
} catch (IncorrectCredentialsException e) {
log.info("登录密码错误:{}", e);
return new Result<>(412, "密码错误,请重新登录", password);
} catch (AuthenticationException e) {
log.warn("用户登录异常:" + e.getMessage());
return new Result<>(416, "账户异常", username);
}
String userInfo = stringRedisTemplate.opsForValue().get(PermissionConstants.USER_NAME + username);
UserAccount account = null;
if (userInfo != null) {
account = JSON.parseObject(userInfo, UserAccount.class);
} else {
//获取登录用户信息
account = UserMangeMapper.selectOne(new QueryWrapper<UserAccount>().eq(PermissionConstants.USER_NAME, username));
stringRedisTemplate.opsForValue().set(PermissionConstants.USER_NAME + username, JSON.toJSONString(account));
}
// 通过 jwtTokenUtil 生成 JWT 令牌和刷新令牌
Map<String, Object> tokenMap = jwtTokenUtil
.generateTokenAndRefreshToken(String.valueOf(account.getId()), username);
// 用户角色映射表中中查询用户角色
account.getRoles();
long endTime = nanoTime();
// 计算耗时(单位为纳秒)
long elapsedTime = endTime - startTime;
// 将耗时转换为毫秒
double elapsedTimeInMillis = elapsedTime / 1000000.0;
// 输出结果
System.out.println("程序运行耗时:" + elapsedTimeInMillis + " 毫秒");
return Result.success(tokenMap);
}
private void saveUserInfoToRedis(String username, UserAccount user) {
stringRedisTemplate.opsForValue().set(PermissionConstants.USER_CACHE_KEY + username,
JSON.toJSONString(user), 30 * 60, TimeUnit.MINUTES);
}
private Result checkUser(String username, String password) {
// 检查用户名和密码的合法性
if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
return new Result<>(400, "用户名或密码不能为空");
}
if (username.length() < 3 || username.length() > 20) {
return new Result<>(400, "用户名长度必须在6-20个字符之间");
}
if (password.length() < 3 || password.length() > 20) {
return new Result<>(400, "密码长度必须在6-20个字符之间");
}
return null;
}
@Override
public CommonResult<Object> refreshToken(String token) {
token = com.auth.server.util.SecurityUtils.replaceTokenPrefix(token);
if (StringUtils.isEmpty(token)) {
return new CommonResult<>(ResponseCodeEnum.TOKEN_MISSION.getCode(),
ResponseCodeEnum.TOKEN_MISSION.getMessage());
}
// 对Token解签名并验证Token是否过期
boolean isJwtNotValid = jwtTokenUtil.isTokenExpired(token);
if (isJwtNotValid) {
return new CommonResult<>(ResponseCodeEnum.TOKEN_INVALID.getCode(),
ResponseCodeEnum.TOKEN_INVALID.getMessage());
}
// 验证 token 里面的 userId 是否为空
String userId = jwtTokenUtil.getUserIdFromToken(token);
String username = jwtTokenUtil.getUserNameFromToken(token);
if (StringUtils.isEmpty(userId)) {
return new CommonResult<>(ResponseCodeEnum.TOKEN_INVALID.getCode(),
ResponseCodeEnum.TOKEN_INVALID.getMessage());
}
// 这里为了保证 refreshToken 只能用一次,刷新后,会从 redis 中删除。
// 如果用的不是 redis 中的 refreshToken 进行刷新令牌,则不能刷新。
// 如果使用 redis 中已过期的 refreshToken 也不能刷新令牌。
boolean isRefreshTokenNotExisted = jwtTokenUtil.isRefreshTokenNotExistCache(token);
if (isRefreshTokenNotExisted) {
return new CommonResult<>(ResponseCodeEnum.REFRESH_TOKEN_INVALID.getCode(),
ResponseCodeEnum.REFRESH_TOKEN_INVALID.getMessage());
}
//String us = jwtTokenUtil.getUserIdFromToken(token);
Map<String, Object> tokenMap = jwtTokenUtil.refreshTokenAndGenerateToken(userId, username);
return new CommonResult<>(200, ResponseCodeEnum.SUCCESS.getMessage(), tokenMap);
}
}

View File

@ -0,0 +1,37 @@
package com.auth.server.util;
import com.etl.common.constants.TokenConstants;
import javax.servlet.http.HttpServletRequest;
import static org.apache.logging.log4j.util.Strings.isEmpty;
/**
*
*
*/
public class SecurityUtils
{
/**
* requesttoken
*/
public static String getToken(HttpServletRequest request)
{
// 从header获取token标识
String token = request.getHeader(TokenConstants.AUTHENTICATION);
return replaceTokenPrefix(token);
}
/**
* token
*/
public static String replaceTokenPrefix(String token) {
// 如果前端设置了令牌前缀,则裁剪掉前缀
if (!isEmpty(token) && token.startsWith(TokenConstants.PREFIX)) {
token = token.replaceFirst(TokenConstants.PREFIX, "");
}
return token;
}
}

View File

@ -0,0 +1,65 @@
package com.auth.server.util;
import com.auth.common.entity.UserAccount;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.auth.server.service.UserManageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
@Slf4j
public class UserRealm extends AuthorizingRealm {
@Autowired
private UserManageService userManageService;
//授权
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
/**
*Userroles
*
* */
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
Subject subject= SecurityUtils.getSubject();
UserAccount currentUser =(UserAccount) subject.getPrincipal();
authorizationInfo.addStringPermission(currentUser.getRoles());
log.info("用户权限为:"+currentUser.getRoles());
return authorizationInfo;
}
//认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
//用户名/密码认证
//从接口处获取得到的用户名
String username = token.getUsername();
//调用mybatis_plus中的方法查询数据库中用户名对应的数据
QueryWrapper<UserAccount> wrapper = new QueryWrapper<>();
wrapper.eq("username",username);
UserAccount user=userManageService.getOne(wrapper);
//为空,即用户名不存在
if(user==null){
return null;
}else {
log.info(user.getUsername());
}
//principal认证的实体信息可以是username也可以是数据库表对应的用户的实体对象
// Object principal = user.getUsername();
ByteSource salt = ByteSource.Util.bytes(username);
return new SimpleAuthenticationInfo(user.getUsername(), user.getPassword() , salt,getName());
}
}

View File

@ -0,0 +1,51 @@
server:
port: 9092
spring:
application:
name: engine-auth
redis:
host: 115.159.33.152
port: 6379
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://115.159.33.152:3306/etl
username: root
password: lzm@123
mvc:
pathmatch:
matching-strategy: ant_path_matcher
profiles:
active: dev
cloud:
nacos:
#注册服务
discovery:
server-addr: 115.159.33.152:8848
namespace: f9f293d4-55ce-45c1-aa15-124ca461c060
# 配置
config:
server-addr: 115.159.33.152:8848
namespace: f9f293d4-55ce-45c1-aa15-124ca461c060
# 配置文件格式
file-extension: yml
# 共享配置
shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
logging:
level:
com.bwie: DEBUG
auth:
jwt:
enabled: true # 是否开启JWT登录认证功能
secret: passjava # JWT 私钥用于校验JWT令牌的合法性
expiration: 1800000 # JWT 令牌的有效期用于校验JWT令牌的合法性半个小时
header: Authorization # HTTP 请求的 Header 名称,该 Header作为参数传递 JWT 令牌
userParamName: username # 用户登录认证用户名参数名称
pwdParamName: password # 用户登录认证密码参数名称
useDefaultController: true # 是否使用默认的JwtAuthController
skipValidUrl:
- /auth/login
- /auth/logout
mybatis-plus:
mapper-locations: classpath:/mapper/*mapper.xml
typeAliasesPackage: com.auth.common.entity

View File

@ -0,0 +1,6 @@
<?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">
<mapper namespace="com.auth.server.mapper.PathMapper">
</mapper>

View File

@ -0,0 +1,6 @@
<?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">
<mapper namespace="com.auth.server.mapper.PermissionMapper">
</mapper>

View File

@ -0,0 +1,6 @@
<?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">
<mapper namespace="com.auth.server.mapper.UserMangeMapper">
</mapper>

View File

@ -0,0 +1,6 @@
<html>
<body>
<h1>hello word!!!</h1>
<p>this is a html page</p>
</body>
</html>

View File

@ -0,0 +1,31 @@
package com.auth.server;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@SpringBootTest
class EtlAuthServerApplicationTests {
@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
@Test
public void enCoder() {
String password="123";
String encode = bCryptPasswordEncoder.encode(password);
System.out.println("加密后的密码:"+encode);
}
@Test
public void matchesPassword(){
String encode="$2a$10$qyOS46MrKAm2wIyJl95.eO70ioBDvsgv8nnCCjhGJUxPIhPC56PTa";
boolean matches = bCryptPasswordEncoder.matches("123", encode);
if (matches){
System.out.println("密码正确");
}else {
System.out.println("密码错误");
}
}
}

View File

@ -0,0 +1,51 @@
server:
port: 9092
spring:
application:
name: engine-auth
redis:
host: 115.159.33.152
port: 6379
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://115.159.33.152:3306/etl
username: root
password: lzm@123
mvc:
pathmatch:
matching-strategy: ant_path_matcher
profiles:
active: dev
cloud:
nacos:
#注册服务
discovery:
server-addr: 115.159.33.152:8848
namespace: f9f293d4-55ce-45c1-aa15-124ca461c060
# 配置
config:
server-addr: 115.159.33.152:8848
namespace: f9f293d4-55ce-45c1-aa15-124ca461c060
# 配置文件格式
file-extension: yml
# 共享配置
shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
logging:
level:
com.bwie: DEBUG
auth:
jwt:
enabled: true # 是否开启JWT登录认证功能
secret: passjava # JWT 私钥用于校验JWT令牌的合法性
expiration: 1800000 # JWT 令牌的有效期用于校验JWT令牌的合法性半个小时
header: Authorization # HTTP 请求的 Header 名称,该 Header作为参数传递 JWT 令牌
userParamName: username # 用户登录认证用户名参数名称
pwdParamName: password # 用户登录认证密码参数名称
useDefaultController: true # 是否使用默认的JwtAuthController
skipValidUrl:
- /auth/login
- /auth/logout
mybatis-plus:
mapper-locations: classpath:/mapper/*mapper.xml
typeAliasesPackage: com.auth.common.entity

View File

@ -0,0 +1,6 @@
<?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">
<mapper namespace="com.auth.server.mapper.PathMapper">
</mapper>

View File

@ -0,0 +1,6 @@
<?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">
<mapper namespace="com.auth.server.mapper.PermissionMapper">
</mapper>

View File

@ -0,0 +1,6 @@
<?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">
<mapper namespace="com.auth.server.mapper.UserMangeMapper">
</mapper>

View File

@ -0,0 +1,6 @@
<html>
<body>
<h1>hello word!!!</h1>
<p>this is a html page</p>
</body>
</html>

24
etl-auth/pom.xml 100644
View File

@ -0,0 +1,24 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.bwie</groupId>
<artifactId>etl-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>etl-auth</artifactId>
<packaging>pom</packaging>
<modules>
<module>etl-auth-common</module>
<module>etl-auth-server</module>
<module>etl-auth-common</module>
</modules>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.13</spring-boot.version>
<spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
</properties>
</project>

View File

@ -21,4 +21,12 @@ public class TokenConstants {
* token * token
*/ */
public static final String TOKEN = "token"; public static final String TOKEN = "token";
/**
*
*/
public static final String AUTHENTICATION = "Authorization";
/**
*
*/
public static final String PREFIX = "Bearer ";
} }

View File

@ -0,0 +1,29 @@
package com.etl.common.enums;
public enum ResponseCodeEnum {
SUCCESS(200, "成功"),
FAIL(412, "失败"),
LOGIN_ERROR(202, "用户名或密码错误"),
UNKNOWN_ERROR(500, "未知错误"),
PARAMETER_ILLEGAL(400, "参数不合法"),
TOKEN_INVALID(412, "token 已过期或验证不正确!"),
TOKEN_SIGNATURE_INVALID(403, "无效的签名"),
TOKEN_MISSION(403, "token 缺失"),
REFRESH_TOKEN_INVALID(412, "refreshToken 无效"),
LOGOUT_ERROR(444, "用户登出失败");
private final int code;
private final String message;
ResponseCodeEnum(int code, String message) {
this.code = code;
this.message = message;
}
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
}

View File

@ -0,0 +1,22 @@
package com.etl.common.result;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T>{
private Integer code;
private String message;
private T data;
private int dataSize;
public CommonResult(Integer code,String message){
this(code,message,null,0);
}
public CommonResult(Integer code,String message,T data){
this(code,message,data,0);
}
}

View File

@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.models.auth.In;
import lombok.Data; import lombok.Data;
import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotEmpty;
@ -19,9 +20,6 @@ public class DataSource{
@TableId(type = IdType.AUTO) @TableId(type = IdType.AUTO)
private Long id; private Long id;
@NotNull(message = "数据源id不能为空")
private Long dataSourceId;
@NotEmpty(message = "数据源描述不能为空") @NotEmpty(message = "数据源描述不能为空")
private String dataSourceDescribe; private String dataSourceDescribe;
@ -51,4 +49,5 @@ public class DataSource{
private Integer dataSourceType; private Integer dataSourceType;
@TableField(exist = false) @TableField(exist = false)
private String dataSourceTypeName; private String dataSourceTypeName;
} }

View File

@ -31,6 +31,7 @@ public class DatabaseConfig {
private String tableName; private String tableName;
@ApiModelProperty(value = "数据库库名") @ApiModelProperty(value = "数据库库名")
private String warehouseName; private String warehouseName;
@ApiModelProperty(value = "数据表输入输出状态 0输入 1输出")
private Integer data;
} }

View File

@ -1,8 +1,8 @@
package com.etl.data.source.common.pojo; package com.etl.data.source.common.pojo;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import javax.persistence.*;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import java.util.Date; import java.util.Date;
@ -12,10 +12,10 @@ import java.util.Date;
*/ */
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@TableName("task_manager")
@ApiModel(value = "Task", description = "任务实体类") @ApiModel(value = "Task", description = "任务实体类")
public class Task { public class Task {
@ApiModelProperty(value = "主键ID", example = "1") @ApiModelProperty(value = "主键ID", example = "1")
private Integer id; private Integer id;

View File

@ -3,6 +3,7 @@ package com.etl.data.source.server.controller;
import com.etl.data.source.server.service.DataSheetService; import com.etl.data.source.server.service.DataSheetService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
/** /**
@ -16,4 +17,6 @@ import org.springframework.web.bind.annotation.RestController;
public class DataSheetController { public class DataSheetController {
@Autowired @Autowired
private DataSheetService dataSheetService; private DataSheetService dataSheetService;
} }

View File

@ -5,6 +5,7 @@ import com.etl.data.source.common.config.Limit;
import com.etl.data.source.common.pojo.DatabaseConfig; import com.etl.data.source.common.pojo.DatabaseConfig;
import com.etl.data.source.common.pojo.DatabaseRedis; import com.etl.data.source.common.pojo.DatabaseRedis;
import com.etl.data.source.common.pojo.resq.ColumnInfo; import com.etl.data.source.common.pojo.resq.ColumnInfo;
import com.etl.data.source.common.pojo.until.R;
import com.etl.data.source.server.service.DatabaseService; import com.etl.data.source.server.service.DatabaseService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
@ -60,4 +61,16 @@ public class DatabaseController {
return Result.success(databaseService.findDatabaseTableField(config)); return Result.success(databaseService.findDatabaseTableField(config));
} }
@PostMapping("/findDatabaseTableFieldPrice")
@ApiOperation("数据表输入")
public Result<List<Map<String, Object>> > findDatabaseTableFieldPrice(@Valid @RequestBody DatabaseConfig config){
return Result.success(databaseService.findDatabaseTableFieldPrice(config));
}
@PostMapping("outDatabaseTableFieldPrice")
@ApiOperation("数据表输出")
public Result outDatabaseTableFieldPrice(@Valid @RequestBody DatabaseConfig config){
return Result.success(databaseService.findDatabaseTableFieldPrice(config));
}
} }

View File

@ -0,0 +1,57 @@
package com.etl.data.source.server.controller;
import com.etl.data.source.common.pojo.Code;
import com.etl.data.source.common.pojo.Rule;
import com.etl.data.source.common.pojo.req.RuleReq;
import com.etl.data.source.common.pojo.until.R;
import com.etl.data.source.server.service.RuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @ClassName RuleController
* @Description
* @Author ZeZhang.Liu
* @Date 2024/6/26 9:33
*/
@RestController
@Api(tags = "规则管理")
public class RuleController {
@Autowired
private RuleService ruleService;
@GetMapping("findRule")
@ApiOperation(value = "查询规则")
public R findRule(@RequestBody RuleReq ruleReq) {
List<Rule> ruleList=ruleService.findRule(ruleReq);
return R.ok(ruleList);
}
@PostMapping("addRule")
@ApiOperation(value = "添加规则")
public R addRule(@RequestBody Rule rule) {
return ruleService.addRule(rule);
}
@PutMapping("updateRule")
@ApiOperation(value = "修改规则")
public R updateRule(@RequestBody Rule rule) {
return ruleService.updateRule(rule);
}
@DeleteMapping("deleteRule")
@ApiOperation(value = "删除规则")
public R deleteRule(@RequestParam("id") Long id) {
return ruleService.deleteRule(id);
}
@PostMapping("/addCode")
@ApiOperation(value = "添加规则")
public R addCode(@RequestBody Code code) {
return ruleService.addCode(code);
}
}

View File

@ -17,7 +17,7 @@ import java.util.List;
* @Date 2024/6/25 20:59 * @Date 2024/6/25 20:59
*/ */
@RestController @RestController
@Api("任务模块") @Api(tags = "任务模块")
public class TaskController { public class TaskController {
@Autowired @Autowired
private TaskService taskService; private TaskService taskService;

View File

@ -0,0 +1,13 @@
package com.etl.data.source.server.mapper;
import com.etl.data.source.common.pojo.Code;
import com.github.yulichang.base.MPJBaseMapper;
/**
* @ClassName CodeBaseMapper
* @Description
* @Author ZeZhang.Liu
* @Date 2024/6/26 9:36
*/
public interface CodeBaseMapper extends MPJBaseMapper<Code> {
}

View File

@ -0,0 +1,13 @@
package com.etl.data.source.server.mapper;
import com.etl.data.source.common.pojo.Rule;
import com.github.yulichang.base.MPJBaseMapper;
/**
* @ClassName RuleBaseMapper
* @Description
* @Author ZeZhang.Liu
* @Date 2024/6/26 9:35
*/
public interface RuleBaseMapper extends MPJBaseMapper<Rule> {
}

View File

@ -29,4 +29,5 @@ public interface DatabaseService {
Map<String, String> testDatabaseRedis(DatabaseRedis databaseRedis); Map<String, String> testDatabaseRedis(DatabaseRedis databaseRedis);
List<Map<String, Object>> findDatabaseTableFieldPrice(DatabaseConfig config);
} }

View File

@ -0,0 +1,27 @@
package com.etl.data.source.server.service;
import com.etl.data.source.common.pojo.Code;
import com.etl.data.source.common.pojo.Rule;
import com.etl.data.source.common.pojo.req.RuleReq;
import com.etl.data.source.common.pojo.until.R;
import com.github.yulichang.base.MPJBaseService;
import java.util.List;
/**
* @ClassName RuleService
* @Description
* @Author ZeZhang.Liu
* @Date 2024/6/26 9:33
*/
public interface RuleService extends MPJBaseService<Rule> {
List<Rule> findRule(RuleReq ruleReq);
R addRule(Rule rule);
R addCode(Code code);
R updateRule(Rule rule);
R deleteRule(Long id);
}

View File

@ -9,6 +9,7 @@ import com.etl.data.source.server.config.RedisConfig;
import com.etl.data.source.server.mapper.DataSourceBaseMapper; import com.etl.data.source.server.mapper.DataSourceBaseMapper;
import com.etl.data.source.server.mapper.DataSourceTypeBaseMapper; import com.etl.data.source.server.mapper.DataSourceTypeBaseMapper;
import com.etl.data.source.server.service.DatabaseService; import com.etl.data.source.server.service.DatabaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.jdbc.datasource.DriverManagerDataSource;
@ -29,6 +30,7 @@ import java.util.*;
* @Date 2024/6/21 20:16 * @Date 2024/6/21 20:16
*/ */
@Service @Service
@Slf4j
public class DatabaseServiceImpl implements DatabaseService { public class DatabaseServiceImpl implements DatabaseService {
@Autowired @Autowired
private DataSourceBaseMapper dataSourceBaseMapper; private DataSourceBaseMapper dataSourceBaseMapper;
@ -159,54 +161,140 @@ public class DatabaseServiceImpl implements DatabaseService {
return columnInfos; return columnInfos;
} }
@Override
public List<Map<String, Object>> findDatabaseTableFieldPrice(DatabaseConfig config) {
List<Map<String, Object>> results = new ArrayList<>();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = getConnection(config);
// 这里我们仅作为示例直接构建SQL查询语句实际中可能需要更复杂的逻辑来构建SQL
String sql = "SELECT * FROM " + config.getWarehouseName() + "." + config.getTableName();
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
while (resultSet.next()) {
Map<String, Object> rowData = new HashMap<>();
for (int i = 1; i <= columnCount; i++) {
String columnName = metaData.getColumnName(i);
Object columnValue = resultSet.getObject(i);
rowData.put(columnName, columnValue);
}
results.add(rowData);
}
} catch (SQLException e) {
// 处理异常
e.printStackTrace();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} finally {
// 关闭资源
try {
if (resultSet != null) resultSet.close();
if (preparedStatement != null) preparedStatement.close();
if (connection != null) connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(1 == config.getData()){
insertDataExcludingId(config, results);
}
return results;
}
public void insertDataExcludingId(DatabaseConfig config, List<Map<String, Object>> results) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = getConnection(config);
for (Map<String, Object> rowData : results) {
// 获取列名并构建 SQL 语句
List<String> columnNames = new ArrayList<>(rowData.keySet());
columnNames.remove("id"); // 移除 id 列
StringBuilder sql = new StringBuilder("INSERT INTO ");
sql.append(config.getWarehouseName()).append(".").append(config.getTableName()).append(" (");
for (int i = 0; i < columnNames.size(); i++) {
if (i > 0) {
sql.append(", ");
}
sql.append(columnNames.get(i));
}
sql.append(") VALUES (");
// 添加占位符
for (int i = 0; i < columnNames.size(); i++) {
if (i > 0) {
sql.append(", ");
}
sql.append("?");
}
sql.append(")");
preparedStatement = connection.prepareStatement(sql.toString());
// 填充数据并执行 INSERT
int index = 1;
for (String columnName : columnNames) {
preparedStatement.setObject(index++, rowData.get(columnName));
}
preparedStatement.executeUpdate();
}
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
} finally {
// 关闭资源
try {
if (preparedStatement != null) preparedStatement.close();
if (connection != null) connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Override @Override
public Map<String, String> testDatabaseRedis(DatabaseRedis databaseRedis) { public Map<String, String> testDatabaseRedis(DatabaseRedis databaseRedis) {
// 创建一个空的HashMap来存储Redis中的所有键值对 // 创建一个空的HashMap来存储Redis中的所有键值对
Map<String, String> allData = new HashMap<>(); Map<String, String> allData = new HashMap<>();
// 从Redis配置中获取Jedis连接池 // 从Redis配置中获取Jedis连接池
JedisPool jedisPool = RedisConfig.getJedisPool(databaseRedis); JedisPool jedisPool = RedisConfig.getJedisPool(databaseRedis);
// 从连接池中获取一个Jedis资源 // 从连接池中获取一个Jedis资源
Jedis resource = jedisPool.getResource(); Jedis resource = jedisPool.getResource();
// 创建一个ScanParams对象设置每次迭代返回的键的数量这里设置为100 // 创建一个ScanParams对象设置每次迭代返回的键的数量这里设置为100
ScanParams scanParams = new ScanParams().count(100); ScanParams scanParams = new ScanParams().count(100);
// 初始化游标为0表示开始迭代 // 初始化游标为0表示开始迭代
String cursor = "0"; String cursor = "0";
// 使用do-while循环来迭代Redis中的所有键 // 使用do-while循环来迭代Redis中的所有键
do { do {
// 使用scan方法和ScanParams对象来获取一批键和新的游标 // 使用scan方法和ScanParams对象来获取一批键和新的游标
ScanResult<String> scanResult = resource.scan(cursor, scanParams); ScanResult<String> scanResult = resource.scan(cursor, scanParams);
// 更新游标以便下一次迭代 // 更新游标以便下一次迭代
cursor = scanResult.getCursor(); cursor = scanResult.getCursor();
// 获取当前迭代返回的键列表 // 获取当前迭代返回的键列表
List<String> keys = scanResult.getResult(); List<String> keys = scanResult.getResult();
// 遍历键列表,并获取每个键对应的值 // 遍历键列表,并获取每个键对应的值
for (String key : keys) { for (String key : keys) {
// 假设所有键对应的值都是字符串类型 // 假设所有键对应的值都是字符串类型
String value = resource.get(key); String value = resource.get(key);
// 如果值不为null则将其添加到Map中 // 如果值不为null则将其添加到Map中
if (value != null) { if (value != null) {
// 将键值对添加到Map中 // 将键值对添加到Map中
allData.put(key, value); allData.put(key, value);
} }
} }
// 当游标为"0"时,表示迭代完成
// 当游标为"0"时,表示迭代完成
} while (!cursor.equals("0")); } while (!cursor.equals("0"));
// 返回包含所有键值对的Map // 返回包含所有键值对的Map
return allData; return allData;
} }
private static Connection getConnection(DatabaseConfig config) throws ClassNotFoundException, SQLException { private static Connection getConnection(DatabaseConfig config) throws ClassNotFoundException, SQLException {
// 加载MySQL驱动 // 加载MySQL驱动
Class.forName(config.getDriverClassName()); Class.forName(config.getDriverClassName());

View File

@ -0,0 +1,95 @@
package com.etl.data.source.server.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etl.data.source.common.pojo.Code;
import com.etl.data.source.common.pojo.CodeType;
import com.etl.data.source.common.pojo.Rule;
import com.etl.data.source.common.pojo.RuleType;
import com.etl.data.source.common.pojo.req.RuleReq;
import com.etl.data.source.common.pojo.until.R;
import com.etl.data.source.server.mapper.CodeBaseMapper;
import com.etl.data.source.server.mapper.RuleBaseMapper;
import com.etl.data.source.server.service.RuleService;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @ClassName RuleServiceImpl
* @Description
* @Author ZeZhang.Liu
* @Date 2024/6/26 9:34
*/
@Service
public class RuleServiceImpl extends MPJBaseServiceImpl<RuleBaseMapper, Rule> implements RuleService {
@Autowired
private RuleBaseMapper ruleBaseMapper;
@Autowired
private CodeBaseMapper codeBaseMapper;
@Override
public List<Rule> findRule(RuleReq ruleReq) {
MPJLambdaWrapper<Rule> ruleMPJLambdaWrapper = new MPJLambdaWrapper<>();
ruleMPJLambdaWrapper.select(Rule::getId, Rule::getRuleTypeId, Rule::getRuleName)
.select(RuleType::getRuleTypeName)
.select(CodeType::getCodeTypeName)
.leftJoin(RuleType.class, RuleType::getId, Rule::getRuleTypeId)
.leftJoin(Code.class, Code::getRuleId, Rule::getId)
.leftJoin(CodeType.class, CodeType::getId, Code::getCodeTypeId);
if (ruleReq.getRuleTypeId() != null) {
ruleMPJLambdaWrapper.eq(Rule::getRuleTypeId, ruleReq.getRuleTypeId());
}
if (ruleReq.getCodeTypeId() != null) {
ruleMPJLambdaWrapper.eq(Code::getCodeTypeId, ruleReq.getCodeTypeId());
}
Page<Rule> page = new Page<>(ruleReq.getPageNum(), ruleReq.getPageSize());
IPage<Rule> ruleIPage = ruleBaseMapper.selectJoinPage(page, Rule.class, ruleMPJLambdaWrapper);
List<Rule> records = ruleIPage.getRecords();
return records;
}
@Override
public R addRule(Rule rule) {
if (rule != null){
if (ruleBaseMapper.insert(rule) > 0){
return R.ok("添加成功");
}
}
return R.fail("添加失败");
}
@Override
public R updateRule(Rule rule) {
if (rule != null){
if (ruleBaseMapper.updateById(rule) > 0){
return R.ok("修改成功");
}
}
return R.fail("修改失败");
}
@Override
public R deleteRule(Long id) {
if (id != null){
if (ruleBaseMapper.deleteById(id) > 0){
return R.ok("删除成功");
}
}
return R.fail("删除失败");
}
@Override
public R addCode(Code code) {
if (code != null){
if (codeBaseMapper.insert(code) > 0){
return R.ok("添加成功");
}
}
return R.fail("添加失败");
}
}

Some files were not shown because too many files have changed in this diff Show More