diff --git a/pom.xml b/pom.xml index e30dc2c..165be5f 100644 --- a/pom.xml +++ b/pom.xml @@ -34,7 +34,7 @@ 1.8 4.13.2 4.2.2 - + 1.2.9 3.5.1 4.0 11.2.0.3 @@ -119,9 +119,14 @@ postgresql - org.springframework.boot - spring-boot-starter-data-jdbc + com.alibaba + druid-spring-boot-starter + ${druid.version} + + + + cn.afterturn diff --git a/ym-admin/src/main/java/com/cnbm/admin/config/MybatisPlusConfig.java b/ym-admin/src/main/java/com/cnbm/admin/config/MybatisPlusConfig.java new file mode 100644 index 0000000..3f5b021 --- /dev/null +++ b/ym-admin/src/main/java/com/cnbm/admin/config/MybatisPlusConfig.java @@ -0,0 +1,29 @@ +package com.cnbm.admin.config; + +import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; +import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor; +import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor; +import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; +import com.cnbm.common.interceptor.DataFilterInterceptor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class MybatisPlusConfig { + + @Bean + public MybatisPlusInterceptor mybatisPlusInterceptor() { + MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); + // 数据权限 + mybatisPlusInterceptor.addInnerInterceptor(new DataFilterInterceptor()); + // 分页插件 + mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); + // 乐观锁 + mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); + // 防止全表更新与删除 + mybatisPlusInterceptor.addInnerInterceptor(new BlockAttackInnerInterceptor()); + + return mybatisPlusInterceptor; + } + +} diff --git a/ym-admin/src/main/java/com/cnbm/admin/controller/SysLogLoginController.java b/ym-admin/src/main/java/com/cnbm/admin/controller/SysLogLoginController.java index 95cf2a1..8234ffa 100644 --- a/ym-admin/src/main/java/com/cnbm/admin/controller/SysLogLoginController.java +++ b/ym-admin/src/main/java/com/cnbm/admin/controller/SysLogLoginController.java @@ -12,6 +12,7 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; +import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.GetMapping; @@ -27,6 +28,7 @@ import java.util.Map; @RestController @RequestMapping("sys/log/login") @Api(tags="登录日志") +@Log4j2 public class SysLogLoginController { @Autowired private SysLogLoginService sysLogLoginService; @@ -45,6 +47,8 @@ public class SysLogLoginController { public Result> page(@ApiIgnore @RequestParam Map params){ PageData page = sysLogLoginService.page(params); + log.info("PageData====={}",page.toString()); + return new Result>().ok(page); } diff --git a/ym-admin/src/main/java/com/cnbm/admin/enums/LoginOperationEnum.java b/ym-admin/src/main/java/com/cnbm/admin/enums/LoginOperationEnum.java new file mode 100644 index 0000000..dea9804 --- /dev/null +++ b/ym-admin/src/main/java/com/cnbm/admin/enums/LoginOperationEnum.java @@ -0,0 +1,22 @@ +package com.cnbm.admin.enums; + +public enum LoginOperationEnum { + /** + * 用户登录 + */ + LOGIN(0), + /** + * 用户退出 + */ + LOGOUT(1); + + private int value; + + LoginOperationEnum(int value) { + this.value = value; + } + + public int value() { + return this.value; + } +} diff --git a/ym-admin/src/main/java/com/cnbm/admin/enums/LoginStatusEnum.java b/ym-admin/src/main/java/com/cnbm/admin/enums/LoginStatusEnum.java new file mode 100644 index 0000000..e30d1b3 --- /dev/null +++ b/ym-admin/src/main/java/com/cnbm/admin/enums/LoginStatusEnum.java @@ -0,0 +1,26 @@ +package com.cnbm.admin.enums; + +public enum LoginStatusEnum { + /** + * 失败 + */ + FAIL(0), + /** + * 成功 + */ + SUCCESS(1), + /** + * 账号已锁定 + */ + LOCK(2); + + private int value; + + LoginStatusEnum(int value) { + this.value = value; + } + + public int value() { + return this.value; + } +} diff --git a/ym-admin/src/main/java/com/cnbm/admin/service/impl/LoginServiceImpl.java b/ym-admin/src/main/java/com/cnbm/admin/service/impl/LoginServiceImpl.java index ada95af..d1cf8c1 100644 --- a/ym-admin/src/main/java/com/cnbm/admin/service/impl/LoginServiceImpl.java +++ b/ym-admin/src/main/java/com/cnbm/admin/service/impl/LoginServiceImpl.java @@ -1,14 +1,20 @@ package com.cnbm.admin.service.impl; import com.cnbm.admin.entity.LoginUser; +import com.cnbm.admin.entity.SysLogLoginEntity; +import com.cnbm.admin.enums.LoginOperationEnum; +import com.cnbm.admin.enums.LoginStatusEnum; import com.cnbm.admin.params.LoginParam; import com.cnbm.admin.service.CaptchaService; +import com.cnbm.admin.service.SysLogLoginService; import com.cnbm.admin.utils.JwtUtil; import com.cnbm.admin.service.LoginService; import com.cnbm.admin.utils.ResponseResult; import com.cnbm.common.exception.ErrorCode; +import com.cnbm.common.utils.IpUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.http.HttpHeaders; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; @@ -16,6 +22,7 @@ import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletRequest; +import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Objects; @@ -37,6 +44,9 @@ public class LoginServiceImpl implements LoginService { @Autowired private CaptchaService captchaService; + @Autowired + private SysLogLoginService sysLogLoginService; + @Override public ResponseResult login(HttpServletRequest request, LoginParam loginParam) { //验证码是否正确 @@ -46,7 +56,18 @@ public class LoginServiceImpl implements LoginService { } UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginParam.getUsername(),loginParam.getPassword()); Authentication authenticate = authenticationManager.authenticate(authenticationToken); + //登录日志 + SysLogLoginEntity log = new SysLogLoginEntity(); + log.setOperation(LoginOperationEnum.LOGIN.value()); + log.setCreateDate(new Date()); + log.setIp(IpUtils.getIpAddr(request)); + log.setUserAgent(request.getHeader(HttpHeaders.USER_AGENT)); + log.setCreatorName(loginParam.getUsername()); + if (Objects.isNull(authenticate)) { + log.setStatus(LoginStatusEnum.FAIL.value()); + log.setCreatorName(loginParam.getUsername()); + sysLogLoginService.save(log); throw new RuntimeException("登录失败"); } //如果认证通过了,使用userid生成一个jwt jwt存入ResponseResult返回 @@ -57,6 +78,11 @@ public class LoginServiceImpl implements LoginService { map.put("token",jwt); //把完整的用户信息存入redis userid作为key redisTemplate.opsForValue().set("login:"+userid,loginUser); + //登录成功 + log.setStatus(LoginStatusEnum.SUCCESS.value()); + log.setCreator(loginUser.getSysUserEntity().getId()); + log.setCreatorName(loginUser.getUsername()); + sysLogLoginService.save(log); return new ResponseResult(200,"登录成功",map); } diff --git a/ym-admin/src/main/java/com/cnbm/admin/service/impl/SysLogLoginServiceImpl.java b/ym-admin/src/main/java/com/cnbm/admin/service/impl/SysLogLoginServiceImpl.java index 21db772..e233466 100644 --- a/ym-admin/src/main/java/com/cnbm/admin/service/impl/SysLogLoginServiceImpl.java +++ b/ym-admin/src/main/java/com/cnbm/admin/service/impl/SysLogLoginServiceImpl.java @@ -10,6 +10,7 @@ import com.cnbm.common.constant.Constant; import com.cnbm.common.page.PageData; import com.cnbm.common.service.impl.BaseServiceImpl; import com.cnbm.common.utils.ConvertUtils; +import lombok.extern.log4j.Log4j2; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -18,6 +19,7 @@ import java.util.List; import java.util.Map; @Service +@Log4j2 public class SysLogLoginServiceImpl extends BaseServiceImpl implements SysLogLoginService { @Override @@ -26,6 +28,7 @@ public class SysLogLoginServiceImpl extends BaseServiceImpl wrapper = new QueryWrapper<>(); wrapper.eq(StringUtils.isNotBlank(status), "status", status); wrapper.like(StringUtils.isNotBlank(creatorName), "creator_name", creatorName); - return wrapper; } diff --git a/ym-admin/src/main/java/com/cnbm/admin/service/impl/UserDetailsServiceImpl.java b/ym-admin/src/main/java/com/cnbm/admin/service/impl/UserDetailsServiceImpl.java index 389c4c9..0204aa6 100644 --- a/ym-admin/src/main/java/com/cnbm/admin/service/impl/UserDetailsServiceImpl.java +++ b/ym-admin/src/main/java/com/cnbm/admin/service/impl/UserDetailsServiceImpl.java @@ -5,23 +5,24 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.cnbm.admin.dto.SysMenuDTO; import com.cnbm.admin.dto.SysUserDTO; import com.cnbm.admin.entity.LoginUser; +import com.cnbm.admin.entity.SysLogLoginEntity; import com.cnbm.admin.entity.SysMenuEntity; import com.cnbm.admin.entity.SysUserEntity; +import com.cnbm.admin.enums.LoginOperationEnum; import com.cnbm.admin.exception.LoginStatusException; import com.cnbm.admin.service.SysMenuService; import com.cnbm.common.utils.ConvertUtils; +import com.cnbm.common.utils.IpUtils; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; import org.springframework.security.authentication.DisabledException; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; /** @@ -45,6 +46,7 @@ public class UserDetailsServiceImpl implements UserDetailsService { LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(SysUserEntity::getUsername,username); SysUserEntity sysUserEntity = userDao.selectOne(lambdaQueryWrapper); + if (Objects.isNull(sysUserEntity)) { throw new UsernameNotFoundException("用户名不存在"); } diff --git a/ym-common/src/main/java/com/cnbm/common/interceptor/DataFilterInterceptor.java b/ym-common/src/main/java/com/cnbm/common/interceptor/DataFilterInterceptor.java new file mode 100644 index 0000000..9c8db8d --- /dev/null +++ b/ym-common/src/main/java/com/cnbm/common/interceptor/DataFilterInterceptor.java @@ -0,0 +1,76 @@ +package com.cnbm.common.interceptor; + +import cn.hutool.core.util.StrUtil; +import com.baomidou.mybatisplus.core.toolkit.PluginUtils; +import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; +import net.sf.jsqlparser.JSQLParserException; +import net.sf.jsqlparser.expression.Expression; +import net.sf.jsqlparser.expression.StringValue; +import net.sf.jsqlparser.expression.operators.conditional.AndExpression; +import net.sf.jsqlparser.parser.CCJSqlParserUtil; +import net.sf.jsqlparser.statement.select.PlainSelect; +import net.sf.jsqlparser.statement.select.Select; +import org.apache.ibatis.executor.Executor; +import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.session.ResultHandler; +import org.apache.ibatis.session.RowBounds; + +import java.util.Map; + +public class DataFilterInterceptor implements InnerInterceptor { + + @Override + public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) { + DataScope scope = getDataScope(parameter); + // 不进行数据过滤 + if(scope == null || StrUtil.isBlank(scope.getSqlFilter())){ + return; + } + + // 拼接新SQL + String buildSql = getSelect(boundSql.getSql(), scope); + + // 重写SQL + PluginUtils.mpBoundSql(boundSql).sql(buildSql); + } + + private DataScope getDataScope(Object parameter){ + if (parameter == null){ + return null; + } + + // 判断参数里是否有DataScope对象 + if (parameter instanceof Map) { + Map parameterMap = (Map) parameter; + for (Map.Entry entry : parameterMap.entrySet()) { + if (entry.getValue() != null && entry.getValue() instanceof DataScope) { + return (DataScope) entry.getValue(); + } + } + } else if (parameter instanceof DataScope) { + return (DataScope) parameter; + } + + return null; + } + + private String getSelect(String buildSql, DataScope scope){ + try { + Select select = (Select) CCJSqlParserUtil.parse(buildSql); + PlainSelect plainSelect = (PlainSelect) select.getSelectBody(); + + Expression expression = plainSelect.getWhere(); + if(expression == null){ + plainSelect.setWhere(new StringValue(scope.getSqlFilter())); + }else{ + AndExpression andExpression = new AndExpression(expression, new StringValue(scope.getSqlFilter())); + plainSelect.setWhere(andExpression); + } + + return select.toString().replaceAll("'", ""); + }catch (JSQLParserException e){ + return buildSql; + } + } +} diff --git a/ym-common/src/main/java/com/cnbm/common/interceptor/DataScope.java b/ym-common/src/main/java/com/cnbm/common/interceptor/DataScope.java new file mode 100644 index 0000000..1f7f8a2 --- /dev/null +++ b/ym-common/src/main/java/com/cnbm/common/interceptor/DataScope.java @@ -0,0 +1,22 @@ +package com.cnbm.common.interceptor; + +public class DataScope { + private String sqlFilter; + + public DataScope(String sqlFilter) { + this.sqlFilter = sqlFilter; + } + + public String getSqlFilter() { + return sqlFilter; + } + + public void setSqlFilter(String sqlFilter) { + this.sqlFilter = sqlFilter; + } + + @Override + public String toString() { + return this.sqlFilter; + } +} diff --git a/ym-gateway/src/main/resources/application-dev.yml b/ym-gateway/src/main/resources/application-dev.yml index 609c59e..2bdf131 100644 --- a/ym-gateway/src/main/resources/application-dev.yml +++ b/ym-gateway/src/main/resources/application-dev.yml @@ -20,10 +20,36 @@ spring: # url: jdbc:postgresql://192.168.10.10:5432/postgres # username: postgres # password: 123456 - hikari: - pool-name: GrowUpHikariCP - minimum-idle: 1 - maximum-pool-size: 10 + initial-size: 10 + max-active: 100 + min-idle: 10 + max-wait: 60000 + pool-prepared-statements: true + max-pool-prepared-statement-per-connection-size: 20 + time-between-eviction-runs-millis: 60000 + min-evictable-idle-time-millis: 300000 + #Oracle需要打开注释 + #validation-query: SELECT 1 FROM DUAL + test-while-idle: true + test-on-borrow: false + test-on-return: false + stat-view-servlet: + enabled: true + url-pattern: /druid/* + #login-username: admin + #login-password: admin + filter: + stat: + log-slow-sql: true + slow-sql-millis: 1000 + merge-sql: false + wall: + config: + multi-statement-allow: true +# hikari: +# pool-name: GrowUpHikariCP +# minimum-idle: 1 +# maximum-pool-size: 10 ##多数据源的配置,需要引用renren-dynamic-datasource #dynamic: diff --git a/ym-gateway/src/main/resources/application.yml b/ym-gateway/src/main/resources/application.yml index d3ab12d..f6a9105 100644 --- a/ym-gateway/src/main/resources/application.yml +++ b/ym-gateway/src/main/resources/application.yml @@ -69,12 +69,12 @@ mybatis-plus: cache-enabled: false call-setters-on-nulls: true jdbc-type-for-null: 'null' - log-impl: org.apache.ibatis.logging.stdout.StdOutImpl +# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl configuration-properties: prefix: blobType: BLOB boolValue: TRUE -logging: - level: - com.cnbm.admin.dao: DEBUG \ No newline at end of file +#logging: +# level: +# com.cnbm.admin.dao: DEBUG \ No newline at end of file diff --git a/ym-gateway/src/main/resources/logback-spring.xml b/ym-gateway/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..7111d2f --- /dev/null +++ b/ym-gateway/src/main/resources/logback-spring.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + +