redis调整

This commit is contained in:
2025-07-28 09:40:40 +08:00
parent 7336a0aaa8
commit 46a2e2068b
5 changed files with 74 additions and 36 deletions

View File

@@ -18,6 +18,12 @@
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>com.njcn</groupId>
<artifactId>common-redis</artifactId>
<version>1.0.0</version>
</dependency>
<!-- <dependency>
<groupId>com.njcn</groupId>

View File

@@ -1,15 +1,19 @@
package com.njcn.gather.event.transientes.security;
import cn.hutool.json.JSONObject;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.PubUtils;
import com.njcn.gather.event.transientes.utils.JwtUtil;
import com.njcn.redis.utils.RedisUtil;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.HttpResultUtil;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@@ -23,13 +27,22 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
@RequiredArgsConstructor
public class AuthController extends BaseController {
@Autowired
private AuthenticationManager authenticationManager;
private final String eventRedisKey = "event_smart_";
private final AuthenticationManager authenticationManager;
private final JwtUtil jwtUtil;
private final RedisUtil redisUtil;
@Autowired
private JwtUtil jwtUtil;
@PostMapping("/cn_authenticate")
@ApiOperation("登录认证")
@@ -37,10 +50,12 @@ public class AuthController extends BaseController {
String methodDescribe = getMethodDescribe("createAuthenticationToken");
//log.info("Authentication request - username: {}, password: {}",authRequest.getUsername(),authRequest.getPassword());
try {
boolean hasFlag = redisUtil.hasKey(eventRedisKey+authRequest.getUsername());
if(hasFlag){
String pass = redisUtil.getRawValue(eventRedisKey+authRequest.getUsername());
// 执行认证,内部会调用 UserDetailsService 加载用户信息
Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(authRequest.getUsername(), authRequest.getPassword())
);
Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(authRequest.getUsername(),pass));
// 将认证信息存入 SecurityContext
SecurityContextHolder.getContext().setAuthentication(authentication);
@@ -57,6 +72,9 @@ public class AuthController extends BaseController {
authResponse.setToken(jwt);
authResponse.setDeptId(department);
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, authResponse, methodDescribe);
}else {
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
}
} catch (Exception e) {
e.printStackTrace();
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);

View File

@@ -1,12 +1,15 @@
package com.njcn.gather.event.transientes.security;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.gather.event.transientes.mapper.PqsUserMapper;
import com.njcn.gather.event.transientes.mapper.PqsUserSetMapper;
import com.njcn.gather.event.transientes.pojo.po.PqsUser;
import com.njcn.gather.event.transientes.pojo.po.PqsUserSet;
import com.njcn.redis.utils.RedisUtil;
import lombok.RequiredArgsConstructor;
import netscape.javascript.JSObject;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
@@ -26,40 +29,36 @@ public class MyUserDetailsService implements UserDetailsService {
private final PqsUserSetMapper pqsUserSetMapper;
private final RedisUtil redisUtil;
@Override
public MyUserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
/* LambdaQueryWrapper<PqsUser> userWrapper = new LambdaQueryWrapper<>();
userWrapper.eq(PqsUser::getLoginname,username);
PqsUser pqsUser = pqsUserMapper.selectOne(userWrapper);
if(Objects.isNull(pqsUser)){
throw new UsernameNotFoundException("User not found with username: " + username);
}
LambdaQueryWrapper<PqsUserSet> userSetWrapper = new LambdaQueryWrapper<>();
userSetWrapper.eq(PqsUserSet::getUserIndex,pqsUser.getUserIndex());
PqsUserSet pqsUserSet = pqsUserSetMapper.selectOne(userSetWrapper);
String deptId = pqsUserSet.getDeptsIndex();*/
if(redisUtil.hasKey("event_smart_"+username)){
String password = redisUtil.getRawValue("event_smart_"+username);
// 这里应该从数据库中获取用户信息,本示例使用硬编码用户
if ("cdf".equals(username)) {
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String encodedPassword = passwordEncoder.encode("@#001njcnpqs");
return new MyUserDetails("1","cdf", encodedPassword,"10001",
new ArrayList<>());
}else if("screen".equals(username)){
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String encodedPassword = passwordEncoder.encode("@#001njcnpqs");
return new MyUserDetails("2","screen", encodedPassword,"10001",
new ArrayList<>());
} else if("test".equals(username)){
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String encodedPassword = passwordEncoder.encode("@#001njcnpqs");
return new MyUserDetails("3","test", encodedPassword,"10001",
String encodedPassword = passwordEncoder.encode(password);
LambdaQueryWrapper<PqsUser> userWrapper = new LambdaQueryWrapper<>();
userWrapper.eq(PqsUser::getLoginname,username);
PqsUser pqsUser = pqsUserMapper.selectOne(userWrapper);
if(Objects.isNull(pqsUser)){
throw new UsernameNotFoundException("User not found with username: " + username);
}
LambdaQueryWrapper<PqsUserSet> userSetWrapper = new LambdaQueryWrapper<>();
userSetWrapper.eq(PqsUserSet::getUserIndex,pqsUser.getUserIndex());
PqsUserSet pqsUserSet = pqsUserSetMapper.selectOne(userSetWrapper);
String deptId = pqsUserSet.getDeptsIndex();
return new MyUserDetails(pqsUser.getUserIndex(),pqsUser.getLoginname(), encodedPassword,deptId,
new ArrayList<>());
}else {
throw new UsernameNotFoundException("User not found with username: " + username);

View File

@@ -593,11 +593,11 @@ public class LargeScreenCountServiceImpl implements LargeScreenCountService {
if(!CollectionUtils.isEmpty(eveIdndex)){
temp =msgEventInfoService.getMsgByIds(eveIdndex);
}
// List<EventDetailVO> change = change(eventdetails,temp);
List<EventDetailVO> change = change(eventdetails,temp);
// mapCountVO.setEventList(change);
mapCountVO.setEventList(change);
mapCountVO.setNoticeCount(temp.size());
// mapCountVO.setNoticeList(temp);
mapCountVO.setNoticeList(temp);
result.add(mapCountVO);
});
return result;

View File

@@ -37,13 +37,28 @@ spring:
#url: jdbc:oracle:thin:@192.168.10.34:11521:pqsbase
#username: pqsadmin
#password: Pqsadmin_123
driver-class-name: oracle.jdbc.driver.OracleDriver
driver-class-name: oracle.jdbc.OracleDriver
# salve:
# driver-class-name: dm.jdbc.driver.DmDriver
# url: jdbc:dm://192.168.1.21:5236/PQSADMIN?useUnicode=true&characterEncoding=utf-8
# username: PQSADMINLN
# password: Pqsadmin123
redis:
database: 10
host: localhost
port: 6379
timeout: 5000
lettuce:
pool:
max-active: 8
max-wait: -1
max-idle: 8
min-idle: 0
#mybatis配置信息
mybatis-plus:
mapper-locations: classpath*:com/njcn/**/mapping/*.xml