Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 04ff87544e | |||
| 2c82c3b57a | |||
| 20b02ddd31 | |||
| 44a0b6028e |
51
pom.xml
51
pom.xml
@@ -8,7 +8,7 @@
|
|||||||
<version>3.3.0</version>
|
<version>3.3.0</version>
|
||||||
<relativePath/> <!-- lookup parent from repository -->
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
</parent>
|
</parent>
|
||||||
<groupId>com.rainbus</groupId>
|
<groupId>com.dlp.admin</groupId>
|
||||||
<artifactId>dlp-admin-backend</artifactId>
|
<artifactId>dlp-admin-backend</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<name>dlp-admin-backend</name>
|
<name>dlp-admin-backend</name>
|
||||||
@@ -20,15 +20,15 @@
|
|||||||
<lomstrcut-binding.version>0.2.0</lomstrcut-binding.version>
|
<lomstrcut-binding.version>0.2.0</lomstrcut-binding.version>
|
||||||
<poi.version>5.2.5</poi.version>
|
<poi.version>5.2.5</poi.version>
|
||||||
<commons-lang3.version>3.14.0</commons-lang3.version>
|
<commons-lang3.version>3.14.0</commons-lang3.version>
|
||||||
|
<commons-text.version>1.12.0</commons-text.version>
|
||||||
<open-api.version>2.5.0</open-api.version>
|
<open-api.version>2.5.0</open-api.version>
|
||||||
<jedis.version>5.1.3</jedis.version>
|
<jedis.version>5.1.3</jedis.version>
|
||||||
<jwt.version>4.4.0</jwt.version>
|
<jwt.version>4.4.0</jwt.version>
|
||||||
|
<mybatis-plus.version>3.5.6</mybatis-plus.version>
|
||||||
|
<page-helper.version>2.1.0</page-helper.version>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
@@ -45,6 +45,21 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- <dependency>-->
|
||||||
|
<!-- <groupId>com.baomidou</groupId>-->
|
||||||
|
<!-- <artifactId>mybatis-plus-spring-boot3-starter</artifactId>-->
|
||||||
|
<!-- <version>${mybatis-plus.version}</version>-->
|
||||||
|
<!-- </dependency>-->
|
||||||
|
<!-- <dependency>-->
|
||||||
|
<!-- <groupId>com.github.pagehelper</groupId>-->
|
||||||
|
<!-- <artifactId>pagehelper-spring-boot-starter</artifactId>-->
|
||||||
|
<!-- <version>${page-helper.version}</version>-->
|
||||||
|
<!-- </dependency>-->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.mysql</groupId>
|
<groupId>com.mysql</groupId>
|
||||||
@@ -74,6 +89,11 @@
|
|||||||
<artifactId>commons-lang3</artifactId>
|
<artifactId>commons-lang3</artifactId>
|
||||||
<version>${commons-lang3.version}</version>
|
<version>${commons-lang3.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-text</artifactId>
|
||||||
|
<version>${commons-text.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springdoc</groupId>
|
<groupId>org.springdoc</groupId>
|
||||||
@@ -93,6 +113,19 @@
|
|||||||
<version>${jwt.version}</version>
|
<version>${jwt.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||||
|
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||||
|
<artifactId>jackson-datatype-hibernate6</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@@ -134,9 +167,17 @@
|
|||||||
<version>${lomstrcut-binding.version}</version>
|
<version>${lomstrcut-binding.version}</version>
|
||||||
</path>
|
</path>
|
||||||
</annotationProcessorPaths>
|
</annotationProcessorPaths>
|
||||||
|
<compilerArgs>--enable-preview</compilerArgs>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>maven_central</id>
|
||||||
|
<name>Maven Central</name>
|
||||||
|
<url>https://repo.maven.apache.org/maven2/</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
package com.rainbus.dlp;
|
package com.dlp.admin;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
|
||||||
|
|
||||||
@EnableJpaAuditing
|
|
||||||
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
|
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
|
||||||
public class DlpAdminBackendApplication {
|
public class DlpAdminBackendApplication {
|
||||||
|
|
||||||
@@ -1,9 +1,8 @@
|
|||||||
package com.rainbus.dlp.config;
|
package com.dlp.admin.config;
|
||||||
|
|
||||||
import com.rainbus.dlp.entity.dto.user.CustomUserDetails;
|
import com.dlp.admin.entity.dto.user.TokenClaims;
|
||||||
import com.rainbus.dlp.entity.dto.user.TokenClaims;
|
import com.dlp.admin.service.UserServ;
|
||||||
import com.rainbus.dlp.service.UserServ;
|
import com.dlp.admin.util.JwtUtil;
|
||||||
import com.rainbus.dlp.util.JwtUtil;
|
|
||||||
import jakarta.servlet.FilterChain;
|
import jakarta.servlet.FilterChain;
|
||||||
import jakarta.servlet.ServletException;
|
import jakarta.servlet.ServletException;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
@@ -13,20 +12,14 @@ import org.apache.commons.lang3.StringUtils;
|
|||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
import org.springframework.security.core.Authentication;
|
|
||||||
import org.springframework.security.core.GrantedAuthority;
|
|
||||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
import org.springframework.security.core.userdetails.User;
|
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
|
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.filter.OncePerRequestFilter;
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@@ -42,10 +35,10 @@ public class JwtFilter extends OncePerRequestFilter {
|
|||||||
filterChain.doFilter(request, response);
|
filterChain.doFilter(request, response);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
TokenClaims claims = JwtUtil.parseValidToken(token);
|
TokenClaims claims = JwtUtil.parseValidToken(token);
|
||||||
UserDetails userDetails = userServ.loadUserByUsername(claims.getUsername());
|
UserDetails userDetails = userServ.loadUserByUsername(claims.getUsername());
|
||||||
List<GrantedAuthority> authorities = claims.getRoles().stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
|
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
|
||||||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, authorities);
|
|
||||||
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
|
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
|
||||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||||
filterChain.doFilter(request, response);
|
filterChain.doFilter(request, response);
|
||||||
20
src/main/java/com/dlp/admin/config/OpenApiConf.java
Normal file
20
src/main/java/com/dlp/admin/config/OpenApiConf.java
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package com.dlp.admin.config;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.models.OpenAPI;
|
||||||
|
import io.swagger.v3.oas.models.info.Info;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class OpenApiConf {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public OpenAPI springOpenAPI() {
|
||||||
|
return new OpenAPI()
|
||||||
|
.info(new Info()
|
||||||
|
.title("DLP Admin")
|
||||||
|
.description("DLP Admin APIs")
|
||||||
|
.version("0.0.1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,39 +1,34 @@
|
|||||||
package com.rainbus.dlp.config;
|
package com.dlp.admin.config;
|
||||||
|
|
||||||
|
|
||||||
import com.rainbus.dlp.entity.pojo.user.SysRole;
|
import com.dlp.admin.entity.pojo.user.SysRole;
|
||||||
import com.rainbus.dlp.repository.user.SysRoleRepo;
|
import com.dlp.admin.repository.user.SysRoleRepo;
|
||||||
import com.rainbus.dlp.service.UserServ;
|
import jakarta.transaction.Transactional;
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
import org.springframework.security.authentication.AuthenticationProvider;
|
|
||||||
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
|
||||||
import org.springframework.security.config.Customizer;
|
import org.springframework.security.config.Customizer;
|
||||||
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
|
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
|
||||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
||||||
import org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer;
|
|
||||||
import org.springframework.security.config.annotation.web.configurers.SessionManagementConfigurer;
|
|
||||||
import org.springframework.security.config.http.SessionCreationPolicy;
|
import org.springframework.security.config.http.SessionCreationPolicy;
|
||||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.security.web.SecurityFilterChain;
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class SecurityConfig {
|
public class SecurityConf {
|
||||||
|
|
||||||
private final UserServ userServ;
|
private final SecurityDataService securityDataService;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SecurityFilterChain filterChain(HttpSecurity http, JwtFilter jwtFilter) throws Exception {
|
public SecurityFilterChain filterChain(HttpSecurity http, JwtFilter jwtFilter) throws Exception {
|
||||||
@@ -41,7 +36,8 @@ public class SecurityConfig {
|
|||||||
.csrf(AbstractHttpConfigurer::disable)
|
.csrf(AbstractHttpConfigurer::disable)
|
||||||
.sessionManagement(sess -> sess.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
.sessionManagement(sess -> sess.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
||||||
.authorizeHttpRequests((authorizeHttpRequests) -> {
|
.authorizeHttpRequests((authorizeHttpRequests) -> {
|
||||||
userServ.getAllRoles().forEach(role -> {
|
//TODO: Lazy load problem
|
||||||
|
securityDataService.fetchAllRoles().forEach(role -> {
|
||||||
role.getResources().forEach(resource -> {
|
role.getResources().forEach(resource -> {
|
||||||
authorizeHttpRequests.requestMatchers(
|
authorizeHttpRequests.requestMatchers(
|
||||||
HttpMethod.valueOf(resource.getRequestMethod().name()),
|
HttpMethod.valueOf(resource.getRequestMethod().name()),
|
||||||
@@ -67,4 +63,18 @@ public class SecurityConfig {
|
|||||||
return new BCryptPasswordEncoder();
|
return new BCryptPasswordEncoder();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
static class SecurityDataService {
|
||||||
|
|
||||||
|
private final SysRoleRepo roleRepo;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public List<SysRole> fetchAllRoles() {
|
||||||
|
return roleRepo.findAll().stream().filter(role -> !role.getResources().isEmpty()).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
16
src/main/java/com/dlp/admin/controller/ExceptionCtrl.java
Normal file
16
src/main/java/com/dlp/admin/controller/ExceptionCtrl.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package com.dlp.admin.controller;
|
||||||
|
|
||||||
|
import com.dlp.admin.entity.exception.UsernameOrPasswordExcp;
|
||||||
|
import com.dlp.admin.entity.resp.Resp;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
@RestControllerAdvice
|
||||||
|
public class ExceptionCtrl {
|
||||||
|
|
||||||
|
@ExceptionHandler(UsernameOrPasswordExcp.class)
|
||||||
|
public Resp<Object> handler(UsernameOrPasswordExcp e) {
|
||||||
|
return Resp.fail(e.getCode(), e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
102
src/main/java/com/dlp/admin/controller/UserCtrl.java
Normal file
102
src/main/java/com/dlp/admin/controller/UserCtrl.java
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
package com.dlp.admin.controller;
|
||||||
|
|
||||||
|
import com.dlp.admin.entity.pojo.user.SysResource;
|
||||||
|
import com.dlp.admin.entity.pojo.user.SysRole;
|
||||||
|
import com.dlp.admin.entity.req.user.*;
|
||||||
|
import com.dlp.admin.entity.resp.PageResp;
|
||||||
|
import com.dlp.admin.entity.resp.Resp;
|
||||||
|
import com.dlp.admin.entity.resp.user.GetTagResp;
|
||||||
|
import com.dlp.admin.entity.resp.user.GetUserResp;
|
||||||
|
import com.dlp.admin.entity.resp.user.LoginResp;
|
||||||
|
import com.dlp.admin.repository.user.SysRoleRepo;
|
||||||
|
import com.dlp.admin.service.UserServ;
|
||||||
|
import com.dlp.admin.util.RedisUtil;
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/user")
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class UserCtrl {
|
||||||
|
|
||||||
|
private final RedisUtil redisUtil;
|
||||||
|
private final UserServ userServ;
|
||||||
|
private final SysRoleRepo sysRoleRepo;
|
||||||
|
|
||||||
|
@PostMapping("/login")
|
||||||
|
@Operation(summary = "登录接口")
|
||||||
|
public Resp<LoginResp> login(@RequestBody LoginReq req) {
|
||||||
|
return Resp.success(userServ.login(req));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/list")
|
||||||
|
@Operation(summary = "搜索用户")
|
||||||
|
public Resp<PageResp<GetUserResp>> listUser(@RequestBody ListUserReq req) {
|
||||||
|
return Resp.success(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/add")
|
||||||
|
@Operation(summary = "增加用户")
|
||||||
|
public Resp<Object> addUser(@RequestBody AddUserReq req) {
|
||||||
|
return Resp.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/get/{id}")
|
||||||
|
@Operation(summary = "获取用户")
|
||||||
|
public Resp<GetUserResp> getUser(@PathVariable("id") Long id) {
|
||||||
|
return Resp.success(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/update")
|
||||||
|
@Operation(summary = "更新用户")
|
||||||
|
public Resp<Object> updateUser(@RequestBody UpdateUserReq req) {
|
||||||
|
return Resp.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/delete/{id}")
|
||||||
|
@Operation(summary = "删除用户")
|
||||||
|
public Resp<Object> deleteUser(@PathVariable Long id) {
|
||||||
|
return Resp.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/resetPwd/{id}")
|
||||||
|
@Operation(summary = "重置密码")
|
||||||
|
public Resp<Object> resetPwd(@PathVariable Long id) {
|
||||||
|
return Resp.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/tag/add")
|
||||||
|
@Operation(summary = "增加用户标签")
|
||||||
|
private Resp<Object> addUserTag(@RequestBody AddUserTagReq req) {
|
||||||
|
return Resp.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/tag/delete/{id}")
|
||||||
|
@Operation(summary = "删除用户标签")
|
||||||
|
private Resp<Object> deleteUserTag(@PathVariable Long id) {
|
||||||
|
return Resp.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/tag/list")
|
||||||
|
@Operation(summary = "获取所有用户标签")
|
||||||
|
private Resp<List<GetTagResp>> listUserTag() {
|
||||||
|
return Resp.success(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/test")
|
||||||
|
private Resp<Object> test() {
|
||||||
|
SysResource resource = new SysResource();
|
||||||
|
resource.setResource("/user/test");
|
||||||
|
resource.setRequestMethod(RequestMethod.GET);
|
||||||
|
List<SysResource> resources = List.of(resource);
|
||||||
|
// return Resp.success(sysResourceMapper.batchInsert(resources));
|
||||||
|
// return Resp.success(sysRoleMapper.selectRoleIdsByUserId(List.of(1L, 2L)));
|
||||||
|
List<SysRole> roles = sysRoleRepo.findAll();
|
||||||
|
roles.stream().forEach(role -> role.getResources().stream().forEach(resource1 -> System.out.println(resource1.getResource())));
|
||||||
|
return Resp.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
11
src/main/java/com/dlp/admin/entity/ApiDescription.java
Normal file
11
src/main/java/com/dlp/admin/entity/ApiDescription.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package com.dlp.admin.entity;
|
||||||
|
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.METHOD)
|
||||||
|
@Documented
|
||||||
|
public @interface ApiDescription {
|
||||||
|
String value();
|
||||||
|
}
|
||||||
21
src/main/java/com/dlp/admin/entity/converter/SystemConv.java
Normal file
21
src/main/java/com/dlp/admin/entity/converter/SystemConv.java
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package com.dlp.admin.entity.converter;
|
||||||
|
|
||||||
|
import com.dlp.admin.entity.pojo.user.SysResource;
|
||||||
|
import com.dlp.admin.entity.dto.system.ApiInfo;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Mapper(componentModel = "spring")
|
||||||
|
public interface SystemConv {
|
||||||
|
|
||||||
|
@Mapping(target = "id", ignore = true)
|
||||||
|
@Mapping(target = "updatedAt", ignore = true)
|
||||||
|
@Mapping(target = "resource", source = "uri")
|
||||||
|
@Mapping(target = "createdAt", ignore = true)
|
||||||
|
SysResource ApiInfoDto2Pojo(ApiInfo apiInfo);
|
||||||
|
|
||||||
|
List<SysResource> ApiInfosDto2Pojo(List<ApiInfo> apiInfos);
|
||||||
|
|
||||||
|
}
|
||||||
26
src/main/java/com/dlp/admin/entity/converter/UserConv.java
Normal file
26
src/main/java/com/dlp/admin/entity/converter/UserConv.java
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package com.dlp.admin.entity.converter;
|
||||||
|
|
||||||
|
import com.dlp.admin.entity.dto.user.CustomUserDetails;
|
||||||
|
import com.dlp.admin.entity.dto.user.TokenClaims;
|
||||||
|
import com.dlp.admin.entity.pojo.user.SysUser;
|
||||||
|
import com.dlp.admin.entity.req.user.AddUserReq;
|
||||||
|
import org.mapstruct.InheritInverseConfiguration;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
|
||||||
|
@Mapper(componentModel = "spring")
|
||||||
|
public interface UserConv {
|
||||||
|
|
||||||
|
@InheritInverseConfiguration
|
||||||
|
SysUser RegisterReq2Pojo(AddUserReq req, String password);
|
||||||
|
|
||||||
|
@Mapping(target = "roles", expression = "java(user.getRoles().stream().map(com.dlp.admin.entity.pojo.user.SysRole::getRole).toList())")
|
||||||
|
@Mapping(target = "orgId", expression = "java(user.getOrg().getId())")
|
||||||
|
TokenClaims ClaimsPojo2Dto(SysUser user);
|
||||||
|
|
||||||
|
@Mapping(target = "orgId", expression = "java(user.getOrg().getId())")
|
||||||
|
@Mapping(target = "authorities", ignore = true)
|
||||||
|
@Mapping(target = "roles", expression = "java(user.getRoles().stream().map(com.dlp.admin.entity.pojo.user.SysRole::getRole).toList())")
|
||||||
|
CustomUserDetails UserDetailsPojo2Dto(SysUser user);
|
||||||
|
|
||||||
|
}
|
||||||
4
src/main/java/com/dlp/admin/entity/dto/Pair.java
Normal file
4
src/main/java/com/dlp/admin/entity/dto/Pair.java
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
package com.dlp.admin.entity.dto;
|
||||||
|
|
||||||
|
public record Pair<T1, T2>(T1 v1, T2 v2) {
|
||||||
|
}
|
||||||
4
src/main/java/com/dlp/admin/entity/dto/Tuple.java
Normal file
4
src/main/java/com/dlp/admin/entity/dto/Tuple.java
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
package com.dlp.admin.entity.dto;
|
||||||
|
|
||||||
|
public record Tuple<T1, T2, T3>(T1 v1, T2 v2, T3 v3) {
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.dlp.admin.entity.dto.system;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
|
||||||
|
public record ApiInfo(String uri, RequestMethod requestMethod, String description) {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,37 +1,53 @@
|
|||||||
package com.rainbus.dlp.entity.dto.user;
|
package com.dlp.admin.entity.dto.user;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.Getter;
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Getter
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class CustomUserDetails implements UserDetails {
|
public class CustomUserDetails implements UserDetails {
|
||||||
|
|
||||||
|
private Long id;
|
||||||
private String username;
|
private String username;
|
||||||
private String password;
|
private String password;
|
||||||
private List<SimpleGrantedAuthority> authorities;
|
private List<String> roles;
|
||||||
|
private Long orgId;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@JsonIgnore
|
||||||
|
public Collection<? extends GrantedAuthority> getAuthorities() {
|
||||||
|
return roles.stream().map(SimpleGrantedAuthority::new).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@JsonIgnore
|
||||||
public boolean isAccountNonExpired() {
|
public boolean isAccountNonExpired() {
|
||||||
return UserDetails.super.isAccountNonExpired();
|
return UserDetails.super.isAccountNonExpired();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@JsonIgnore
|
||||||
public boolean isAccountNonLocked() {
|
public boolean isAccountNonLocked() {
|
||||||
return UserDetails.super.isAccountNonLocked();
|
return UserDetails.super.isAccountNonLocked();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@JsonIgnore
|
||||||
public boolean isCredentialsNonExpired() {
|
public boolean isCredentialsNonExpired() {
|
||||||
return UserDetails.super.isCredentialsNonExpired();
|
return UserDetails.super.isCredentialsNonExpired();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@JsonIgnore
|
||||||
public boolean isEnabled() {
|
public boolean isEnabled() {
|
||||||
return UserDetails.super.isEnabled();
|
return UserDetails.super.isEnabled();
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package com.rainbus.dlp.entity.dto.user;
|
package com.dlp.admin.entity.dto.user;
|
||||||
|
|
||||||
import com.auth0.jwt.interfaces.Claim;
|
import com.auth0.jwt.interfaces.Claim;
|
||||||
import com.rainbus.dlp.util.ConvertUtil;
|
import com.dlp.admin.util.ConvertUtil;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
@@ -10,9 +10,11 @@ import java.util.Map;
|
|||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class TokenClaims {
|
public class TokenClaims {
|
||||||
|
|
||||||
private Long id;
|
private Long id;
|
||||||
private String username;
|
private String username;
|
||||||
private List<String> roles;
|
private List<String> roles;
|
||||||
|
private Long orgId;
|
||||||
|
|
||||||
public Map<String, Object> toMap() {
|
public Map<String, Object> toMap() {
|
||||||
return ConvertUtil.toMap(this, Object.class);
|
return ConvertUtil.toMap(this, Object.class);
|
||||||
15
src/main/java/com/dlp/admin/entity/enums/ExceptionEnum.java
Normal file
15
src/main/java/com/dlp/admin/entity/enums/ExceptionEnum.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package com.dlp.admin.entity.enums;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public enum ExceptionEnum {
|
||||||
|
USERNAME_OR_PASSWORD_ERROR(40001, "用户名或密码错误"),
|
||||||
|
USER_DISABLED(40002, "用户当前无法使用");
|
||||||
|
|
||||||
|
|
||||||
|
private final int code;
|
||||||
|
private final String message;
|
||||||
|
}
|
||||||
15
src/main/java/com/dlp/admin/entity/enums/OrgTypeEnum.java
Normal file
15
src/main/java/com/dlp/admin/entity/enums/OrgTypeEnum.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package com.dlp.admin.entity.enums;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public enum OrgTypeEnum {
|
||||||
|
COMPANY("公司"), DEPARTMENT("部门"), USER_GROUP("用户组");
|
||||||
|
|
||||||
|
private final String desc;
|
||||||
|
|
||||||
|
OrgTypeEnum(String desc) {
|
||||||
|
this.desc = desc;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.rainbus.dlp.entity.enums;
|
package com.dlp.admin.entity.enums;
|
||||||
|
|
||||||
public enum RoleEnum {
|
public enum RoleEnum {
|
||||||
// system admin
|
// system admin
|
||||||
11
src/main/java/com/dlp/admin/entity/enums/UserStatusEnum.java
Normal file
11
src/main/java/com/dlp/admin/entity/enums/UserStatusEnum.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package com.dlp.admin.entity.enums;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
public enum UserStatusEnum {
|
||||||
|
ACTIVATED("激活"), UNACTIVATED("未激活"), DISABLED("禁用");
|
||||||
|
private final String desc;
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.dlp.admin.entity.exception;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class OrgNotExistExcp extends RuntimeException {
|
||||||
|
private final int code;
|
||||||
|
private final String message;
|
||||||
|
|
||||||
|
public OrgNotExistExcp() {
|
||||||
|
this.code = 40003;
|
||||||
|
this.message = "组织不存在";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.dlp.admin.entity.exception;
|
||||||
|
|
||||||
|
import com.dlp.admin.entity.enums.ExceptionEnum;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class UserDisabledExcp extends RuntimeException {
|
||||||
|
private final int code;
|
||||||
|
private final String message;
|
||||||
|
|
||||||
|
public UserDisabledExcp() {
|
||||||
|
this.code = ExceptionEnum.USER_DISABLED.getCode();
|
||||||
|
this.message = ExceptionEnum.USER_DISABLED.getMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.dlp.admin.entity.exception;
|
||||||
|
|
||||||
|
import com.dlp.admin.entity.enums.ExceptionEnum;
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class UsernameOrPasswordExcp extends RuntimeException {
|
||||||
|
private final int code;
|
||||||
|
private final String message;
|
||||||
|
|
||||||
|
public UsernameOrPasswordExcp() {
|
||||||
|
this.code = ExceptionEnum.USERNAME_OR_PASSWORD_ERROR.getCode();
|
||||||
|
this.message = ExceptionEnum.USERNAME_OR_PASSWORD_ERROR.getMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,39 +1,38 @@
|
|||||||
package com.rainbus.dlp.entity.pojo.user;
|
package com.dlp.admin.entity.pojo.user;
|
||||||
|
|
||||||
|
import com.dlp.admin.entity.enums.OrgTypeEnum;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.springframework.data.annotation.CreatedBy;
|
import org.springframework.data.annotation.CreatedBy;
|
||||||
import org.springframework.data.annotation.CreatedDate;
|
import org.springframework.data.annotation.CreatedDate;
|
||||||
import org.springframework.data.annotation.LastModifiedBy;
|
import org.springframework.data.annotation.LastModifiedBy;
|
||||||
import org.springframework.data.annotation.LastModifiedDate;
|
import org.springframework.data.annotation.LastModifiedDate;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@Entity
|
@Entity
|
||||||
public class SysResource {
|
@EnableJpaAuditing
|
||||||
|
public class SysOrg {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
@Column
|
private String org;
|
||||||
private String resource;
|
|
||||||
|
|
||||||
@Column
|
private OrgTypeEnum type;
|
||||||
@Enumerated(EnumType.STRING)
|
|
||||||
private RequestMethod requestMethod;
|
|
||||||
|
|
||||||
@Column
|
private String fullPathName;
|
||||||
private String description;
|
|
||||||
|
|
||||||
@CreatedBy
|
private String fullPath;
|
||||||
private Long createdBy;
|
|
||||||
|
|
||||||
@LastModifiedBy
|
private Long parentId;
|
||||||
private Long updatedBy;
|
|
||||||
|
private String region;
|
||||||
|
|
||||||
|
private String area;
|
||||||
|
|
||||||
@CreatedDate
|
@CreatedDate
|
||||||
private LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
@@ -41,4 +40,10 @@ public class SysResource {
|
|||||||
@LastModifiedDate
|
@LastModifiedDate
|
||||||
private LocalDateTime updatedAt;
|
private LocalDateTime updatedAt;
|
||||||
|
|
||||||
|
@CreatedBy
|
||||||
|
private Long createdBy;
|
||||||
|
|
||||||
|
@LastModifiedBy
|
||||||
|
private Long updatedBy;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.dlp.admin.entity.pojo.user;
|
||||||
|
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Entity
|
||||||
|
public class SysResource {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String resource;
|
||||||
|
|
||||||
|
@Enumerated(EnumType.STRING)
|
||||||
|
private RequestMethod requestMethod;
|
||||||
|
|
||||||
|
private String description;
|
||||||
|
|
||||||
|
private LocalDateTime createdAt;
|
||||||
|
|
||||||
|
private LocalDateTime updatedAt;
|
||||||
|
|
||||||
|
}
|
||||||
31
src/main/java/com/dlp/admin/entity/pojo/user/SysRole.java
Normal file
31
src/main/java/com/dlp/admin/entity/pojo/user/SysRole.java
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package com.dlp.admin.entity.pojo.user;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Entity
|
||||||
|
public class SysRole {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String role;
|
||||||
|
|
||||||
|
private String description;
|
||||||
|
|
||||||
|
@ManyToMany
|
||||||
|
private List<SysResource> resources;
|
||||||
|
|
||||||
|
private Long createdBy;
|
||||||
|
|
||||||
|
private Long updatedBy;
|
||||||
|
|
||||||
|
private Long createdAt;
|
||||||
|
|
||||||
|
private Long updatedAt;
|
||||||
|
|
||||||
|
}
|
||||||
42
src/main/java/com/dlp/admin/entity/pojo/user/SysUser.java
Normal file
42
src/main/java/com/dlp/admin/entity/pojo/user/SysUser.java
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package com.dlp.admin.entity.pojo.user;
|
||||||
|
|
||||||
|
import com.dlp.admin.entity.enums.UserStatusEnum;
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Entity
|
||||||
|
public class SysUser {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
@ManyToMany
|
||||||
|
private List<SysRole> roles;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
private SysOrg org;
|
||||||
|
|
||||||
|
private UserStatusEnum status;
|
||||||
|
|
||||||
|
private Long createBy;
|
||||||
|
|
||||||
|
private Long updateBy;
|
||||||
|
|
||||||
|
private LocalDateTime createAt;
|
||||||
|
|
||||||
|
private LocalDateTime updateAt;
|
||||||
|
|
||||||
|
private Integer isDeleted;
|
||||||
|
|
||||||
|
}
|
||||||
9
src/main/java/com/dlp/admin/entity/req/PageReq.java
Normal file
9
src/main/java/com/dlp/admin/entity/req/PageReq.java
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package com.dlp.admin.entity.req;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class PageReq {
|
||||||
|
private Integer pageCurrent = 1;
|
||||||
|
private Integer pageSize = 10;
|
||||||
|
}
|
||||||
37
src/main/java/com/dlp/admin/entity/req/user/AddUserReq.java
Normal file
37
src/main/java/com/dlp/admin/entity/req/user/AddUserReq.java
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package com.dlp.admin.entity.req.user;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.Email;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import jakarta.validation.constraints.Pattern;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class AddUserReq {
|
||||||
|
|
||||||
|
@Pattern(regexp = "^[a-zA-Z0-9_-]{4,16}$", message = "用户名格式不正确")
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
@Pattern(regexp = "^[\\u4E00-\\u9FA5]{2,4}$", message = "姓名格式不正确")
|
||||||
|
private String realName;
|
||||||
|
|
||||||
|
@Email(message = "邮箱格式不正确")
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private Long orgId;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private String region;
|
||||||
|
|
||||||
|
@Pattern(regexp = "^1(3\\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\\d|9[0-35-9])\\d{8}$", message = "手机号格式不正确")
|
||||||
|
private String mobile;
|
||||||
|
|
||||||
|
@Pattern(regexp = "0\\d{2,3}-\\d{7,8}$", message = "座机号格式不正确")
|
||||||
|
private String phone;
|
||||||
|
|
||||||
|
private String remark;
|
||||||
|
|
||||||
|
private Long tagId;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.dlp.admin.entity.req.user;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class AddUserTagReq {
|
||||||
|
private String tag;
|
||||||
|
private String remark;
|
||||||
|
}
|
||||||
14
src/main/java/com/dlp/admin/entity/req/user/ListUserReq.java
Normal file
14
src/main/java/com/dlp/admin/entity/req/user/ListUserReq.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package com.dlp.admin.entity.req.user;
|
||||||
|
|
||||||
|
import com.dlp.admin.entity.req.PageReq;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
public class ListUserReq extends PageReq {
|
||||||
|
private String username;
|
||||||
|
private String email;
|
||||||
|
private String region;
|
||||||
|
private Long orgId;
|
||||||
|
}
|
||||||
15
src/main/java/com/dlp/admin/entity/req/user/LoginReq.java
Normal file
15
src/main/java/com/dlp/admin/entity/req/user/LoginReq.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package com.dlp.admin.entity.req.user;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.Pattern;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class LoginReq {
|
||||||
|
|
||||||
|
@Pattern(regexp = "^[a-zA-Z0-9_-]{4,16}$", message = "用户名格式不正确")
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
@Pattern(regexp = "^[a-zA-Z0-9_-]{8,16}$", message = "密码格式不正确")
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package com.dlp.admin.entity.req.user;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.Email;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import jakarta.validation.constraints.Pattern;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class UpdateUserReq {
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Pattern(regexp = "^[a-zA-Z0-9_-]{4,16}$", message = "用户名格式不正确")
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
@Pattern(regexp = "^[\\u4E00-\\u9FA5]{2,4}$", message = "姓名格式不正确")
|
||||||
|
private String realName;
|
||||||
|
|
||||||
|
@Email(message = "邮箱格式不正确")
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private Long orgId;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private String region;
|
||||||
|
|
||||||
|
@Pattern(regexp = "^1(3\\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\\d|9[0-35-9])\\d{8}$", message = "手机号格式不正确")
|
||||||
|
private String mobile;
|
||||||
|
|
||||||
|
@Pattern(regexp = "0\\d{2,3}-\\d{7,8}$", message = "座机号格式不正确")
|
||||||
|
private String phone;
|
||||||
|
|
||||||
|
private String remark;
|
||||||
|
|
||||||
|
}
|
||||||
13
src/main/java/com/dlp/admin/entity/resp/PageResp.java
Normal file
13
src/main/java/com/dlp/admin/entity/resp/PageResp.java
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package com.dlp.admin.entity.resp;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class PageResp<T> {
|
||||||
|
private List<T> list;
|
||||||
|
private Long total;
|
||||||
|
private Long pageSize;
|
||||||
|
private Long current;
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.rainbus.dlp.entity.resp;
|
package com.dlp.admin.entity.resp;
|
||||||
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
@@ -20,6 +20,12 @@ public class Resp<T> {
|
|||||||
return new Resp<>(200, "success", null);
|
return new Resp<>(200, "success", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static Resp<Object> fail(int code, String msg) {
|
||||||
|
return new Resp<>(code, msg, null);
|
||||||
|
}
|
||||||
|
|
||||||
public static Resp<Object> fail(String msg) {
|
public static Resp<Object> fail(String msg) {
|
||||||
return new Resp<>(400, msg, null);
|
return new Resp<>(400, msg, null);
|
||||||
}
|
}
|
||||||
10
src/main/java/com/dlp/admin/entity/resp/user/GetTagResp.java
Normal file
10
src/main/java/com/dlp/admin/entity/resp/user/GetTagResp.java
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package com.dlp.admin.entity.resp.user;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class GetTagResp {
|
||||||
|
private Long id;
|
||||||
|
private String tag;
|
||||||
|
private String remark;
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package com.dlp.admin.entity.resp.user;
|
||||||
|
|
||||||
|
import com.dlp.admin.entity.enums.UserStatusEnum;
|
||||||
|
import com.dlp.admin.entity.pojo.user.SysOrg;
|
||||||
|
import com.dlp.admin.entity.pojo.user.SysRole;
|
||||||
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class GetUserResp {
|
||||||
|
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
private String email;
|
||||||
|
|
||||||
|
private List<SysRole> roles;
|
||||||
|
|
||||||
|
private SysOrg org;
|
||||||
|
|
||||||
|
@Schema(description = "未定字段格式")
|
||||||
|
private List<Object> tags;
|
||||||
|
|
||||||
|
private UserStatusEnum status;
|
||||||
|
|
||||||
|
private Long createBy;
|
||||||
|
|
||||||
|
private Long updateBy;
|
||||||
|
|
||||||
|
private LocalDateTime createAt;
|
||||||
|
|
||||||
|
private LocalDateTime updateAt;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.dlp.admin.entity.resp.user;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class LoginResp {
|
||||||
|
private String token;
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.dlp.admin.repository.user;
|
||||||
|
|
||||||
|
import com.dlp.admin.entity.pojo.user.SysOrg;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
public interface SysOrgRepo extends JpaRepository<SysOrg, Long> {
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package com.rainbus.dlp.repository.user;
|
package com.dlp.admin.repository.user;
|
||||||
|
|
||||||
import com.rainbus.dlp.entity.pojo.user.SysResource;
|
import com.dlp.admin.entity.pojo.user.SysResource;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
public interface SysResourceRepo extends JpaRepository<SysResource, Long> {
|
public interface SysResourceRepo extends JpaRepository<SysResource, Long> {
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package com.rainbus.dlp.repository.user;
|
package com.dlp.admin.repository.user;
|
||||||
|
|
||||||
import com.rainbus.dlp.entity.pojo.user.SysRole;
|
import com.dlp.admin.entity.pojo.user.SysRole;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
public interface SysRoleRepo extends JpaRepository<SysRole, Long> {
|
public interface SysRoleRepo extends JpaRepository<SysRole, Long> {
|
||||||
@@ -1,12 +1,10 @@
|
|||||||
package com.rainbus.dlp.repository.user;
|
package com.dlp.admin.repository.user;
|
||||||
|
|
||||||
import com.rainbus.dlp.entity.pojo.user.SysUser;
|
import com.dlp.admin.entity.pojo.user.SysUser;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface SysUserRepo extends JpaRepository<SysUser, Long> {
|
public interface SysUserRepo extends JpaRepository<SysUser, Long> {
|
||||||
|
|
||||||
Optional<SysUser> findByUsername(String username);
|
Optional<SysUser> findByUsername(String username);
|
||||||
|
}
|
||||||
}
|
|
||||||
55
src/main/java/com/dlp/admin/service/SystemServ.java
Normal file
55
src/main/java/com/dlp/admin/service/SystemServ.java
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package com.dlp.admin.service;
|
||||||
|
|
||||||
|
import com.dlp.admin.entity.converter.SystemConv;
|
||||||
|
import com.dlp.admin.entity.dto.system.ApiInfo;
|
||||||
|
|
||||||
|
import com.dlp.admin.entity.pojo.user.SysResource;
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.method.HandlerMethod;
|
||||||
|
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
|
||||||
|
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||||
|
import org.springframework.web.util.pattern.PathPattern;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class SystemServ {
|
||||||
|
|
||||||
|
private final ApplicationContext applicationContext;
|
||||||
|
private final SystemConv systemConv;
|
||||||
|
|
||||||
|
public List<ApiInfo> getSystemApis() {
|
||||||
|
|
||||||
|
RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
|
||||||
|
Map<RequestMappingInfo, HandlerMethod> methodMap = mapping.getHandlerMethods();
|
||||||
|
|
||||||
|
|
||||||
|
List<ApiInfo> apiInfos = new LinkedList<>();
|
||||||
|
methodMap.forEach((info, func) -> {
|
||||||
|
if (Objects.isNull(info.getPathPatternsCondition())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<PathPattern> patterns = info.getPathPatternsCondition().getPatterns();
|
||||||
|
Set<RequestMethod> methods = info.getMethodsCondition().getMethods();
|
||||||
|
Optional<Operation> desc = Optional.ofNullable(func.getMethod().getAnnotation(Operation.class));
|
||||||
|
for (PathPattern url : patterns) {
|
||||||
|
for (RequestMethod method : methods) {
|
||||||
|
apiInfos.add(new ApiInfo(url.getPatternString(), method, desc.map(Operation::summary).orElse(null)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return apiInfos;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initResources() {
|
||||||
|
List<SysResource> resources = systemConv.ApiInfosDto2Pojo(getSystemApis());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
109
src/main/java/com/dlp/admin/service/UserServ.java
Normal file
109
src/main/java/com/dlp/admin/service/UserServ.java
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
package com.dlp.admin.service;
|
||||||
|
|
||||||
|
import com.dlp.admin.entity.converter.UserConv;
|
||||||
|
import com.dlp.admin.entity.dto.user.CustomUserDetails;
|
||||||
|
import com.dlp.admin.entity.dto.user.TokenClaims;
|
||||||
|
import com.dlp.admin.entity.enums.UserStatusEnum;
|
||||||
|
import com.dlp.admin.entity.exception.OrgNotExistExcp;
|
||||||
|
import com.dlp.admin.entity.exception.UserDisabledExcp;
|
||||||
|
import com.dlp.admin.entity.exception.UsernameOrPasswordExcp;
|
||||||
|
import com.dlp.admin.entity.pojo.user.SysOrg;
|
||||||
|
import com.dlp.admin.entity.pojo.user.SysUser;
|
||||||
|
import com.dlp.admin.entity.req.user.LoginReq;
|
||||||
|
import com.dlp.admin.entity.resp.user.LoginResp;
|
||||||
|
import com.dlp.admin.repository.user.SysUserRepo;
|
||||||
|
import com.dlp.admin.util.AuthUtil;
|
||||||
|
import com.dlp.admin.util.JwtUtil;
|
||||||
|
import com.dlp.admin.util.RedisUtil;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||||
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class UserServ implements UserDetailsService {
|
||||||
|
|
||||||
|
private final UserConv userConv;
|
||||||
|
private final SysUserRepo userRepo;
|
||||||
|
|
||||||
|
public static final String REDIS_USER_DETAILS = "user_details_%s";
|
||||||
|
public static final long REDIS_USER_DETAILS_EXPIRE = 1 * 60 * 60;
|
||||||
|
private final RedisUtil redisUtil;
|
||||||
|
private final PasswordEncoder passwordEncoder;
|
||||||
|
private final AuthUtil authUtil;
|
||||||
|
|
||||||
|
public SysUser getUserByUsername(String username) {
|
||||||
|
Optional<SysUser> user = userRepo.findByUsername(username);
|
||||||
|
if (user.isEmpty()) {
|
||||||
|
throw new UsernameNotFoundException("用户不存在");
|
||||||
|
}
|
||||||
|
return user.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public LoginResp login(LoginReq req) {
|
||||||
|
Optional<SysUser> userOpt = userRepo.findByUsername(req.getUsername());
|
||||||
|
SysUser user = validUserAvailable(userOpt);
|
||||||
|
validPassword(req.getPassword(), user.getPassword());
|
||||||
|
TokenClaims claims = userConv.ClaimsPojo2Dto(user);
|
||||||
|
LoginResp resp = new LoginResp();
|
||||||
|
resp.setToken(JwtUtil.generateToken(claims));
|
||||||
|
return resp;
|
||||||
|
}
|
||||||
|
//
|
||||||
|
// public Object listUser() {
|
||||||
|
// Long orgId = authUtil.getUserDetails().getOrgId();
|
||||||
|
// List<Long> subOrgIds = userDao.listOrgWithSubOrg(orgId).stream().map(SysOrg::getId).toList();
|
||||||
|
// return userDao.listUserByOrgIds(subOrgIds);
|
||||||
|
// }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||||
|
// 读缓存
|
||||||
|
Optional<CustomUserDetails> userDetailsOpt = loadUserDetailsFromRedis(username);
|
||||||
|
if (userDetailsOpt.isPresent()) {
|
||||||
|
return userDetailsOpt.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读数据库
|
||||||
|
Optional<SysUser> userOpt = userRepo.findByUsername(username);
|
||||||
|
SysUser user = validUserAvailable(userOpt);
|
||||||
|
CustomUserDetails userDetails = userConv.UserDetailsPojo2Dto(user);
|
||||||
|
|
||||||
|
// 写缓存
|
||||||
|
redisUtil.setex(RedisUtil.parseKey(REDIS_USER_DETAILS, username), userDetails, REDIS_USER_DETAILS_EXPIRE);
|
||||||
|
return userDetails;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Optional<CustomUserDetails> loadUserDetailsFromRedis(String username) {
|
||||||
|
return redisUtil.get(RedisUtil.parseKey(REDIS_USER_DETAILS, username), CustomUserDetails.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void validPassword(String rawPassword, String encodedPassword) {
|
||||||
|
if (!passwordEncoder.matches(rawPassword, encodedPassword)) {
|
||||||
|
throw new UsernameOrPasswordExcp();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private SysUser validUserAvailable(Optional<SysUser> userOpt) {
|
||||||
|
if (userOpt.isEmpty()) {
|
||||||
|
throw new UsernameOrPasswordExcp();
|
||||||
|
}
|
||||||
|
if (userOpt.get().getStatus() == UserStatusEnum.DISABLED) {
|
||||||
|
throw new UserDisabledExcp();
|
||||||
|
}
|
||||||
|
return userOpt.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
private SysOrg validOrgAvailable(Optional<SysOrg> orgOpt) {
|
||||||
|
if (orgOpt.isEmpty()) {
|
||||||
|
throw new OrgNotExistExcp();
|
||||||
|
}
|
||||||
|
return orgOpt.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
12
src/main/java/com/dlp/admin/util/AuthUtil.java
Normal file
12
src/main/java/com/dlp/admin/util/AuthUtil.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package com.dlp.admin.util;
|
||||||
|
|
||||||
|
import com.dlp.admin.entity.dto.user.CustomUserDetails;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class AuthUtil {
|
||||||
|
public CustomUserDetails getUserDetails() {
|
||||||
|
return (CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getDetails();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.rainbus.dlp.util;
|
package com.dlp.admin.util;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.databind.type.MapType;
|
import com.fasterxml.jackson.databind.type.MapType;
|
||||||
|
|
||||||
@@ -8,7 +9,7 @@ import java.util.Map;
|
|||||||
|
|
||||||
public class ConvertUtil {
|
public class ConvertUtil {
|
||||||
|
|
||||||
private static final ObjectMapper mapper = new ObjectMapper();
|
private static final ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();
|
||||||
|
|
||||||
public static <T, V> Map<String, V> toMap(T source, Class<V> vClass) {
|
public static <T, V> Map<String, V> toMap(T source, Class<V> vClass) {
|
||||||
MapType mapType = mapper.getTypeFactory().constructMapType(HashMap.class, String.class, vClass);
|
MapType mapType = mapper.getTypeFactory().constructMapType(HashMap.class, String.class, vClass);
|
||||||
@@ -19,4 +20,11 @@ public class ConvertUtil {
|
|||||||
return mapper.convertValue(map, tClass);
|
return mapper.convertValue(map, tClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <T> T fromJsonStr(String jsonStr, Class<T> tClass) throws JsonProcessingException {
|
||||||
|
return mapper.readValue(jsonStr, tClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <V> String toJsonStr(V value) throws JsonProcessingException {
|
||||||
|
return mapper.writeValueAsString(value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.rainbus.dlp.util;
|
package com.dlp.admin.util;
|
||||||
|
|
||||||
import com.auth0.jwt.JWT;
|
import com.auth0.jwt.JWT;
|
||||||
import com.auth0.jwt.JWTCreator;
|
import com.auth0.jwt.JWTCreator;
|
||||||
@@ -6,7 +6,7 @@ import com.auth0.jwt.JWTVerifier;
|
|||||||
import com.auth0.jwt.algorithms.Algorithm;
|
import com.auth0.jwt.algorithms.Algorithm;
|
||||||
import com.auth0.jwt.exceptions.JWTVerificationException;
|
import com.auth0.jwt.exceptions.JWTVerificationException;
|
||||||
import com.auth0.jwt.interfaces.DecodedJWT;
|
import com.auth0.jwt.interfaces.DecodedJWT;
|
||||||
import com.rainbus.dlp.entity.dto.user.TokenClaims;
|
import com.dlp.admin.entity.dto.user.TokenClaims;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
|
||||||
67
src/main/java/com/dlp/admin/util/RedisUtil.java
Normal file
67
src/main/java/com/dlp/admin/util/RedisUtil.java
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
package com.dlp.admin.util;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class RedisUtil {
|
||||||
|
|
||||||
|
private final RedisTemplate<String, String> redisTemplate;
|
||||||
|
|
||||||
|
public boolean set(String key, String value) {
|
||||||
|
redisTemplate.opsForValue().set(key, value);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <V> boolean set(String key, V value) {
|
||||||
|
try {
|
||||||
|
set(key, ConvertUtil.toJsonStr(value));
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<String> get(String key) {
|
||||||
|
return Optional.ofNullable(redisTemplate.opsForValue().get(key));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <V> Optional<V> get(String key, Class<V> vClass) {
|
||||||
|
return get(key).map(s -> {
|
||||||
|
try {
|
||||||
|
return ConvertUtil.fromJsonStr(s, vClass);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delete(String key) {
|
||||||
|
redisTemplate.delete(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean setex(String key, String value, long timeout) {
|
||||||
|
redisTemplate.opsForValue().set(key, value, Duration.ofSeconds(timeout));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <V> boolean setex(String key, V value, long timeout) {
|
||||||
|
try {
|
||||||
|
setex(key, ConvertUtil.toJsonStr(value), timeout);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String parseKey(String pattern, Object... args) {
|
||||||
|
return String.format(pattern, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
package com.rainbus.dlp.controller;
|
|
||||||
|
|
||||||
import com.rainbus.dlp.entity.dto.user.TokenClaims;
|
|
||||||
import com.rainbus.dlp.entity.mapper.UserConv;
|
|
||||||
import com.rainbus.dlp.entity.pojo.user.SysUser;
|
|
||||||
import com.rainbus.dlp.entity.req.user.RegisterReq;
|
|
||||||
import com.rainbus.dlp.entity.resp.Resp;
|
|
||||||
import com.rainbus.dlp.repository.user.SysUserRepo;
|
|
||||||
import com.rainbus.dlp.util.JwtUtil;
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
||||||
import org.springframework.security.core.Authentication;
|
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@RequestMapping("/user")
|
|
||||||
@AllArgsConstructor
|
|
||||||
public class UserCtrl {
|
|
||||||
|
|
||||||
private final SysUserRepo userRepo;
|
|
||||||
private final UserConv userConv;
|
|
||||||
private final AuthenticationManager authenticationManager;
|
|
||||||
private final PasswordEncoder passwordEncoder;
|
|
||||||
|
|
||||||
@PostMapping("/register")
|
|
||||||
public Resp<Object> register(@RequestBody RegisterReq req) {
|
|
||||||
req.setPassword(passwordEncoder.encode(req.getPassword()));
|
|
||||||
userRepo.save(userConv.RegisterReq2Pojo(req));
|
|
||||||
return Resp.success();
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping("/login")
|
|
||||||
public Resp<Object> login(@RequestBody RegisterReq req) {
|
|
||||||
SysUser user = userRepo.findByUsername(req.getUsername()).orElse(null);
|
|
||||||
Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(req.getUsername(), req.getPassword()));
|
|
||||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
|
||||||
if (user == null) {
|
|
||||||
return Resp.fail("user not exist");
|
|
||||||
}
|
|
||||||
if (!passwordEncoder.matches(req.getPassword(), user.getPassword())) {
|
|
||||||
return Resp.fail("password error");
|
|
||||||
}
|
|
||||||
return Resp.success(JwtUtil.generateToken(userConv.ClaimsPojo2Dto(user)));
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("token")
|
|
||||||
public Resp<Object> token() {
|
|
||||||
TokenClaims claims = new TokenClaims();
|
|
||||||
claims.setUsername("rainbus");
|
|
||||||
claims.setId(1L);
|
|
||||||
claims.setRoles(List.of("USER"));
|
|
||||||
return Resp.success(JwtUtil.generateToken(claims));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
package com.rainbus.dlp.entity.mapper;
|
|
||||||
|
|
||||||
import com.rainbus.dlp.entity.dto.user.TokenClaims;
|
|
||||||
import com.rainbus.dlp.entity.pojo.user.SysUser;
|
|
||||||
import com.rainbus.dlp.entity.req.user.RegisterReq;
|
|
||||||
import org.mapstruct.InheritInverseConfiguration;
|
|
||||||
import org.mapstruct.Mapper;
|
|
||||||
import org.mapstruct.Mapping;
|
|
||||||
|
|
||||||
@Mapper(componentModel = "spring")
|
|
||||||
public interface UserConv {
|
|
||||||
|
|
||||||
@InheritInverseConfiguration
|
|
||||||
SysUser RegisterReq2Pojo(RegisterReq req);
|
|
||||||
|
|
||||||
@Mapping(target = "roles", expression = "java(user.getRoles().stream().map(com.rainbus.dlp.entity.pojo.user.SysRole::getRole).toList())")
|
|
||||||
TokenClaims ClaimsPojo2Dto(SysUser user);
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
package com.rainbus.dlp.entity.pojo.user;
|
|
||||||
|
|
||||||
import com.rainbus.dlp.entity.enums.RoleEnum;
|
|
||||||
import jakarta.persistence.*;
|
|
||||||
import lombok.Data;
|
|
||||||
import org.hibernate.annotations.Comment;
|
|
||||||
import org.springframework.data.annotation.CreatedBy;
|
|
||||||
import org.springframework.data.annotation.CreatedDate;
|
|
||||||
import org.springframework.data.annotation.LastModifiedBy;
|
|
||||||
import org.springframework.data.annotation.LastModifiedDate;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@Entity
|
|
||||||
public class SysRole {
|
|
||||||
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
|
||||||
@Comment("id")
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
@Comment("角色")
|
|
||||||
private String role;
|
|
||||||
|
|
||||||
@Column
|
|
||||||
@Comment("描述")
|
|
||||||
private String description;
|
|
||||||
|
|
||||||
@OneToMany(fetch = FetchType.EAGER)
|
|
||||||
private List<SysResource> resources;
|
|
||||||
|
|
||||||
@CreatedBy
|
|
||||||
@Comment("创建人")
|
|
||||||
private Long createdBy;
|
|
||||||
|
|
||||||
@LastModifiedBy
|
|
||||||
@Comment("更新人")
|
|
||||||
private Long updatedBy;
|
|
||||||
|
|
||||||
@CreatedDate
|
|
||||||
@Comment("创建时间")
|
|
||||||
private Long createdAt;
|
|
||||||
|
|
||||||
@LastModifiedDate
|
|
||||||
@Comment("更新时间")
|
|
||||||
private Long updatedAt;
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
package com.rainbus.dlp.entity.pojo.user;
|
|
||||||
|
|
||||||
import com.rainbus.dlp.entity.enums.RoleEnum;
|
|
||||||
import jakarta.persistence.*;
|
|
||||||
import lombok.Data;
|
|
||||||
import org.springframework.data.annotation.CreatedBy;
|
|
||||||
import org.springframework.data.annotation.CreatedDate;
|
|
||||||
import org.springframework.data.annotation.LastModifiedBy;
|
|
||||||
import org.springframework.data.annotation.LastModifiedDate;
|
|
||||||
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
|
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@Entity
|
|
||||||
//@SQLRestriction("is_deleted = 0")
|
|
||||||
@EntityListeners(value = AuditingEntityListener.class)
|
|
||||||
//@SQLDelete(sql = "update `user` set is_deleted = 1 where id = ?")
|
|
||||||
public class SysUser {
|
|
||||||
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
@Column
|
|
||||||
private String username;
|
|
||||||
|
|
||||||
@Column
|
|
||||||
private String password;
|
|
||||||
|
|
||||||
@OneToMany(fetch = FetchType.EAGER)
|
|
||||||
private List<SysRole> roles;
|
|
||||||
|
|
||||||
@CreatedBy
|
|
||||||
private Long createBy;
|
|
||||||
|
|
||||||
@LastModifiedBy
|
|
||||||
private Long updateBy;
|
|
||||||
|
|
||||||
@CreatedDate
|
|
||||||
private LocalDateTime createAt;
|
|
||||||
|
|
||||||
@LastModifiedDate
|
|
||||||
private LocalDateTime updateAt;
|
|
||||||
|
|
||||||
@Column
|
|
||||||
private Integer isDeleted;
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
package com.rainbus.dlp.entity.req.user;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
public class RegisterReq {
|
|
||||||
|
|
||||||
private String username;
|
|
||||||
|
|
||||||
private String password;
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
package com.rainbus.dlp.service;
|
|
||||||
|
|
||||||
import com.rainbus.dlp.entity.dto.user.CustomUserDetails;
|
|
||||||
import com.rainbus.dlp.entity.pojo.user.SysRole;
|
|
||||||
import com.rainbus.dlp.entity.pojo.user.SysUser;
|
|
||||||
import com.rainbus.dlp.repository.user.SysResourceRepo;
|
|
||||||
import com.rainbus.dlp.repository.user.SysRoleRepo;
|
|
||||||
import com.rainbus.dlp.repository.user.SysUserRepo;
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import org.springframework.security.core.GrantedAuthority;
|
|
||||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
|
||||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
|
||||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.validation.SimpleErrors;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
|
|
||||||
@Service
|
|
||||||
@AllArgsConstructor
|
|
||||||
public class UserServ implements UserDetailsService {
|
|
||||||
|
|
||||||
private final SysUserRepo userRepo;
|
|
||||||
private final SysRoleRepo roleRepo;
|
|
||||||
private final SysResourceRepo resourceRepo;
|
|
||||||
|
|
||||||
public SysUser getUserByUsername(String username) {
|
|
||||||
return userRepo.findByUsername(username).orElse(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<SysRole> getAllRoles() {
|
|
||||||
return roleRepo.findAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
|
||||||
SysUser user = userRepo.findByUsername(username).orElseThrow(() -> new UsernameNotFoundException("user not exist"));
|
|
||||||
List<SimpleGrantedAuthority> authorities = user.getRoles().stream().map(role -> new SimpleGrantedAuthority(role.getRole())).toList();
|
|
||||||
return new CustomUserDetails(user.getUsername(), user.getPassword(), authorities);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -11,6 +11,10 @@ spring:
|
|||||||
hibernate:
|
hibernate:
|
||||||
ddl-auto: update
|
ddl-auto: update
|
||||||
show-sql: true
|
show-sql: true
|
||||||
|
# properties:
|
||||||
|
# hibernate:
|
||||||
|
# enable_lazy_load_no_trans: true
|
||||||
|
|
||||||
|
|
||||||
data:
|
data:
|
||||||
redis:
|
redis:
|
||||||
@@ -18,4 +22,4 @@ spring:
|
|||||||
port: 6379
|
port: 6379
|
||||||
password: 13291004986
|
password: 13291004986
|
||||||
timeout: 10000
|
timeout: 10000
|
||||||
database: 0
|
database: 0
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.rainbus.dlp;
|
package com.dlp.admin;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
Reference in New Issue
Block a user