告警功能完善:告警分级(一般 / 紧急)、告警推送(Web/APP 消息提醒)、告警历史查询接口;pull/43/head
parent
85fe0bd79b
commit
8d19836ea0
@ -0,0 +1,55 @@
|
||||
// com/campus/water/security/JwtAuthenticationFilter.java
|
||||
package com.campus.water.security;
|
||||
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* JWT认证拦截器
|
||||
*/
|
||||
@Component
|
||||
public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
||||
|
||||
@Autowired
|
||||
private JwtTokenProvider jwtTokenProvider;
|
||||
|
||||
@Autowired
|
||||
private UserDetailsServiceImpl userDetailsService;
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
|
||||
throws ServletException, IOException {
|
||||
try {
|
||||
// 提取JWT令牌
|
||||
String jwt = jwtTokenProvider.getJwtFromRequest(request);
|
||||
|
||||
if (jwt != null && jwtTokenProvider.validateJwtToken(jwt)) {
|
||||
// 获取用户名并加载用户信息
|
||||
String username = jwtTokenProvider.getUsernameFromJwtToken(jwt);
|
||||
UserDetails userDetails = userDetailsService.loadUserByUsername(username);
|
||||
|
||||
// 设置认证信息
|
||||
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
|
||||
userDetails, null, userDetails.getAuthorities());
|
||||
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
|
||||
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("认证失败: ", e);
|
||||
}
|
||||
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
// com/campus/water/security/RoleConstants.java
|
||||
package com.campus.water.security;
|
||||
|
||||
/**
|
||||
* 角色常量定义
|
||||
*/
|
||||
public class RoleConstants {
|
||||
/** 学生角色 */
|
||||
public static final String ROLE_STUDENT = "ROLE_STUDENT";
|
||||
/** 维修人员角色 */
|
||||
public static final String ROLE_REPAIRMAN = "ROLE_REPAIRMAN";
|
||||
/** 管理员角色 */
|
||||
public static final String ROLE_ADMIN = "ROLE_ADMIN";
|
||||
|
||||
private RoleConstants() {}
|
||||
}
|
||||
@ -0,0 +1,79 @@
|
||||
// com/campus/water/security/UserDetailsServiceImpl.java
|
||||
package com.campus.water.security;
|
||||
|
||||
import com.campus.water.entity.po.AdminPO;
|
||||
import com.campus.water.entity.po.RepairerAuthPO;
|
||||
import com.campus.water.entity.po.UserPO;
|
||||
import com.campus.water.mapper.AdminRepository;
|
||||
import com.campus.water.mapper.RepairerAuthRepository;
|
||||
import com.campus.water.mapper.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
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.Collections;
|
||||
|
||||
/**
|
||||
* 加载用户权限信息
|
||||
*/
|
||||
@Service
|
||||
public class UserDetailsServiceImpl implements UserDetailsService {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository; // 学生用户仓库
|
||||
|
||||
@Autowired
|
||||
private AdminRepository adminRepository; // 管理员仓库
|
||||
|
||||
@Autowired
|
||||
private RepairerAuthRepository repairerAuthRepository; // 维修人员仓库
|
||||
|
||||
@Override
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
// 1. 尝试查询学生用户
|
||||
UserPO student = userRepository.findByUsername(username).orElse(null);
|
||||
if (student != null) {
|
||||
return createUserDetails(
|
||||
student.getUsername(),
|
||||
student.getPassword(),
|
||||
RoleConstants.ROLE_STUDENT
|
||||
);
|
||||
}
|
||||
|
||||
// 2. 尝试查询管理员用户
|
||||
AdminPO admin = adminRepository.findByUsername(username).orElse(null);
|
||||
if (admin != null) {
|
||||
return createUserDetails(
|
||||
admin.getUsername(),
|
||||
admin.getPassword(),
|
||||
RoleConstants.ROLE_ADMIN
|
||||
);
|
||||
}
|
||||
|
||||
// 3. 尝试查询维修人员用户
|
||||
RepairerAuthPO repairer = repairerAuthRepository.findByUsername(username).orElse(null);
|
||||
if (repairer != null) {
|
||||
return createUserDetails(
|
||||
repairer.getUsername(),
|
||||
repairer.getPassword(),
|
||||
RoleConstants.ROLE_REPAIRMAN
|
||||
);
|
||||
}
|
||||
|
||||
// 所有类型用户都不存在
|
||||
throw new UsernameNotFoundException("用户不存在: " + username);
|
||||
}
|
||||
|
||||
// 构建UserDetails对象的工具方法
|
||||
private UserDetails createUserDetails(String username, String password, String role) {
|
||||
return new User(
|
||||
username,
|
||||
password,
|
||||
Collections.singletonList(new SimpleGrantedAuthority(role))
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue