fix: 🐛修改分页不生效问题

This commit is contained in:
weihongyang 2022-08-08 16:36:53 +08:00
parent 16e656d820
commit 030abafd1f
13 changed files with 277 additions and 16 deletions

11
pom.xml
View File

@ -34,7 +34,7 @@
<java.version>1.8</java.version> <java.version>1.8</java.version>
<junit.version>4.13.2</junit.version> <junit.version>4.13.2</junit.version>
<jedis.version>4.2.2</jedis.version> <jedis.version>4.2.2</jedis.version>
<!-- <druid.version>1.2.9</druid.version>--> <druid.version>1.2.9</druid.version>
<mybatisplus.version>3.5.1</mybatisplus.version> <mybatisplus.version>3.5.1</mybatisplus.version>
<sqlserver.version>4.0</sqlserver.version> <sqlserver.version>4.0</sqlserver.version>
<oracle.version>11.2.0.3</oracle.version> <oracle.version>11.2.0.3</oracle.version>
@ -119,9 +119,14 @@
<artifactId>postgresql</artifactId> <artifactId>postgresql</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>com.alibaba</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId> <artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency> </dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-data-jdbc</artifactId>-->
<!-- </dependency>-->
<dependency> <dependency>
<groupId>cn.afterturn</groupId> <groupId>cn.afterturn</groupId>

View File

@ -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;
}
}

View File

@ -12,6 +12,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@ -27,6 +28,7 @@ import java.util.Map;
@RestController @RestController
@RequestMapping("sys/log/login") @RequestMapping("sys/log/login")
@Api(tags="登录日志") @Api(tags="登录日志")
@Log4j2
public class SysLogLoginController { public class SysLogLoginController {
@Autowired @Autowired
private SysLogLoginService sysLogLoginService; private SysLogLoginService sysLogLoginService;
@ -45,6 +47,8 @@ public class SysLogLoginController {
public Result<PageData<SysLogLoginDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){ public Result<PageData<SysLogLoginDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
PageData<SysLogLoginDTO> page = sysLogLoginService.page(params); PageData<SysLogLoginDTO> page = sysLogLoginService.page(params);
log.info("PageData<SysLogLoginDTO>====={}",page.toString());
return new Result<PageData<SysLogLoginDTO>>().ok(page); return new Result<PageData<SysLogLoginDTO>>().ok(page);
} }

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -1,14 +1,20 @@
package com.cnbm.admin.service.impl; package com.cnbm.admin.service.impl;
import com.cnbm.admin.entity.LoginUser; 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.params.LoginParam;
import com.cnbm.admin.service.CaptchaService; import com.cnbm.admin.service.CaptchaService;
import com.cnbm.admin.service.SysLogLoginService;
import com.cnbm.admin.utils.JwtUtil; import com.cnbm.admin.utils.JwtUtil;
import com.cnbm.admin.service.LoginService; import com.cnbm.admin.service.LoginService;
import com.cnbm.admin.utils.ResponseResult; import com.cnbm.admin.utils.ResponseResult;
import com.cnbm.common.exception.ErrorCode; import com.cnbm.common.exception.ErrorCode;
import com.cnbm.common.utils.IpUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
@ -16,6 +22,7 @@ import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -37,6 +44,9 @@ public class LoginServiceImpl implements LoginService {
@Autowired @Autowired
private CaptchaService captchaService; private CaptchaService captchaService;
@Autowired
private SysLogLoginService sysLogLoginService;
@Override @Override
public ResponseResult login(HttpServletRequest request, LoginParam loginParam) { public ResponseResult login(HttpServletRequest request, LoginParam loginParam) {
//验证码是否正确 //验证码是否正确
@ -46,7 +56,18 @@ public class LoginServiceImpl implements LoginService {
} }
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginParam.getUsername(),loginParam.getPassword()); UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginParam.getUsername(),loginParam.getPassword());
Authentication authenticate = authenticationManager.authenticate(authenticationToken); 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)) { if (Objects.isNull(authenticate)) {
log.setStatus(LoginStatusEnum.FAIL.value());
log.setCreatorName(loginParam.getUsername());
sysLogLoginService.save(log);
throw new RuntimeException("登录失败"); throw new RuntimeException("登录失败");
} }
//如果认证通过了使用userid生成一个jwt jwt存入ResponseResult返回 //如果认证通过了使用userid生成一个jwt jwt存入ResponseResult返回
@ -57,6 +78,11 @@ public class LoginServiceImpl implements LoginService {
map.put("token",jwt); map.put("token",jwt);
//把完整的用户信息存入redis userid作为key //把完整的用户信息存入redis userid作为key
redisTemplate.opsForValue().set("login:"+userid,loginUser); 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); return new ResponseResult(200,"登录成功",map);
} }

View File

