Skip to content

Commit

Permalink
调整配置文件
Browse files Browse the repository at this point in the history
  • Loading branch information
pnoker committed Mar 29, 2022
1 parent 98c2b9f commit 1757606
Show file tree
Hide file tree
Showing 74 changed files with 1,120 additions and 743 deletions.
30 changes: 17 additions & 13 deletions dc3-center/dc3-center-auth/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@
# limitations under the License.
#

FROM registry.cn-beijing.aliyuncs.com/dc3/alpine-java:dragonwell-8.6.6
FROM registry.cn-beijing.aliyuncs.com/dc3/alpine-java:corretto-11
MAINTAINER pnoker <pnokers.icloud.com>

ENV JAVA_OPS -server -javaagent:/usr/local/java/lib/aspectjweaver.jar -Xms128m -Xmx1024m -XX:CompressedClassSpaceSize=128m -XX:MetaspaceSize=200m -XX:MaxMetaspaceSize=200m
ENV GC_LOG -XX:+PrintGCDetails \
-XX:+PrintGCDateStamps \
-XX:+PrintTenuringDistribution \
-XX:+PrintHeapAtGC \
-XX:+PrintReferenceGC \
-XX:+PrintGCApplicationStoppedTime \
-XX:+UseGCLogFileRotation \
-XX:NumberOfGCLogFiles=10 \
-XX:GCLogFileSize=10M \
-Xloggc:dc3/logs/center/auth/gc/dc3-center-auth-gc.log
ENV JAVA_OPS -server \
-javaagent:/usr/local/java/lib/aspectjweaver.jar \
-Xms128m \
-Xmx2048m \
-Xss256k \
-Djava.security.egd=file:/dev/./urandom \
-XX:MaxDirectMemorySize=256m \
-XX:+UseCompressedOops \
-XX:+UseCompressedClassPointers \
-XX:+SegmentedCodeCache \
-XX:+PrintCommandLineFlags
ENV GC_LOG -verbose:gc \
-XX:+UseG1GC \
-XX:+ExplicitGCInvokesConcurrent \
-Xlog:gc*,safepoint:dc3/logs/center/auth/gc/dc3-center-auth-gc.log:time,uptime:filecount=20,filesize=20M

RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

Expand All @@ -39,4 +43,4 @@ EXPOSE 8300
VOLUME /dc3-center/dc3-center-auth/dc3/logs

CMD mkdir -p /dc3-center/dc3-center-auth/dc3/logs/center/auth/gc \
&& java ${JAVA_OPS} ${GC_LOG} -Djava.security.egd=file:/dev/./urandom -jar dc3-center-auth.jar
&& java ${JAVA_OPS} ${GC_LOG} -jar dc3-center-auth.jar
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public R<User> selectById(Long id) {
@Override
public R<User> selectByName(String name) {
try {
User select = userService.selectByName(name);
User select = userService.selectByName(name, false);
if (null != select) {
return R.ok(select);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
Expand All @@ -36,7 +37,12 @@
public class WebMvcConfig implements WebMvcConfigurer {

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> list) {
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/static/");
}

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();

// Long ToStringSerializer
Expand All @@ -50,9 +56,11 @@ public void configureMessageConverters(List<HttpMessageConverter<?>> list) {
List<MediaType> mediaTypes = new ArrayList<>(jsonConverter.getSupportedMediaTypes());
mediaTypes.add(MediaType.ALL);
mediaTypes.add(MediaType.TEXT_PLAIN);
mediaTypes.add(MediaType.APPLICATION_JSON);
mediaTypes.add(MediaType.MULTIPART_FORM_DATA);

jsonConverter.setSupportedMediaTypes(mediaTypes);
list.add(jsonConverter);
converters.add(jsonConverter);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,29 @@ public interface UserService extends Service<User, UserDto> {
/**
* 根据用户名查询用户
*
* @param nama Username
* @param name Username
* @param isEx Throw Exception
* @return User
*/
User selectByName(String nama);
User selectByName(String name, boolean isEx);

/**
* 根据手机号查询用户
*
* @param phone Phone
* @param isEx Throw Exception
* @return User
*/
User selectByPhone(String phone);
User selectByPhone(String phone, boolean isEx);

/**
* 根据邮箱查询用户
*
* @param email Email
* @param isEx Throw Exception
* @return User
*/
User selectByEmail(String email);
User selectByEmail(String email, boolean isEx);

/**
* 根据用户名判断用户是否存在
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public String generateSalt(String username) {
public String generateToken(String tenant, String name, String salt, String password) {
checkUserLimit(name);
Tenant tempTenant = tenantService.selectByName(tenant);
User tempUser = userService.selectByName(name);
User tempUser = userService.selectByName(name, false);
if (tempTenant.getEnable() && tempUser.getEnable()) {
tenantBindService.selectByTenantIdAndUserId(tempTenant.getId(), tempUser.getId());
String redisSaltKey = Common.Cache.USER + Common.Cache.SALT + Common.Cache.SEPARATOR + name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@

package com.dc3.center.auth.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dc3.center.auth.mapper.UserMapper;
import com.dc3.center.auth.service.UserService;
import com.dc3.common.annotation.Logs;
import com.dc3.common.bean.Pages;
import com.dc3.common.constant.Common;
import com.dc3.common.dto.UserDto;
import com.dc3.common.exception.DuplicateException;
import com.dc3.common.exception.NotFoundException;
import com.dc3.common.exception.ServiceException;
import com.dc3.common.exception.*;
import com.dc3.common.model.User;
import com.dc3.common.utils.Dc3Util;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -33,6 +34,7 @@
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Optional;
Expand Down Expand Up @@ -62,37 +64,38 @@ public class UserServiceImpl implements UserService {
@CacheEvict(value = Common.Cache.USER + Common.Cache.LIST, allEntries = true, condition = "#result!=null")
}
)
@Logs("Add user")
@Transactional
// 2022-03-13 检查:不通过,会返回密码数据
public User add(User user) {
// 判断用户是否存在
if (null != selectByName(user.getName())) {
throw new DuplicateException("The user already exists with username {}", user.getName());
User selectByName = selectByName(user.getName(), false);
if (ObjectUtil.isNotNull(selectByName)) {
throw new DuplicateException("The user already exists with username: {}", user.getName());
}

// 判断 phone 是否存在
if (StrUtil.isNotBlank(user.getPhone())) {
if (null != selectByPhone(user.getPhone())) {
throw new DuplicateException("The user already exists with phone {}", user.getPhone());
// 判断 phone 是否存在,如果有 phone 不为空,检查该 phone 是否被占用
if (StrUtil.isNotEmpty(user.getPhone())) {
User selectByPhone = selectByPhone(user.getPhone(), false);
if (ObjectUtil.isNotNull(selectByPhone)) {
throw new DuplicateException("The user already exists with phone: {}", user.getPhone());
}
} else {
user.setPhone(null);
}


// 判断 email 是否存在
if (StrUtil.isNotBlank(user.getEmail())) {
if (null != selectByEmail(user.getEmail())) {
throw new DuplicateException("The user already exists with email {}", user.getEmail());
// 判断 email 是否存在,如果有 email 不为空,检查该 email 是否被占用
if (StrUtil.isNotEmpty(user.getEmail())) {
User selectByEmail = selectByEmail(user.getEmail(), false);
if (ObjectUtil.isNotNull(selectByEmail)) {
throw new DuplicateException("The user already exists with email: {}", user.getEmail());
}
} else {
user.setEmail(null);
}


// 插入 user 数据,并返回插入后的 user
if (userMapper.insert(user.setPassword(Dc3Util.md5(user.getPassword()))) > 0) {
return userMapper.selectById(user.getId());
}

throw new ServiceException("The user add failed");
throw new AddException("The user add failed: {}", user.toString());
}

@Override
Expand All @@ -106,6 +109,7 @@ public User add(User user) {
@CacheEvict(value = Common.Cache.USER + Common.Cache.LIST, allEntries = true, condition = "#result==true")
}
)
@Transactional
public boolean delete(Long id) {
User user = selectById(id);
if (null == user) {
Expand All @@ -129,12 +133,13 @@ public boolean delete(Long id) {
@CacheEvict(value = Common.Cache.USER + Common.Cache.LIST, allEntries = true, condition = "#result!=null")
}
)
@Transactional
public User update(User user) {
User byId = selectById(user.getId());
// 判断 phone 是否修改
if (StrUtil.isNotBlank(user.getPhone())) {
if (null == byId.getPhone() || !byId.getPhone().equals(user.getPhone())) {
if (null != selectByPhone(user.getPhone())) {
if (null != selectByPhone(user.getPhone(), false)) {
throw new DuplicateException("The user already exists with phone {}", user.getPhone());
}
}
Expand All @@ -145,7 +150,7 @@ public User update(User user) {
// 判断 email 是否修改
if (StrUtil.isNotBlank(user.getEmail())) {
if (null == byId.getEmail() || !byId.getEmail().equals(user.getEmail())) {
if (null != selectByEmail(user.getEmail())) {
if (null != selectByEmail(user.getEmail(), false)) {
throw new DuplicateException("The user already exists with email {}", user.getEmail());
}
}
Expand All @@ -170,30 +175,44 @@ public User selectById(Long id) {

@Override
@Cacheable(value = Common.Cache.USER + Common.Cache.NAME, key = "#name", unless = "#result==null")
public User selectByName(String name) {
LambdaQueryWrapper<User> queryWrapper = Wrappers.<User>query().lambda();
queryWrapper.eq(User::getName, name);
User user = userMapper.selectOne(queryWrapper);
if (null == user) {
throw new NotFoundException("The user does not exist");
// 2022-03-13 检查:通过
public User selectByName(String name, boolean isEx) {
if (StrUtil.isEmpty(name)) {
if (isEx) {
throw new EmptyException("The name is empty");
}
return null;
}
return user;

return selectByKey(User::getName, name, isEx);
}

@Override
@Cacheable(value = Common.Cache.USER + Common.Cache.PHONE, key = "#phone", unless = "#result==null")
public User selectByPhone(String phone) {
LambdaQueryWrapper<User> queryWrapper = Wrappers.<User>query().lambda();
queryWrapper.eq(User::getPhone, phone);
return userMapper.selectOne(queryWrapper);
// 2022-03-13 检查:通过
public User selectByPhone(String phone, boolean isEx) {
if (StrUtil.isEmpty(phone)) {
if (isEx) {
throw new EmptyException("The phone is empty");
}
return null;
}

return selectByKey(User::getPhone, phone, isEx);
}

@Override
@Cacheable(value = Common.Cache.USER + Common.Cache.EMAIL, key = "#email", unless = "#result==null")
public User selectByEmail(String email) {
LambdaQueryWrapper<User> queryWrapper = Wrappers.<User>query().lambda();
queryWrapper.eq(User::getEmail, email);
return userMapper.selectOne(queryWrapper);
// 2022-03-13 检查:通过
public User selectByEmail(String email, boolean isEx) {
if (StrUtil.isEmpty(email)) {
if (isEx) {
throw new EmptyException("The phone is empty");
}
return null;
}

return selectByKey(User::getEmail, email, isEx);
}

@Override
Expand All @@ -207,17 +226,17 @@ public Page<User> list(UserDto userDto) {

@Override
public boolean checkUserValid(String name) {
User user = selectByName(name);
User user = selectByName(name, false);
if (null != user) {
return user.getEnable();
}

user = selectByPhone(name);
user = selectByPhone(name, false);
if (null != user) {
return user.getEnable();
}

user = selectByEmail(name);
user = selectByEmail(name, false);
if (null != user) {
return user.getEnable();
}
Expand Down Expand Up @@ -246,4 +265,17 @@ public LambdaQueryWrapper<User> fuzzyQuery(UserDto userDto) {
return queryWrapper;
}

private User selectByKey(SFunction<User, ?> key, String value, boolean isEx) {
LambdaQueryWrapper<User> queryWrapper = Wrappers.<User>query().lambda();
queryWrapper.eq(key, value);
User user = userMapper.selectOne(queryWrapper);
if (ObjectUtil.isNull(user)) {
if (isEx) {
throw new NotFoundException("The user does not exist");
}
return null;
}
return user;
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
eureka:
instance:
ip-address: ${SERVICE_HOST:192.168.3.25}
ip-address: ${SERVICE_HOST:192.168.0.103}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
eureka:
instance:
ip-address: ${SERVICE_HOST:192.168.0.103}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
eureka:
instance:
ip-address: ${SERVICE_HOST:192.168.0.103}
4 changes: 2 additions & 2 deletions dc3-center/dc3-center-auth/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ spring:
name: @project.artifactId@
profiles:
active:
- server
- register
- monitor
- server
- mysql
- redis
- feign
- ${PROFILE_ACTIVE:dev}
- ${environment:dev}
main:
allow-bean-definition-overriding: true
mvc:
Expand Down
Loading

0 comments on commit 1757606

Please sign in to comment.