实体提交

This commit is contained in:
2025-06-26 15:08:36 +08:00
parent 8103b45a61
commit 118068bafe
7 changed files with 204 additions and 14 deletions

View File

@@ -0,0 +1,9 @@
package com.njcn.gather.event.transientes.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.njcn.gather.event.transientes.pojo.po.PqsUser;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface PqsUserMapper extends BaseMapper<PqsUser> {
}

View File

@@ -0,0 +1,58 @@
package com.njcn.gather.event.transientes.pojo.po;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.util.Date;
/**
* @Author: cdf
* @CreateTime: 2025-06-26
* @Description:
*/
@Data
@TableName("PQS_USER")
public class PqsUser {
@TableId(type = IdType.INPUT)
private String userIndex;
private String name;
private String loginname;
private String password;
private String phone;
private String email;
@TableField(fill = FieldFill.INSERT)
private Date registertime;
private Date psdvalidity;
private Date logintime;
private Integer state;
private Integer mark;
private String limitIpstart;
private String limitIpend;
private String limitTime;
private Integer loginErrorTimes;
@TableField("CASUAL_USER")
private Integer casualUser;
private Date firsterrorTime;
private Date lockTime;
private String referralCode;
}

View File

@@ -13,7 +13,7 @@ import java.io.Serializable;
* @Description:
*/
@Data
@TableName("PQSADMIN_BJ.PQS_USERSET")
@TableName("PQS_USERSET")
public class PqsUserSet implements Serializable {
private static final long serialVersionUID = 1L;

View File

@@ -13,6 +13,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.PostMapping;
@@ -26,28 +28,39 @@ public class AuthController extends BaseController {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private JwtUtil jwtUtil;
@PostMapping("/cn_authenticate")
@ApiOperation("登录认证")
public HttpResult<String> createAuthenticationToken(@RequestBody AuthRequest authRequest) {
public HttpResult<AuthResponse> createAuthenticationToken(@RequestBody AuthRequest authRequest) {
String methodDescribe = getMethodDescribe("createAuthenticationToken");
log.info("Authentication request - username: {}, password: {}",authRequest.getUsername(),authRequest.getPassword());
try {
authenticationManager.authenticate(
// 执行认证,内部会调用 UserDetailsService 加载用户信息
Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(authRequest.getUsername(), authRequest.getPassword())
);
} catch (BadCredentialsException e) {
// 将认证信息存入 SecurityContext
SecurityContextHolder.getContext().setAuthentication(authentication);
// 直接从 Authentication 对象中获取已加载的 UserDetails避免重复查询
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
// 获取用户部门(假设 CustomUserDetails 包含部门信息)
String department = ((MyUserDetails) userDetails).getDeptId();
final String jwt = jwtUtil.generateToken(userDetails);
AuthResponse authResponse = new AuthResponse();
authResponse.setToken(jwt);
authResponse.setDeptId(department);
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, authResponse, methodDescribe);
} catch (Exception e) {
e.printStackTrace();
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
}
final UserDetails userDetails = userDetailsService.loadUserByUsername(authRequest.getUsername());
final String jwt = jwtUtil.generateToken(userDetails);
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, jwt, methodDescribe);
}
}

View File

@@ -0,0 +1,20 @@
package com.njcn.gather.event.transientes.security;
import lombok.Data;
/**
* @Author: cdf
* @CreateTime: 2025-06-26
* @Description:
*/
@Data
public class AuthResponse {
private String token;
private String deptId;
private String roleId;
private String userIndex;
}

View File

@@ -0,0 +1,63 @@
package com.njcn.gather.event.transientes.security;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection;
/**
* @Author: cdf
* @CreateTime: 2025-06-26
* @Description:
*/
@Data
public class MyUserDetails implements UserDetails {
private String userId; // 用户唯一标识
private String username; // 用户名
private String password; // 密码
private String deptId; // 部门信息
private Collection<? extends GrantedAuthority> authorities; // 权限集合
private boolean accountNonExpired; // 账户是否未过期
private boolean accountNonLocked; // 账户是否未锁定
private boolean credentialsNonExpired; // 凭证是否未过期
private boolean enabled; // 账户是否启用
public MyUserDetails(String username, String password, String deptId,Collection<? extends GrantedAuthority> authorities) {
this.username = username;
this.password = password;
this.deptId = deptId;
this.authorities = authorities;
}
@Override
public String getPassword() {
return this.password;
}
@Override
public String getUsername() {
return this.username;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}

View File

@@ -1,5 +1,11 @@
package com.njcn.gather.event.transientes.security;
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 lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
@@ -10,29 +16,50 @@ import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Objects;
@Service
@RequiredArgsConstructor
public class MyUserDetailsService implements UserDetailsService {
private final PqsUserMapper pqsUserMapper;
private final PqsUserSetMapper pqsUserSetMapper;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
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 ("cdf".equals(username)) {
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String encodedPassword = passwordEncoder.encode("@#001njcnpqs");
return new User("cdf", encodedPassword,
return new MyUserDetails("cdf", encodedPassword,"10001",
new ArrayList<>());
}else if("screen".equals(username)){
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String encodedPassword = passwordEncoder.encode("@#001njcnpqs");
return new User("screen", encodedPassword,
return new MyUserDetails("screen", encodedPassword,"10001",
new ArrayList<>());
} else if("test".equals(username)){
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String encodedPassword = passwordEncoder.encode("@#001njcnpqs");
return new User("test", encodedPassword,
return new MyUserDetails("test", encodedPassword,"10001",
new ArrayList<>());
}else {
throw new UsernameNotFoundException("User not found with username: " + username);