@ -10,6 +10,7 @@ import com.cnbm.common.constant.Constant;
import com.cnbm.common.page.PageData; import com.cnbm.common.page.PageData;
import com.cnbm.common.service.impl.BaseServiceImpl; import com.cnbm.common.service.impl.BaseServiceImpl;
import com.cnbm.common.utils.ConvertUtils; import com.cnbm.common.utils.ConvertUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -18,6 +19,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
@Service @Service
@Log4j2
public class SysLogLoginServiceImpl extends BaseServiceImpl<SysLogLoginDao, SysLogLoginEntity> implements SysLogLoginService { public class SysLogLoginServiceImpl extends BaseServiceImpl<SysLogLoginDao, SysLogLoginEntity> implements SysLogLoginService {
@Override @Override
@ -26,6 +28,7 @@ public class SysLogLoginServiceImpl extends BaseServiceImpl<SysLogLoginDao, SysL
getPage(params, Constant.CREATE_DATE, false), getPage(params, Constant.CREATE_DATE, false),
getWrapper(params) getWrapper(params)
); );
log.info("page======{}",page.toString());
return getPageData(page, SysLogLoginDTO.class); return getPageData(page, SysLogLoginDTO.class);
} }
@ -44,7 +47,6 @@ public class SysLogLoginServiceImpl extends BaseServiceImpl<SysLogLoginDao, SysL
QueryWrapper<SysLogLoginEntity> wrapper = new QueryWrapper<>(); QueryWrapper<SysLogLoginEntity> wrapper = new QueryWrapper<>();
wrapper.eq(StringUtils.isNotBlank(status), "status", status); wrapper.eq(StringUtils.isNotBlank(status), "status", status);
wrapper.like(StringUtils.isNotBlank(creatorName), "creator_name", creatorName); wrapper.like(StringUtils.isNotBlank(creatorName), "creator_name", creatorName);
return wrapper; return wrapper;
} }

View File

@ -5,23 +5,24 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cnbm.admin.dto.SysMenuDTO; import com.cnbm.admin.dto.SysMenuDTO;
import com.cnbm.admin.dto.SysUserDTO; import com.cnbm.admin.dto.SysUserDTO;
import com.cnbm.admin.entity.LoginUser; import com.cnbm.admin.entity.LoginUser;
import com.cnbm.admin.entity.SysLogLoginEntity;
import com.cnbm.admin.entity.SysMenuEntity; import com.cnbm.admin.entity.SysMenuEntity;
import com.cnbm.admin.entity.SysUserEntity; import com.cnbm.admin.entity.SysUserEntity;
import com.cnbm.admin.enums.LoginOperationEnum;
import com.cnbm.admin.exception.LoginStatusException; import com.cnbm.admin.exception.LoginStatusException;
import com.cnbm.admin.service.SysMenuService; import com.cnbm.admin.service.SysMenuService;
import com.cnbm.common.utils.ConvertUtils; import com.cnbm.common.utils.ConvertUtils;
import com.cnbm.common.utils.IpUtils;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.security.authentication.DisabledException; import org.springframework.security.authentication.DisabledException;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -45,6 +46,7 @@ public class UserDetailsServiceImpl implements UserDetailsService {
LambdaQueryWrapper<SysUserEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<SysUserEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(SysUserEntity::getUsername,username); lambdaQueryWrapper.eq(SysUserEntity::getUsername,username);
SysUserEntity sysUserEntity = userDao.selectOne(lambdaQueryWrapper); SysUserEntity sysUserEntity = userDao.selectOne(lambdaQueryWrapper);
if (Objects.isNull(sysUserEntity)) { if (Objects.isNull(sysUserEntity)) {
throw new UsernameNotFoundException("用户名不存在"); throw new UsernameNotFoundException("用户名不存在");
} }

View File

@ -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;
}
}
}

View File

@ -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;
}
}

View File

@ -20,10 +20,36 @@ spring:
# url: jdbc:postgresql://192.168.10.10:5432/postgres # url: jdbc:postgresql://192.168.10.10:5432/postgres
# username: postgres # username: postgres
# password: 123456 # password: 123456
hikari: initial-size: 10
pool-name: GrowUpHikariCP max-active: 100
minimum-idle: 1 min-idle: 10
maximum-pool-size: 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 ##多数据源的配置需要引用renren-dynamic-datasource
#dynamic: #dynamic:

View File

@ -69,12 +69,12 @@ mybatis-plus:
cache-enabled: false cache-enabled: false
call-setters-on-nulls: true call-setters-on-nulls: true
jdbc-type-for-null: 'null' jdbc-type-for-null: 'null'
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
configuration-properties: configuration-properties:
prefix: prefix:
blobType: BLOB blobType: BLOB
boolValue: TRUE boolValue: TRUE
logging: #logging:
level: # level:
com.cnbm.admin.dao: DEBUG # com.cnbm.admin.dao: DEBUG

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<logger name="org.springframework.web" level="INFO"/>
<logger name="org.springboot.sample" level="TRACE" />
<!-- 开发、测试环境 -->
<springProfile name="dev,test">
<logger name="org.springframework.web" level="INFO"/>
<logger name="org.springboot.sample" level="INFO" />
<logger name="com.cnbm" level="DEBUG" />
</springProfile>
<!-- 生产环境 -->
<springProfile name="prod">
<logger name="org.springframework.web" level="ERROR"/>
<logger name="org.springboot.sample" level="ERROR" />
<logger name="com.cnbm" level="ERROR" />
</springProfile>
</configuration>