Skip to content

Commit 5048bd8

Browse files
committed
refactor: 将核心库升级到 0.2.0-RC1 版本
1 parent 7092dfb commit 5048bd8

10 files changed

Lines changed: 45 additions & 42 deletions

File tree

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<groupId>io.github.talelin</groupId>
1313
<artifactId>latticy</artifactId>
14-
<version>0.1.1-RC2</version>
14+
<version>0.2.0-RC1</version>
1515
<name>latticy</name>
1616
<description>Demo project for lin cms</description>
1717

@@ -47,7 +47,7 @@
4747
<dependency>
4848
<groupId>io.github.talelin</groupId>
4949
<artifactId>lin-cms-spring-boot-starter</artifactId>
50-
<version>0.1.1-RC3</version>
50+
<version>0.2.0-RC1</version>
5151
</dependency>
5252

5353
<dependency>

src/main/java/io/github/talelin/latticy/common/exception/RestExceptionHandler.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import cn.hutool.core.util.StrUtil;
44
import io.github.talelin.autoconfigure.bean.Code;
5+
import io.github.talelin.autoconfigure.exception.HttpException;
56
import io.github.talelin.latticy.common.configuration.CodeMessageConfiguration;
67
import io.github.talelin.latticy.vo.UnifyResponseVO;
7-
import io.github.talelin.autoconfigure.exception.HttpException;
88
import lombok.extern.slf4j.Slf4j;
99
import org.springframework.beans.TypeMismatchException;
1010
import org.springframework.beans.factory.annotation.Value;
@@ -26,6 +26,8 @@
2626
import javax.servlet.http.HttpServletRequest;
2727
import javax.servlet.http.HttpServletResponse;
2828
import javax.validation.ConstraintViolationException;
29+
import java.lang.reflect.Constructor;
30+
import java.lang.reflect.InvocationTargetException;
2931
import java.util.HashMap;
3032
import java.util.List;
3133
import java.util.Map;
@@ -49,19 +51,20 @@ public class RestExceptionHandler {
4951
* HttpException
5052
*/
5153
@ExceptionHandler({HttpException.class})
52-
public UnifyResponseVO processException(HttpException exception, HttpServletRequest request, HttpServletResponse response) {
53-
log.error("", exception);
54+
public UnifyResponseVO processException(HttpException exception, HttpServletRequest request, HttpServletResponse response) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
5455
UnifyResponseVO unifyResponse = new UnifyResponseVO();
5556
unifyResponse.setRequest(getSimpleRequest(request));
5657
int code = exception.getCode();
57-
boolean messageOnly = exception.isMessageOnly();
58+
boolean defaultMessage = exception.ifDefaultMessage();
5859
unifyResponse.setCode(code);
5960
response.setStatus(exception.getHttpCode());
6061
String errorMessage = CodeMessageConfiguration.getMessage(code);
61-
if (StrUtil.isBlank(errorMessage) || messageOnly) {
62+
if (StrUtil.isBlank(errorMessage) || !defaultMessage) {
6263
unifyResponse.setMessage(exception.getMessage());
64+
log.error("", exception);
6365
} else {
6466
unifyResponse.setMessage(errorMessage);
67+
log.error("", exception.getClass().getConstructor(int.class, String.class).newInstance(code, errorMessage));
6568
}
6669
return unifyResponse;
6770
}

src/main/java/io/github/talelin/latticy/common/interceptor/AuthorizeVerifyResolverImpl.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ public boolean handleLogin(HttpServletRequest request, HttpServletResponse respo
6363
try {
6464
claims = jwt.decodeAccessToken(tokenStr);
6565
} catch (TokenExpiredException e) {
66-
throw new io.github.talelin.autoconfigure.exception.TokenExpiredException(e.getMessage(), 10051);
66+
throw new io.github.talelin.autoconfigure.exception.TokenExpiredException(10051);
6767
} catch (AlgorithmMismatchException | SignatureVerificationException | JWTDecodeException | InvalidClaimException e) {
68-
throw new TokenInvalidException(e.getMessage(), 10041);
68+
throw new TokenInvalidException(10041);
6969
}
7070
return getClaim(claims);
7171
}
@@ -83,7 +83,7 @@ public boolean handleGroup(HttpServletRequest request, HttpServletResponse respo
8383
List<PermissionDO> permissions = userService.getUserPermissions(userId);
8484
boolean matched = permissions.stream().anyMatch(it -> it.getModule().equals(module) && it.getName().equals(permission));
8585
if (!matched) {
86-
throw new AuthenticationException("you don't have the permission to access", 10001);
86+
throw new AuthenticationException(10001);
8787
}
8888
return true;
8989
}
@@ -93,7 +93,7 @@ public boolean handleAdmin(HttpServletRequest request, HttpServletResponse respo
9393
handleLogin(request, response, meta);
9494
UserDO user = LocalUser.getLocalUser();
9595
if (!verifyAdmin(user)) {
96-
throw new AuthenticationException("you don't have the permission to access", 10001);
96+
throw new AuthenticationException(10001);
9797
}
9898
return true;
9999
}
@@ -106,9 +106,9 @@ public boolean handleRefresh(HttpServletRequest request, HttpServletResponse res
106106
try {
107107
claims = jwt.decodeRefreshToken(tokenStr);
108108
} catch (TokenExpiredException e) {
109-
throw new io.github.talelin.autoconfigure.exception.TokenExpiredException(e.getMessage(), 10051);
109+
throw new io.github.talelin.autoconfigure.exception.TokenExpiredException(10051);
110110
} catch (AlgorithmMismatchException | SignatureVerificationException | JWTDecodeException | InvalidClaimException e) {
111-
throw new TokenInvalidException(e.getMessage(), 10041);
111+
throw new TokenInvalidException(10041);
112112
}
113113
return getClaim(claims);
114114
}
@@ -126,12 +126,12 @@ public void handleAfterCompletion(HttpServletRequest request, HttpServletRespons
126126

127127
private boolean getClaim(Map<String, Claim> claims) {
128128
if (claims == null) {
129-
throw new TokenInvalidException("token is invalid, can't be decode", 10041);
129+
throw new TokenInvalidException(10041);
130130
}
131131
int identity = claims.get("identity").asInt();
132132
UserDO user = userService.getById(identity);
133133
if (user == null) {
134-
throw new NotFoundException("user is not found", 10021);
134+
throw new NotFoundException(10021);
135135
}
136136
String avatarUrl;
137137
if (user.getAvatar() == null) {
@@ -159,18 +159,18 @@ private String verifyHeader(HttpServletRequest request, HttpServletResponse resp
159159
// 处理头部header,带有access_token的可以访问
160160
String authorization = request.getHeader(AUTHORIZATION_HEADER);
161161
if (authorization == null || Strings.isBlank(authorization)) {
162-
throw new AuthorizationException("authorization field is required", 10012);
162+
throw new AuthorizationException(10012);
163163
}
164164
String[] splits = authorization.split(" ");
165165
if (splits.length != 2) {
166-
throw new AuthorizationException("authorization field is invalid", 10013);
166+
throw new AuthorizationException(10013);
167167
}
168168
// Bearer 字段
169169
String scheme = splits[0];
170170
// token 字段
171171
String tokenStr = splits[1];
172172
if (!Pattern.matches(BEARER_PATTERN, scheme)) {
173-
throw new AuthorizationException("authorization field is invalid", 10013);
173+
throw new AuthorizationException(10013);
174174
}
175175
return tokenStr;
176176
}

src/main/java/io/github/talelin/latticy/controller/cms/UserController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ public CreatedVO register(@RequestBody @Validated RegisterDTO validator) {
7373
public Tokens login(@RequestBody @Validated LoginDTO validator) {
7474
UserDO user = userService.getUserByUsername(validator.getUsername());
7575
if (user == null) {
76-
throw new NotFoundException("user not found", 10021);
76+
throw new NotFoundException(10021);
7777
}
7878
boolean valid = userIdentityService.verifyUsernamePassword(
7979
user.getId(),
8080
user.getUsername(),
8181
validator.getPassword());
8282
if (!valid) {
83-
throw new ParameterException("username or password is fault", 10031);
83+
throw new ParameterException(10031);
8484
}
8585
return jwt.generateTokens(user.getId());
8686
}

src/main/java/io/github/talelin/latticy/controller/v1/BookController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class BookController {
4040
public BookDO getBook(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
4141
BookDO book = bookService.getById(id);
4242
if (book == null) {
43-
throw new NotFoundException("book not found", 10022);
43+
throw new NotFoundException(10022);
4444
}
4545
return book;
4646
}
@@ -70,7 +70,7 @@ public CreatedVO createBook(@RequestBody @Validated CreateOrUpdateBookDTO valida
7070
public UpdatedVO updateBook(@PathVariable("id") @Positive(message = "{id.positive}") Integer id, @RequestBody @Validated CreateOrUpdateBookDTO validator) {
7171
BookDO book = bookService.getById(id);
7272
if (book == null) {
73-
throw new NotFoundException("book not found", 10022);
73+
throw new NotFoundException(10022);
7474
}
7575
bookService.updateBook(book, validator);
7676
return new UpdatedVO(13);
@@ -83,7 +83,7 @@ public UpdatedVO updateBook(@PathVariable("id") @Positive(message = "{id.positiv
8383
public DeletedVO deleteBook(@PathVariable("id") @Positive(message = "{id.positive}") Integer id) {
8484
BookDO book = bookService.getById(id);
8585
if (book == null) {
86-
throw new NotFoundException("book not found", 10022);
86+
throw new NotFoundException(10022);
8787
}
8888
bookService.deleteById(book.getId());
8989
return new DeletedVO(14);

src/main/java/io/github/talelin/latticy/extension/file/LocalUploader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected boolean handleOneFile(byte[] bytes, String newFilename) {
4545
stream.close();
4646
} catch (Exception e) {
4747
log.error("write file to local err:", e);
48-
// throw new FailedException("read file date failed", 10190);
48+
// throw new FailedException(10190);
4949
return false;
5050
}
5151
return true;

src/main/java/io/github/talelin/latticy/module/file/AbstractUploader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ protected String getNewFilename(String ext) {
120120
*/
121121
protected void checkFileMap(MultiValueMap<String, MultipartFile> fileMap) {
122122
if (fileMap.isEmpty()) {
123-
throw new NotFoundException("file not found", 10026);
123+
throw new NotFoundException(10026);
124124
}
125125
int nums = getFileProperties().getNums();
126126
if (fileMap.size() > nums) {
127-
throw new FileTooManyException("too many files, amount of files must less than" + nums, 10180);
127+
throw new FileTooManyException(10180, "too many files, amount of files must less than" + nums);
128128
}
129129
}
130130

src/main/java/io/github/talelin/latticy/service/impl/AdminServiceImpl.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public boolean updateUserInfo(Integer id, UpdateUserInfoDTO validator) {
9999
Integer rootGroupId = groupService.getParticularGroupIdByLevel(GroupLevelEnum.ROOT);
100100
boolean anyMatch = newGroupIds.stream().anyMatch(it -> it.equals(rootGroupId));
101101
if (anyMatch) {
102-
throw new ForbiddenException("you can't add user to root group", 10073);
102+
throw new ForbiddenException(10073);
103103
}
104104
List<Integer> existGroupIds = groupService.getUserGroupIdsByUserId(id);
105105
// 删除existGroupIds有,而newGroupIds没有的
@@ -141,7 +141,7 @@ public boolean updateGroup(Integer id, UpdateGroupDTO dto) {
141141
// bug 如果只修改info,不修改name,则name已经存在,此时不应该报错
142142
GroupDO exist = groupService.getById(id);
143143
if (exist == null) {
144-
throw new NotFoundException("group not found", 10024);
144+
throw new NotFoundException(10024);
145145
}
146146
if (!exist.getName().equals(dto.getName())) {
147147
throwGroupNameExist(dto.getName());
@@ -156,10 +156,10 @@ public boolean deleteGroup(Integer id) {
156156
Integer rootGroupId = groupService.getParticularGroupIdByLevel(GroupLevelEnum.ROOT);
157157
Integer guestGroupId = groupService.getParticularGroupIdByLevel(GroupLevelEnum.GUEST);
158158
if (id.equals(rootGroupId)) {
159-
throw new ForbiddenException("root group can't delete", 10074);
159+
throw new ForbiddenException(10074);
160160
}
161161
if (id.equals(guestGroupId)) {
162-
throw new ForbiddenException("guest group can't delete", 10075);
162+
throw new ForbiddenException(10075);
163163
}
164164
throwGroupNotExistById(id);
165165
return groupService.removeById(id);
@@ -221,21 +221,21 @@ public Map<String, List<PermissionDO>> getAllStructuralPermissions() {
221221
private void throwUserNotExistById(Integer id) {
222222
boolean exist = userService.checkUserExistById(id);
223223
if (!exist) {
224-
throw new NotFoundException("user not found", 10021);
224+
throw new NotFoundException(10021);
225225
}
226226
}
227227

228228
private void throwGroupNotExistById(Integer id) {
229229
boolean exist = groupService.checkGroupExistById(id);
230230
if (!exist) {
231-
throw new NotFoundException("group not found", 10024);
231+
throw new NotFoundException(10024);
232232
}
233233
}
234234

235235
private void throwGroupNameExist(String name) {
236236
boolean exist = groupService.checkGroupExistByName(name);
237237
if (exist) {
238-
throw new ForbiddenException("group name already exist, please enter a new one", 10072);
238+
throw new ForbiddenException(10072);
239239
}
240240
}
241241
}

src/main/java/io/github/talelin/latticy/service/impl/GroupServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public boolean deleteUserGroupRelations(Integer userId, List<Integer> deleteIds)
8585
return true;
8686
}
8787
if (checkIsRootByUserId(userId)) {
88-
throw new ForbiddenException("can't modify the root user's group", 10078);
88+
throw new ForbiddenException(10078);
8989
}
9090
QueryWrapper<UserGroupDO> wrapper = new QueryWrapper<>();
9191
wrapper.lambda()
@@ -101,7 +101,7 @@ public boolean addUserGroupRelations(Integer userId, List<Integer> addIds) {
101101
}
102102
boolean ok = checkGroupExistByIds(addIds);
103103
if (!ok) {
104-
throw new ForbiddenException("cant't add user to non-existent group", 10077);
104+
throw new ForbiddenException(10077);
105105
}
106106
List<UserGroupDO> relations = addIds.stream().map(it -> new UserGroupDO(userId, it)).collect(Collectors.toList());
107107
return userGroupMapper.insertBatch(relations) > 0;

src/main/java/io/github/talelin/latticy/service/impl/UserServiceImpl.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
5959
public UserDO createUser(RegisterDTO dto) {
6060
boolean exist = this.checkUserExistByUsername(dto.getUsername());
6161
if (exist) {
62-
throw new ForbiddenException("username already exist, please choose a new one", 10071);
62+
throw new ForbiddenException(10071);
6363
}
6464
if (StrUtil.isNotBlank(dto.getEmail())) {
6565
exist = this.checkUserExistByEmail(dto.getEmail());
6666
if (exist) {
67-
throw new ForbiddenException("email already exist, please choose a new one", 10076);
67+
throw new ForbiddenException(10076);
6868
}
6969
} else {
7070
// bug 前端如果传入的email为 "" 时,由于数据库中存在""的email,会报duplication错误
@@ -99,7 +99,7 @@ public UserDO updateUserInfo(UpdateInfoDTO dto) {
9999
if (StrUtil.isNotBlank(dto.getUsername())) {
100100
boolean exist = this.checkUserExistByUsername(dto.getUsername());
101101
if (exist) {
102-
throw new ForbiddenException("username already exist, please choose a new one", 10071);
102+
throw new ForbiddenException(10071);
103103
}
104104
user.setUsername(dto.getUsername());
105105
userIdentityService.changeUsername(user.getId(), dto.getUsername());
@@ -114,11 +114,11 @@ public UserDO changeUserPassword(ChangePasswordDTO dto) {
114114
UserDO user = LocalUser.getLocalUser();
115115
boolean valid = userIdentityService.verifyUsernamePassword(user.getId(), user.getUsername(), dto.getOldPassword());
116116
if (!valid) {
117-
throw new ParameterException("password invalid, please enter correct password", 10032);
117+
throw new ParameterException(10032);
118118
}
119119
valid = userIdentityService.changePassword(user.getId(), dto.getNewPassword());
120120
if (!valid) {
121-
throw new FailedException("password change failed", 10011);
121+
throw new FailedException(10011);
122122
}
123123
return user;
124124
}
@@ -198,7 +198,7 @@ public Integer getRootUserId() {
198198
private void checkGroupsExist(List<Integer> ids) {
199199
for (Integer id : ids) {
200200
if (!groupService.checkGroupExistById(id)) {
201-
throw new NotFoundException("group not found,can't create user", 10023);
201+
throw new NotFoundException(10023);
202202
}
203203
}
204204
}
@@ -207,7 +207,7 @@ private void checkGroupsValid(List<Integer> ids) {
207207
Integer rootGroupId = groupService.getParticularGroupIdByLevel(GroupLevelEnum.ROOT);
208208
boolean anyMatch = ids.stream().anyMatch(it -> it.equals(rootGroupId));
209209
if (anyMatch) {
210-
throw new ForbiddenException("you can't add user to root group", 10073);
210+
throw new ForbiddenException(10073);
211211
}
212212
}
213213
}

0 commit comments

Comments
 (0)