Skip to content
Merged

Dev #314

Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c47b5f5
Refactor/replace deprecated usage (#236)
colorful3 Jul 6, 2021
8359baf
Merge branch 'dev' of https://github.com/TaleLin/lin-cms-spring-boot …
colorful3 Jul 6, 2021
4ee1a90
chore(pom.xml): 升级框架版本为0.2.1-RELEASE
colorful3 Jul 10, 2021
a20d14a
Merge branch 'chore/upgrade-version' into dev
colorful3 Jul 10, 2021
cd87258
fix #223 & test去除deprecated api等问题修复
gadfly3173 Jul 14, 2021
1666240
fix: #247
towelong Nov 14, 2021
7af27b3
fix: 反代后IP获取错误
gadfly3173 Nov 15, 2021
e3a5703
feat: 登录验证码
gadfly3173 Nov 19, 2021
830d05e
fix: 无用字段
gadfly3173 Nov 19, 2021
9cec353
fix: 不开启验证码时要求header的错误
gadfly3173 Nov 19, 2021
5d6d9af
fix: sout clean
gadfly3173 Nov 19, 2021
6f81033
fix: aes192 to 256
gadfly3173 Nov 19, 2021
c99f73d
fix: 修复错误修改的配置
gadfly3173 Nov 19, 2021
44f8460
fix: 增加aes配置校验,避免输入参数不合法
gadfly3173 Nov 19, 2021
a83c656
fix: 部分情况下fileMap.size()不可靠&校验信息不需要${}
gadfly3173 Nov 22, 2021
19c21d8
fix: 属性前缀
gadfly3173 Nov 22, 2021
838ca9c
fix: fix #264
gadfly3173 Dec 9, 2021
5ce8064
chore(application-dev.yml): 默认关闭验证码
colorful3 Aug 13, 2022
92f511d
refactor: 数据库新增 is_deleted 字段,作为删除标识
colorful3 Sep 25, 2022
14d581b
merge: 解决代码冲突
colorful3 Nov 20, 2022
c08253f
refactor: non null copy (#300)
gadfly3173 Nov 20, 2022
b78eac3
Refactor/code specification (#312)
colorful3 Nov 28, 2022
e1c97cf
refactor: 整理代码规范
colorful3 Nov 29, 2022
f9edcd8
Merge branch 'master' into dev
colorful3 Nov 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/**
* @author pedro@TaleLin
* 启动入口类
*/
@RestController
@MapperScan(basePackages = {"io.github.talelin.latticy.mapper"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @author pedro@TaleLin
* @author Juzi@TaleLin
* @author colorful@TaleLin
* 分组权限业务对象
*/
@Data
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/**
* @author Gadfly
* @since 2021-11-19 15:20
* 登录验证码业务对象
*/
@Data
@NoArgsConstructor
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/io/github/talelin/latticy/common/LocalUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@
import io.github.talelin.latticy.model.UserDO;

/**
* 线程安全的当前登录用户,如果用户为登录,则得到 null
* 线程安全的当前登录用户,如果用户未登录,则得到 null
*
* @author pedro@TaleLin
*/
public class LocalUser {

private static ThreadLocal<UserDO> local = new ThreadLocal<>();
private LocalUser() {
throw new IllegalStateException("Utility class");
}

private static final ThreadLocal<UserDO> LOCAL = new ThreadLocal<>();

/**
* 得到当前登录用户
*
* @return user | null
*/
public static UserDO getLocalUser() {
return LocalUser.local.get();
return LocalUser.LOCAL.get();
}

/**
Expand All @@ -26,17 +30,17 @@ public static UserDO getLocalUser() {
* @param user user
*/
public static void setLocalUser(UserDO user) {
LocalUser.local.set(user);
LocalUser.LOCAL.set(user);
}

public static <T> T getLocalUser(Class<T> clazz) {
return (T) local.get();
return (T) LOCAL.get();
}

/**
* 清理当前用户
*/
public static void clearLocalUser() {
LocalUser.local.remove();
LOCAL.remove();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.springframework.util.StringUtils;

/**
* 处理返回结果为 UnifyResponseVO 的 Controller
* 处理返回结果为 UnifyResponseVO 的控制器层方法
* message 默认为 null,在此处通过 code 设置为对应消息
*
* @author pedro@TaleLin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

/**
* @author pedro@TaleLin
*
* 消息码配置类
*/
@SuppressWarnings("ConfigurationProperties")
@Component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
/**
* @author pedro@TaleLin
* @author colorful@TaleLin
*
* 公共配置
*/
@Configuration(proxyBeanMethods = false)
public class CommonConfiguration {
Expand All @@ -29,8 +31,6 @@ public RequestLogInterceptor requestLogInterceptor() {

/**
* 新的分页插件,一缓和二缓遵循mybatis的规则
* 需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
* 参考链接:https://mp.baomidou.com/guide/interceptor.html
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
Expand All @@ -39,9 +39,6 @@ public MybatisPlusInterceptor mybatisPlusInterceptor() {
return interceptor;
}

/**
* 参考链接:https://mp.baomidou.com/guide/interceptor.html
*/
@Bean
@SuppressWarnings("deprecation")
public ConfigurationCustomizer configurationCustomizer() {
Expand Down Expand Up @@ -70,7 +67,6 @@ public PermissionMetaCollector postProcessBeans() {
@Bean
public Jackson2ObjectMapperBuilderCustomizer customJackson() {
return jacksonObjectMapperBuilder -> {
// jacksonObjectMapperBuilder.serializationInclusion(JsonInclude.Include.NON_NULL);
jacksonObjectMapperBuilder.failOnUnknownProperties(false);
jacksonObjectMapperBuilder.propertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

/**
* @author Gadfly
*
* A customizing Servlet-specific ModelAttributeMethodProcessor that applies data binding through
* a WebDataBinder of type CustomServletModelAttributeMethodProcessor.
*/

public class CustomServletModelAttributeMethodProcessor extends ServletModelAttributeMethodProcessor {

public CustomServletModelAttributeMethodProcessor(final boolean annotationNotRequired) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

/**
* @author Gadfly
*
* 自定义servlet请求参数绑定类
*/

public class CustomServletRequestDataBinder extends ServletRequestDataBinder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

/**
* @author Gadfly
*
* 登录图形验证码配置类
*
*/
@Slf4j
@Getter
Expand All @@ -20,21 +23,23 @@ public class LoginCaptchaProperties {
/**
* aes 密钥
*/
private String secret;
private String secret = CaptchaUtil.getRandomString(32);
/**
* aes 偏移量
*/
private String iv;
private String iv = CaptchaUtil.getRandomString(16);
/**
* 启用验证码
*/
private Boolean enabled = Boolean.FALSE;

public void setSecret(String secret) {
this.secret = CaptchaUtil.getRandomString(32);
final long ivLen1 = 16;
final long ivLen2 = 24;
final long ivLen3 = 32;
if (StringUtils.hasText(secret)) {
byte[] bytes = secret.getBytes();
if (bytes.length == 16 || bytes.length == 24 || bytes.length == 32) {
if (bytes.length == ivLen1 || bytes.length == ivLen2 || bytes.length == ivLen3) {
this.secret = secret;
} else {
log.warn("AES密钥必须为128/192/256bit,输入的密钥为{}bit,已启用随机密钥{}", bytes.length * 8, this.secret);
Expand All @@ -43,10 +48,10 @@ public void setSecret(String secret) {
}

public void setIv(String iv) {
this.iv = CaptchaUtil.getRandomString(16);
final long ivLen = 16;
if (StringUtils.hasText(iv)) {
byte[] bytes = iv.getBytes();
if (bytes.length == 16) {
if (bytes.length == ivLen) {
this.iv = iv;
} else {
log.warn("AES初始向量必须为128bit,输入的密钥为{}bit,已启用随机向量{}", bytes.length * 8, this.iv);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ public class IdentityConstant {
*/
public static final String USERNAME_PASSWORD_IDENTITY = "USERNAME_PASSWORD";

private IdentityConstant() {
throw new IllegalStateException("Utility class");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
/**
* @author colorful@TaleLin
* @author Juzi@TaleLin
*
* 分组级别枚举类
*/
public enum GroupLevelEnum implements IEnum<Integer> {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
* @author pedro@TaleLin
* @author colorful@TaleLin
* @author Juzi@TaleLin
*
* Restful 接口异常配置类
*/
@Order
@RestControllerAdvice
Expand Down Expand Up @@ -76,7 +78,7 @@ public UnifyResponseVO<Map<String, Object>> processException(BindException excep
HttpServletRequest request,
HttpServletResponse response) {
log.error(exception.toString());
Map<String, Object> msg = new HashMap<>();
Map<String, Object> msg = new HashMap<>(3);
exception.getAllErrors().forEach(error -> {
if (error instanceof FieldError) {
FieldError fieldError = (FieldError) error;
Expand All @@ -98,7 +100,7 @@ public UnifyResponseVO<Map<String, Object>> processException(ConstraintViolation
HttpServletRequest request,
HttpServletResponse response) {
log.error("", exception);
Map<String, Object> msg = new HashMap<>();
Map<String, Object> msg = new HashMap<>(3);
exception.getConstraintViolations().forEach(constraintViolation -> {
String template = constraintViolation.getMessage();
String path = constraintViolation.getPropertyPath().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

/**
* @author Juzi@TaleLin
* YAML配置预加载
*/
public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@
/**
* @author pedro@TaleLin
* @author Juzi@TaleLin
* 鉴权实现类
*/
@Component
public class AuthorizeVerifyResolverImpl implements AuthorizeVerifyResolver {

public final static String AUTHORIZATION_HEADER = "Authorization";
public static final String AUTHORIZATION_HEADER = "Authorization";

public final static String BEARER_PATTERN = "^Bearer$";
public static final String BEARER_PATTERN = "^Bearer$";

@Autowired
private DoubleJWT jwt;
Expand All @@ -58,7 +59,7 @@ public class AuthorizeVerifyResolverImpl implements AuthorizeVerifyResolver {

@Override
public boolean handleLogin(HttpServletRequest request, HttpServletResponse response, MetaInfo meta) {
String tokenStr = verifyHeader(request, response);
String tokenStr = verifyHeader(request);
Map<String, Claim> claims;
try {
claims = jwt.decodeAccessToken(tokenStr);
Expand Down Expand Up @@ -101,7 +102,7 @@ public boolean handleAdmin(HttpServletRequest request, HttpServletResponse respo

@Override
public boolean handleRefresh(HttpServletRequest request, HttpServletResponse response, MetaInfo meta) {
String tokenStr = verifyHeader(request, response);
String tokenStr = verifyHeader(request);
Map<String, Claim> claims;
try {
claims = jwt.decodeRefreshToken(tokenStr);
Expand Down Expand Up @@ -134,9 +135,10 @@ private boolean getClaim(Map<String, Claim> claims) {
throw new NotFoundException(10021);
}
String avatarUrl;
final String protocolPrefix = "http";
if (user.getAvatar() == null) {
avatarUrl = null;
} else if (user.getAvatar().startsWith("http")) {
} else if (user.getAvatar().startsWith(protocolPrefix)) {
avatarUrl = user.getAvatar();
} else {
avatarUrl = domain + servePath.split("/")[0] + "/" + user.getAvatar();
Expand All @@ -155,14 +157,15 @@ private boolean verifyAdmin(UserDO user) {
return groupService.checkIsRootByUserId(user.getId());
}

private String verifyHeader(HttpServletRequest request, HttpServletResponse response) {
private String verifyHeader(HttpServletRequest request) {
// 处理头部header,带有access_token的可以访问
String authorization = request.getHeader(AUTHORIZATION_HEADER);
if (authorization == null || Strings.isBlank(authorization)) {
throw new AuthorizationException(10012);
}
String[] splits = authorization.split(" ");
if (splits.length != 2) {
final int tokenSplitLen = 2;
if (splits.length != tokenSplitLen) {
throw new AuthorizationException(10013);
}
// Bearer 字段
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand All @@ -21,6 +20,7 @@
* @author pedro@TaleLin
* @author Juzi@TaleLin
* @author colorful@TaleLin
* 行为日志实现类
*/
@Slf4j
@Component
Expand All @@ -41,7 +41,7 @@ public void handle(PermissionMeta meta, Logger logger, HttpServletRequest reques
template = this.parseTemplate(template, user, request, response);
String permission = "";
if (meta != null) {
permission = !StringUtils.hasLength(meta.value()) ? meta.value() : meta.value();
permission = meta.value();
}
Integer userId = user.getId();
String username = user.getUsername();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,28 @@
/**
* @author pedro@TaleLin
* @author colorful@TaleLin
* 请求日志拦截器
*/
@Slf4j
public class RequestLogInterceptor implements AsyncHandlerInterceptor {


private ThreadLocal<Long> startTime = new ThreadLocal<>();
private final ThreadLocal<Long> startTime = new ThreadLocal<>();

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
startTime.set(System.currentTimeMillis());
return true;
}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
log.info("[{}] -> [{}] from: {} costs: {}ms",
request.getMethod(),
request.getServletPath(),
IPUtil.getIPFromRequest(request),
System.currentTimeMillis() - startTime.get()
);
startTime.remove();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
/**
* @author pedro@TaleLin
* @author colorful@TaleLin
* 权限监听器
*/
@Component
public class PermissionHandleListener implements ApplicationListener<ContextRefreshedEvent> {
Expand Down
Loading