(improvement)[build] Use Spotless to customize the code formatting (#1750)

This commit is contained in:
lexluo09
2024-10-04 00:05:04 +08:00
committed by GitHub
parent 44d1cde34f
commit 71a9954be5
521 changed files with 7811 additions and 13046 deletions

View File

@@ -7,4 +7,5 @@ import java.lang.annotation.Target;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface AuthenticationIgnore {}
public @interface AuthenticationIgnore {
}

View File

@@ -24,9 +24,8 @@ public class AuthenticationConfig {
@Value("${s2.authentication.token.default.appKey:supersonic}")
private String tokenDefaultAppKey;
@Value(
"${s2.authentication.token.appSecret:supersonic:WIaO9YRRVt+7QtpPvyWsARFngnEcbaKBk"
+ "783uGFwMrbJBaochsqCH62L4Kijcb0sZCYoSsiKGV/zPml5MnZ3uQ==}")
@Value("${s2.authentication.token.appSecret:supersonic:WIaO9YRRVt+7QtpPvyWsARFngnEcbaKBk"
+ "783uGFwMrbJBaochsqCH62L4Kijcb0sZCYoSsiKGV/zPml5MnZ3uQ==}")
private String tokenAppSecret;
@Value("${s2.authentication.token.http.header.key:Authorization}")
@@ -48,8 +47,7 @@ public class AuthenticationConfig {
private Long tokenTimeout;
public Map<String, String> getAppKeyToSecretMap() {
return Arrays.stream(this.tokenAppSecret.split(","))
.map(s -> s.split(":"))
return Arrays.stream(this.tokenAppSecret.split(",")).map(s -> s.split(":"))
.collect(Collectors.toMap(e -> e[0].trim(), e -> e[1].trim()));
}
}

View File

@@ -20,8 +20,8 @@ public class User {
private Integer isAdmin;
public static User get(
Long id, String name, String displayName, String email, Integer isAdmin) {
public static User get(Long id, String name, String displayName, String email,
Integer isAdmin) {
return new User(id, name, displayName, email, isAdmin);
}

View File

@@ -9,24 +9,14 @@ public class UserWithPassword extends User {
private String password;
public UserWithPassword(
Long id,
String name,
String displayName,
String email,
String password,
public UserWithPassword(Long id, String name, String displayName, String email, String password,
Integer isAdmin) {
super(id, name, displayName, email, isAdmin);
this.password = password;
}
public static UserWithPassword get(
Long id,
String name,
String displayName,
String email,
String password,
Integer isAdmin) {
public static UserWithPassword get(Long id, String name, String displayName, String email,
String password, Integer isAdmin) {
return new UserWithPassword(id, name, displayName, email, password, isAdmin);
}
}

View File

@@ -12,8 +12,8 @@ import java.util.Set;
public interface UserService {
User getCurrentUser(
HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse);
User getCurrentUser(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse);
List<String> getUserNames();

View File

@@ -52,12 +52,10 @@ public class DefaultUserAdaptor implements UserAdaptor {
new Organization("1", "0", "SuperSonic", "SuperSonic", Lists.newArrayList(), true);
Organization hr =
new Organization("2", "1", "Hr", "SuperSonic/Hr", Lists.newArrayList(), false);
Organization sales =
new Organization(
"3", "1", "Sales", "SuperSonic/Sales", Lists.newArrayList(), false);
Organization marketing =
new Organization(
"4", "1", "Marketing", "SuperSonic/Marketing", Lists.newArrayList(), false);
Organization sales = new Organization("3", "1", "Sales", "SuperSonic/Sales",
Lists.newArrayList(), false);
Organization marketing = new Organization("4", "1", "Marketing", "SuperSonic/Marketing",
Lists.newArrayList(), false);
List<Organization> subOrganization = Lists.newArrayList(hr, sales, marketing);
superSonic.setSubOrganizations(subOrganization);
return Lists.newArrayList(superSonic);
@@ -113,19 +111,12 @@ public class DefaultUserAdaptor implements UserAdaptor {
throw new RuntimeException("user not exist,please register");
}
try {
String password =
AESEncryptionUtil.encrypt(
userReq.getPassword(),
AESEncryptionUtil.getBytesFromString(userDO.getSalt()));
String password = AESEncryptionUtil.encrypt(userReq.getPassword(),
AESEncryptionUtil.getBytesFromString(userDO.getSalt()));
if (userDO.getPassword().equals(password)) {
UserWithPassword user =
UserWithPassword.get(
userDO.getId(),
userDO.getName(),
userDO.getDisplayName(),
userDO.getEmail(),
userDO.getPassword(),
userDO.getIsAdmin());
UserWithPassword user = UserWithPassword.get(userDO.getId(), userDO.getName(),
userDO.getDisplayName(), userDO.getEmail(), userDO.getPassword(),
userDO.getIsAdmin());
return user;
} else {
throw new RuntimeException("password not correct, please try again");

View File

@@ -68,8 +68,8 @@ public abstract class AuthenticationInterceptor implements HandlerInterceptor {
try {
if (request instanceof StandardMultipartHttpServletRequest) {
RequestFacade servletRequest =
(RequestFacade)
((StandardMultipartHttpServletRequest) request).getRequest();
(RequestFacade) ((StandardMultipartHttpServletRequest) request)
.getRequest();
Class<? extends HttpServletRequest> servletRequestClazz = servletRequest.getClass();
Field request1 = servletRequestClazz.getDeclaredField("request");
request1.setAccessible(true);

View File

@@ -22,9 +22,8 @@ import java.lang.reflect.Method;
public class DefaultAuthenticationInterceptor extends AuthenticationInterceptor {
@Override
public boolean preHandle(
HttpServletRequest request, HttpServletResponse response, Object handler)
throws AccessException {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws AccessException {
authenticationConfig = ContextUtils.getBean(AuthenticationConfig.class);
userServiceImpl = ContextUtils.getBean(UserServiceImpl.class);
userTokenUtils = ContextUtils.getBean(UserTokenUtils.class);
@@ -74,11 +73,9 @@ public class DefaultAuthenticationInterceptor extends AuthenticationInterceptor
}
private void setContext(String userName, HttpServletRequest request) {
ThreadContext threadContext =
ThreadContext.builder()
.token(request.getHeader(authenticationConfig.getTokenHttpHeaderKey()))
.userName(userName)
.build();
ThreadContext threadContext = ThreadContext.builder()
.token(request.getHeader(authenticationConfig.getTokenHttpHeaderKey()))
.userName(userName).build();
s2ThreadContext.set(threadContext);
}
}

View File

@@ -13,17 +13,14 @@ public class InterceptorFactory implements WebMvcConfigurer {
private List<AuthenticationInterceptor> authenticationInterceptors;
public InterceptorFactory() {
authenticationInterceptors =
SpringFactoriesLoader.loadFactories(
AuthenticationInterceptor.class,
Thread.currentThread().getContextClassLoader());
authenticationInterceptors = SpringFactoriesLoader.loadFactories(
AuthenticationInterceptor.class, Thread.currentThread().getContextClassLoader());
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
for (AuthenticationInterceptor authenticationInterceptor : authenticationInterceptors) {
registry.addInterceptor(authenticationInterceptor)
.addPathPatterns("/**")
registry.addInterceptor(authenticationInterceptor).addPathPatterns("/**")
.excludePathPatterns("/", "/webapp/**", "/error");
}
}

View File

@@ -138,8 +138,8 @@ public class UserDOExample {
criteria.add(new Criterion(condition, value));
}
protected void addCriterion(
String condition, Object value1, Object value2, String property) {
protected void addCriterion(String condition, Object value1, Object value2,
String property) {
if (value1 == null || value2 == null) {
throw new RuntimeException("Between values for " + property + " cannot be null");
}
@@ -628,8 +628,8 @@ public class UserDOExample {
this(condition, value, null);
}
protected Criterion(
String condition, Object value, Object secondValue, String typeHandler) {
protected Criterion(String condition, Object value, Object secondValue,
String typeHandler) {
super();
this.condition = condition;
this.value = value;

View File

@@ -30,8 +30,8 @@ public class UserController {
}
@GetMapping("/getCurrentUser")
public User getCurrentUser(
HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
public User getCurrentUser(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
return userService.getCurrentUser(httpServletRequest, httpServletResponse);
}

View File

@@ -27,8 +27,8 @@ public class UserServiceImpl implements UserService {
}
@Override
public User getCurrentUser(
HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
public User getCurrentUser(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
User user = UserHolder.findUser(httpServletRequest, httpServletResponse);
if (user != null) {
SystemConfig systemConfig = sysParameterService.getSystemConfig();

View File

@@ -18,8 +18,8 @@ public class UserStrategyFactory {
private AuthenticationConfig authenticationConfig;
public UserStrategyFactory(
AuthenticationConfig authenticationConfig, List<UserStrategy> userStrategyList) {
public UserStrategyFactory(AuthenticationConfig authenticationConfig,
List<UserStrategy> userStrategyList) {
this.authenticationConfig = authenticationConfig;
this.userStrategyList = userStrategyList;
}

View File

@@ -17,8 +17,7 @@ public class ComponentFactory {
}
private static <T> T init(Class<T> factoryType) {
return SpringFactoriesLoader.loadFactories(
factoryType, Thread.currentThread().getContextClassLoader())
.get(0);
return SpringFactoriesLoader
.loadFactories(factoryType, Thread.currentThread().getContextClassLoader()).get(0);
}
}

View File

@@ -48,8 +48,7 @@ public class UserTokenUtils {
Map<String, Object> claims = new HashMap<>(5);
claims.put(TOKEN_USER_ID, user.getId());
claims.put(TOKEN_USER_NAME, StringUtils.isEmpty(user.getName()) ? "" : user.getName());
claims.put(
TOKEN_USER_PASSWORD,
claims.put(TOKEN_USER_PASSWORD,
StringUtils.isEmpty(user.getPassword()) ? "" : user.getPassword());
claims.put(TOKEN_USER_DISPLAY_NAME, user.getDisplayName());
claims.put(TOKEN_CREATE_TIME, System.currentTimeMillis());
@@ -83,10 +82,8 @@ public class UserTokenUtils {
String userName = String.valueOf(claims.get(TOKEN_USER_NAME));
String email = String.valueOf(claims.get(TOKEN_USER_EMAIL));
String displayName = String.valueOf(claims.get(TOKEN_USER_DISPLAY_NAME));
Integer isAdmin =
claims.get(TOKEN_IS_ADMIN) == null
? 0
: Integer.parseInt(claims.get(TOKEN_IS_ADMIN).toString());
Integer isAdmin = claims.get(TOKEN_IS_ADMIN) == null ? 0
: Integer.parseInt(claims.get(TOKEN_IS_ADMIN).toString());
return User.get(userId, userName, displayName, email, isAdmin);
}
@@ -105,10 +102,8 @@ public class UserTokenUtils {
String email = String.valueOf(claims.get(TOKEN_USER_EMAIL));
String displayName = String.valueOf(claims.get(TOKEN_USER_DISPLAY_NAME));
String password = String.valueOf(claims.get(TOKEN_USER_PASSWORD));
Integer isAdmin =
claims.get(TOKEN_IS_ADMIN) == null
? 0
: Integer.parseInt(claims.get(TOKEN_IS_ADMIN).toString());
Integer isAdmin = claims.get(TOKEN_IS_ADMIN) == null ? 0
: Integer.parseInt(claims.get(TOKEN_IS_ADMIN).toString());
return UserWithPassword.get(userId, userName, displayName, email, password, isAdmin);
}
@@ -121,11 +116,8 @@ public class UserTokenUtils {
try {
String tokenSecret = getTokenSecret(appKey);
Claims claims =
Jwts.parser()
.setSigningKey(tokenSecret.getBytes(StandardCharsets.UTF_8))
.build()
.parseClaimsJws(getTokenString(token))
.getBody();
Jwts.parser().setSigningKey(tokenSecret.getBytes(StandardCharsets.UTF_8))
.build().parseClaimsJws(getTokenString(token)).getBody();
return Optional.of(claims);
} catch (Exception e) {
log.info("can not getClaims from appKey:{} token:{}, please login", appKey, token);
@@ -149,15 +141,10 @@ public class UserTokenUtils {
Date expirationDate = new Date(expiration);
String tokenSecret = getTokenSecret(appKey);
return Jwts.builder()
.setClaims(claims)
.setSubject(claims.get(TOKEN_USER_NAME).toString())
return Jwts.builder().setClaims(claims).setSubject(claims.get(TOKEN_USER_NAME).toString())
.setExpiration(expirationDate)
.signWith(
new SecretKeySpec(
tokenSecret.getBytes(StandardCharsets.UTF_8),
SignatureAlgorithm.HS512.getJcaName()),
SignatureAlgorithm.HS512)
.signWith(new SecretKeySpec(tokenSecret.getBytes(StandardCharsets.UTF_8),
SignatureAlgorithm.HS512.getJcaName()), SignatureAlgorithm.HS512)
.compact();
}

View File

@@ -31,8 +31,7 @@ public class AuthController {
}
@GetMapping("/queryGroup")
public List<AuthGroup> queryAuthGroup(
@RequestParam("modelId") String modelId,
public List<AuthGroup> queryAuthGroup(@RequestParam("modelId") String modelId,
@RequestParam(value = "groupId", required = false) Integer groupId) {
return authService.queryAuthGroups(modelId, groupId);
}
@@ -69,10 +68,8 @@ public class AuthController {
* @return
*/
@PostMapping("/queryAuthorizedRes")
public AuthorizedResourceResp queryAuthorizedResources(
@RequestBody QueryAuthResReq req,
HttpServletRequest request,
HttpServletResponse response) {
public AuthorizedResourceResp queryAuthorizedResources(@RequestBody QueryAuthResReq req,
HttpServletRequest request, HttpServletResponse response) {
User user = UserHolder.findUser(request, response);
return authService.queryAuthorizedResources(req, user);
}

View File

@@ -39,18 +39,15 @@ public class AuthServiceImpl implements AuthService {
List<String> rows =
jdbcTemplate.queryForList("select config from s2_auth_groups", String.class);
Gson g = new Gson();
return rows.stream()
.map(row -> g.fromJson(row, AuthGroup.class))
return rows.stream().map(row -> g.fromJson(row, AuthGroup.class))
.collect(Collectors.toList());
}
@Override
public List<AuthGroup> queryAuthGroups(String modelId, Integer groupId) {
return load().stream()
.filter(
group ->
(Objects.isNull(groupId) || groupId.equals(group.getGroupId()))
&& modelId.equals(group.getModelId().toString()))
.filter(group -> (Objects.isNull(groupId) || groupId.equals(group.getGroupId()))
&& modelId.equals(group.getModelId().toString()))
.collect(Collectors.toList());
}
@@ -65,15 +62,11 @@ public class AuthServiceImpl implements AuthService {
nextGroupId = obj + 1;
}
group.setGroupId(nextGroupId);
jdbcTemplate.update(
"insert into s2_auth_groups (group_id, config) values (?, ?);",
nextGroupId,
g.toJson(group));
jdbcTemplate.update("insert into s2_auth_groups (group_id, config) values (?, ?);",
nextGroupId, g.toJson(group));
} else {
jdbcTemplate.update(
"update s2_auth_groups set config = ? where group_id = ?;",
g.toJson(group),
group.getGroupId());
jdbcTemplate.update("update s2_auth_groups set config = ? where group_id = ?;",
g.toJson(group), group.getGroupId());
}
}
@@ -119,30 +112,24 @@ public class AuthServiceImpl implements AuthService {
return resource;
}
private List<AuthGroup> getAuthGroups(
List<Long> modelIds, String userName, List<String> departmentIds) {
List<AuthGroup> groups =
load().stream()
.filter(
group -> {
if (!modelIds.contains(group.getModelId())) {
return false;
}
if (!CollectionUtils.isEmpty(group.getAuthorizedUsers())
&& group.getAuthorizedUsers().contains(userName)) {
return true;
}
for (String departmentId : departmentIds) {
if (!CollectionUtils.isEmpty(
group.getAuthorizedDepartmentIds())
&& group.getAuthorizedDepartmentIds()
.contains(departmentId)) {
return true;
}
}
return false;
})
.collect(Collectors.toList());
private List<AuthGroup> getAuthGroups(List<Long> modelIds, String userName,
List<String> departmentIds) {
List<AuthGroup> groups = load().stream().filter(group -> {
if (!modelIds.contains(group.getModelId())) {
return false;
}
if (!CollectionUtils.isEmpty(group.getAuthorizedUsers())
&& group.getAuthorizedUsers().contains(userName)) {
return true;
}
for (String departmentId : departmentIds) {
if (!CollectionUtils.isEmpty(group.getAuthorizedDepartmentIds())
&& group.getAuthorizedDepartmentIds().contains(departmentId)) {
return true;
}
}
return false;
}).collect(Collectors.toList());
log.info("user:{} department:{} authGroups:{}", userName, departmentIds, groups);
return groups;
}