generated from youfool-project/youfool-prj-springboot-template
修正AI生成的代码
This commit is contained in:
parent
29794e033e
commit
683a82a409
61
pom.xml
61
pom.xml
|
|
@ -7,7 +7,7 @@
|
|||
<groupId>com.chinaweal.youfool</groupId>
|
||||
<artifactId>youfool-course</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
<packaging>jar</packaging>
|
||||
<name>${project.artifactId}</name>
|
||||
<url>https://www.chinaweal.com.cn</url>
|
||||
<description>youfool课程</description>
|
||||
|
|
@ -58,20 +58,65 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
<!-- Web相关依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.14.1</version> <!-- 适配 JDK 17 的版本 -->
|
||||
<configuration>
|
||||
<!-- 显式指定编译器参数,保留方法参数名 -->
|
||||
<compilerArgs>
|
||||
<arg>-parameters</arg>
|
||||
</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<!-- 配置 Spring Boot 打包插件 -->
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<configuration>
|
||||
<!-- 不打包依赖 -->
|
||||
<layout>ZIP</layout>
|
||||
<includes>
|
||||
<include>
|
||||
<groupId>nothing</groupId>
|
||||
<artifactId>nothing</artifactId>
|
||||
</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<!-- 分离依赖 -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-dependencies</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>copy-dependencies</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<!-- 依赖输出目录 -->
|
||||
<outputDirectory>${project.build.directory}/libs</outputDirectory>
|
||||
<!-- 排除传递依赖中的可选依赖 -->
|
||||
<excludeGroupIds>org.projectlombok</excludeGroupIds>
|
||||
<!-- <excludeOptional>true</excludeOptional>-->
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<!-- 代码风格检测插件 checkstyle -->
|
||||
<plugin>
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ public class SpringMvcConfig implements WebMvcConfigurer {
|
|||
// SaRouter.match("/notice/**", r -> StpUtil.checkPermission("notice"));
|
||||
// SaRouter.match("/comment/**", r -> StpUtil.checkPermission("comment"));
|
||||
// 指定一条 match 规则
|
||||
SaRouter.notMatch("/login**","/oauth2**","/user/auth/**", "/test/**", "/doc.html**", "/cross/**", "/swagger*",
|
||||
SaRouter.notMatch("/login**","/oauth2/**","/user/auth/**", "/test/**", "/doc.html**", "/cross/**", "/swagger*",
|
||||
"/cms/index.html", "/cms/static/**", "/cms/favicon.ico", "/cms/user/login", "/network/ping", "/error", "/static/data/**")
|
||||
.match("/**", r -> StpUtil.checkLogin());
|
||||
})).addPathPatterns("/**");
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.chinaweal.youfool.course.controller;
|
|||
|
||||
import cn.dev33.satoken.session.SaSession;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.chinaweal.youfool.course.entity.SysUser;
|
||||
import com.chinaweal.youfool.course.entity.UserBaseExtend;
|
||||
import com.chinaweal.youfool.course.service.SysUserService;
|
||||
|
|
@ -91,10 +92,11 @@ public class OAuth2Controller {
|
|||
}
|
||||
|
||||
String accessToken = (String) tokenResponse.get("access_token");
|
||||
log.info("获取access_token成功: {}", accessToken);
|
||||
|
||||
// 获取用户信息
|
||||
Map<String, Object> userInfo = getUserInfo(accessToken);
|
||||
|
||||
log.info("登录用户信息:{}", JSONObject.toJSONString(userInfo));
|
||||
// 处理用户登录
|
||||
handleUserLogin(userInfo);
|
||||
|
||||
|
|
@ -118,7 +120,7 @@ public class OAuth2Controller {
|
|||
headers.put("Authorization", "token " + accessToken);
|
||||
|
||||
org.springframework.http.HttpEntity<?> entity = new org.springframework.http.HttpEntity<>(headers);
|
||||
Map<String, Object> userInfo = restTemplate.exchange(userUrl,
|
||||
Map<String, Object> userInfo = restTemplate.exchange(userUrl + "?access_token=" + accessToken,
|
||||
org.springframework.http.HttpMethod.GET, entity, Map.class).getBody();
|
||||
|
||||
return userInfo;
|
||||
|
|
@ -130,31 +132,33 @@ public class OAuth2Controller {
|
|||
* @param userInfo 用户信息
|
||||
*/
|
||||
private void handleUserLogin(Map<String, Object> userInfo) {
|
||||
String username = (String) userInfo.get("username");
|
||||
String username = (String) userInfo.get("preferred_username");
|
||||
String name = (String) userInfo.get("name");
|
||||
String email = (String) userInfo.get("email");
|
||||
String avatarUrl = (String) userInfo.get("avatar_url");
|
||||
Integer id = (Integer) userInfo.get("id");
|
||||
String avatarUrl = (String) userInfo.get("picture");
|
||||
String id = (String) userInfo.get("sub");
|
||||
|
||||
log.info("Gitea用户登录成功: username={}, email={}, id={}", username, email, id);
|
||||
|
||||
// 生成Gitea Open ID
|
||||
String giteaOpenId = String.valueOf(id);
|
||||
|
||||
// 检查用户是否已存在
|
||||
SysUser sysUser = sysUserService.getUserByGiteaOpenId(giteaOpenId);
|
||||
SysUser sysUser = sysUserService.getUserByGiteaOpenId(id);
|
||||
|
||||
// 如果用户不存在,则创建新用户
|
||||
if (sysUser == null) {
|
||||
sysUser = new SysUser();
|
||||
sysUser.setUsername(username);
|
||||
sysUser.setNickname(username);
|
||||
sysUser.setNickname(name);
|
||||
sysUser.setName(name);
|
||||
sysUser.setEmail(email);
|
||||
sysUser.setAvatar(avatarUrl);
|
||||
sysUser.setGiteaOpenId(giteaOpenId);
|
||||
sysUser.setGiteaOpenId(id);
|
||||
sysUserService.save(sysUser);
|
||||
} else {
|
||||
// 更新用户信息
|
||||
sysUser.setEmail(email);
|
||||
sysUser.setName(name);
|
||||
sysUser.setAvatar(avatarUrl);
|
||||
sysUserService.updateById(sysUser);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,12 +9,12 @@ spring:
|
|||
dynamic:
|
||||
datasource:
|
||||
master:
|
||||
url: jdbc:postgresql://172.22.80.157:5432/pms
|
||||
url: jdbc:postgresql://172.22.80.157:5432/course
|
||||
username: pms_dev
|
||||
password: 123456
|
||||
driver-class-name: org.postgresql.Driver # 3.2.0开始支持SPI可省略此配置
|
||||
youfool:
|
||||
url: jdbc:postgresql://172.22.80.157:5432/pms
|
||||
url: jdbc:postgresql://172.22.80.157:5432/course
|
||||
username: pms_dev
|
||||
password: 123456
|
||||
driver-class-name: org.postgresql.Driver
|
||||
|
|
@ -9,12 +9,12 @@ spring:
|
|||
dynamic:
|
||||
datasource:
|
||||
master:
|
||||
url: jdbc:postgresql://172.22.80.157:5432/pms
|
||||
url: jdbc:postgresql://172.22.80.157:5432/course
|
||||
username: pms_dev
|
||||
password: 123456
|
||||
driver-class-name: org.postgresql.Driver # 3.2.0开始支持SPI可省略此配置
|
||||
youfool:
|
||||
url: jdbc:postgresql://172.22.80.157:5432/pms
|
||||
url: jdbc:postgresql://172.22.80.157:5432/course
|
||||
username: pms_dev
|
||||
password: 123456
|
||||
driver-class-name: org.postgresql.Driver
|
||||
|
|
@ -4,9 +4,9 @@ logging:
|
|||
filePath: ${LOG_PATH:../logs}
|
||||
spring:
|
||||
profiles:
|
||||
active: dev
|
||||
active: ${COURSE_ACTIVE:dev}
|
||||
application:
|
||||
name: youfool-lesson
|
||||
name: youfool-course
|
||||
# Thymeleaf配置
|
||||
thymeleaf:
|
||||
cache: false
|
||||
|
|
@ -76,7 +76,9 @@ knife4j:
|
|||
|
||||
server:
|
||||
servlet:
|
||||
context-path: /course
|
||||
context-path: ${COURSE_CONTEXT_PATH:/course}
|
||||
port: ${COURSE_PORT:8080}
|
||||
|
||||
# Sa-Token配置
|
||||
sa-token:
|
||||
# token名称 (同时也是cookie名称)
|
||||
|
|
@ -95,9 +97,10 @@ sa-token:
|
|||
is-log: false
|
||||
# Gitea OAuth2配置
|
||||
gitea:
|
||||
client-id: ${GITEA_CLIENT_ID:your-gitea-client-id}
|
||||
client-secret: ${GITEA_CLIENT_SECRET:your-gitea-client-secret}
|
||||
auth-url: ${GITEA_AUTH_URL:https://gitea.com/login/oauth/authorize}
|
||||
token-url: ${GITEA_TOKEN_URL:https://gitea.com/login/oauth/access_token}
|
||||
user-url: ${GITEA_USER_URL:https://gitea.com/api/v1/user}
|
||||
redirect-uri: ${BASE_URL:http://localhost:8080}/course/oauth2/gitea/callback
|
||||
client-id: ${GITEA_CLIENT_ID:fc3fb72c-ed76-4712-99d9-0c3b5d437117}
|
||||
client-secret: ${GITEA_CLIENT_SECRET:gto_i5hvz65jbrsv6mdwfu2t4siz3zrxfairg2cx454g3z6a5qy5t5na}
|
||||
auth-url: ${GITEA_AUTH_URL:http://47.107.61.133:3000/login/oauth/authorize}
|
||||
token-url: ${GITEA_TOKEN_URL:http://47.107.61.133:3000/login/oauth/access_token}
|
||||
user-url: ${GITEA_USER_URL:http://47.107.61.133:3000/login/oauth/userinfo}
|
||||
redirect-uri: ${BASE_URL:http://localhost:8080}/course/oauth2/gitea/callback
|
||||
token: ${GITEA_TOKEN:}
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
package com.chinaweal.youfool.course;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
//@RunWith(SpringRunner.class)
|
||||
//@SpringBootTest(classes = {YoufoolPmsApplication.class})
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test1() {
|
||||
String no = String.format("%04d", 51);
|
||||
System.out.println(no);
|
||||
System.out.println("12345678");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test2() {
|
||||
List<Map<String, String>> params = new ArrayList<>();
|
||||
Map<String, String> map = new LinkedHashMap<>();
|
||||
map.put("测试", "askdjf;kl ");
|
||||
params.add(map);
|
||||
String str = String.format("测试,%s", LocalDate.now());
|
||||
System.out.println(str);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,57 +0,0 @@
|
|||
package com.chinaweal.youfool.course.entity;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* SysUser密码加密测试类
|
||||
*
|
||||
* @author test
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class SysUserTest {
|
||||
|
||||
@Test
|
||||
public void testEncryptPassword() {
|
||||
// 测试密码加密功能
|
||||
String originalPassword = "test123";
|
||||
String encryptedPassword = SysUser.encryptPassword(originalPassword);
|
||||
|
||||
// 验证加密结果不为空
|
||||
assertNotNull(encryptedPassword);
|
||||
// 验证加密结果是32位MD5哈希值
|
||||
assertEquals(32, encryptedPassword.length());
|
||||
// 验证相同密码加密结果一致
|
||||
String encryptedPassword2 = SysUser.encryptPassword(originalPassword);
|
||||
assertEquals(encryptedPassword, encryptedPassword2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVerifyPassword() {
|
||||
// 测试密码验证功能
|
||||
String originalPassword = "test123";
|
||||
String encryptedPassword = SysUser.encryptPassword(originalPassword);
|
||||
|
||||
// 验证正确密码
|
||||
assertTrue(SysUser.verifyPassword(originalPassword, encryptedPassword));
|
||||
|
||||
// 验证错误密码
|
||||
assertFalse(SysUser.verifyPassword("wrongPassword", encryptedPassword));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPasswordEncryptionWithExample() {
|
||||
// 测试具体示例:密码"admin"的加密结果
|
||||
String password = "admin";
|
||||
String encrypted = SysUser.encryptPassword(password);
|
||||
|
||||
System.out.println("密码 '" + password + "' 的加密结果: " + encrypted);
|
||||
|
||||
// 验证加密结果
|
||||
assertNotNull(encrypted);
|
||||
assertEquals(32, encrypted.length());
|
||||
|
||||
// 验证能够正确验证
|
||||
assertTrue(SysUser.verifyPassword(password, encrypted));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue