[fix] 修改了认证链
This commit is contained in:
parent
1fae1e971e
commit
4923396996
|
|
@ -26,12 +26,17 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
|||
String token = resolveToken(request);
|
||||
|
||||
// 2. 验证 Token 是否有效
|
||||
if (token != null && jwtTokenProvider.validateToken(token)) {
|
||||
// 3. 如果 Token 有效,获取认证信息
|
||||
Authentication authentication = jwtTokenProvider.getAuthentication(token);
|
||||
|
||||
// 4. 将认证信息设置到 SecurityContext 中
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
if (token != null) {
|
||||
if (jwtTokenProvider.validateToken(token)) {
|
||||
// Token 验证成功
|
||||
Authentication authentication = jwtTokenProvider.getAuthentication(token);
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
} else {
|
||||
// Token 无效,记录日志
|
||||
logger.warn("Invalid JWT token");
|
||||
}
|
||||
} else {
|
||||
logger.warn("JWT token not found in request");
|
||||
}
|
||||
|
||||
// 5. 继续执行过滤器链
|
||||
|
|
|
|||
|
|
@ -45,26 +45,39 @@ public class SecurityConfig {
|
|||
.cors(cors -> cors.configurationSource(corsConfigurationSource())) // 配置 CORS
|
||||
.authorizeHttpRequests(authz -> authz
|
||||
.requestMatchers("/api/auth/**").permitAll()
|
||||
.requestMatchers("/static/**","/assets/**").permitAll()
|
||||
.requestMatchers("/", "/login","/index.html","/favicon.svg").permitAll() // 确保允许访问根路径和登录路径
|
||||
.anyRequest().authenticated())
|
||||
.authenticationProvider(authenticationProvider())
|
||||
.addFilterBefore((Filter) new JwtAuthenticationFilter(jwtTokenProvider), UsernamePasswordAuthenticationFilter.class);
|
||||
|
||||
return http.build();
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public CorsConfigurationSource corsConfigurationSource() {
|
||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||
CorsConfiguration config = new CorsConfiguration();
|
||||
config.setAllowCredentials(true);
|
||||
Collections.singletonList("*");
|
||||
config.setAllowedOrigins(Arrays.asList("http://localhost:8848")); // 替换为允许的源
|
||||
config.setAllowedOrigins(Arrays.asList("http://localhost:9527", "http://localhost:8080","http://154.219.110.17:8080", "https://test.tju.edu.kg"));
|
||||
config.setAllowedHeaders(Arrays.asList("*"));
|
||||
config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
|
||||
source.registerCorsConfiguration("/**", config);
|
||||
return source;
|
||||
}
|
||||
|
||||
|
||||
/* @Bean
|
||||
public CorsConfigurationSource corsConfigurationSource() {
|
||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||
CorsConfiguration config = new CorsConfiguration();
|
||||
config.setAllowCredentials(true);
|
||||
config.setAllowedOrigins(Arrays.asList("http://localhost:9527","http://154.219.110.17:8080","https://test.tju.edu.kg")); // 替换为允许的源
|
||||
config.setAllowedOrigins(Collections.singletonList("*")); // 允许所有源
|
||||
config.setAllowedHeaders(Arrays.asList("*")); // 允许的头
|
||||
config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS")); // 允许的方法
|
||||
source.registerCorsConfiguration("/**", config);
|
||||
return source;
|
||||
}
|
||||
}*/
|
||||
|
||||
@Bean
|
||||
public UserDetailsService userDetailsService() {
|
||||
|
|
|
|||
|
|
@ -2,18 +2,24 @@ package com.waterquality.projectmanagement.controller;
|
|||
|
||||
import com.waterquality.projectmanagement.Response;
|
||||
import com.waterquality.projectmanagement.dto.EmployeeDTO;
|
||||
import com.waterquality.projectmanagement.dto.order.WorkOrderVO;
|
||||
import com.waterquality.projectmanagement.entity.employee.CustomUserDetails;
|
||||
import com.waterquality.projectmanagement.entity.employee.Employee;
|
||||
import com.waterquality.projectmanagement.entity.order.WorkOrderStatus;
|
||||
import com.waterquality.projectmanagement.service.EmployeeService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.web.PageableDefault;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/employees")
|
||||
|
|
@ -44,4 +50,24 @@ public class EmployeeController {
|
|||
List<Employee> allEmployees = employeeService.getAllEmployees();
|
||||
return ResponseEntity.ok(Response.newSuccess(allEmployees));
|
||||
}
|
||||
|
||||
// 根据ID获取员工详细信息
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<Response<Employee>> getEmployeeById(@PathVariable Long id) {
|
||||
Employee employee = employeeService.getEmployeeById(Math.toIntExact(id));
|
||||
if (employee != null) {
|
||||
return ResponseEntity.ok(Response.newSuccess(employee));
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
|
||||
// 根据token获取信息:
|
||||
@GetMapping("/getUserInfo")
|
||||
public ResponseEntity<Response<Employee>>getUserInfo(
|
||||
@RequestParam(required = false) Set<WorkOrderStatus> statuses,
|
||||
@AuthenticationPrincipal CustomUserDetails user){
|
||||
return ResponseEntity.ok(Response.newSuccess(
|
||||
employeeService.getEmployeeById(user.getUserID())));
|
||||
}
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ package com.waterquality.projectmanagement.controller;
|
|||
import com.waterquality.projectmanagement.Response;
|
||||
import com.waterquality.projectmanagement.dto.facility.FacilityCreateDTO;
|
||||
import com.waterquality.projectmanagement.dto.facility.FacilityVO;
|
||||
import com.waterquality.projectmanagement.entity.facility.Facility;
|
||||
import com.waterquality.projectmanagement.entity.facility.FacilityStatus;
|
||||
import com.waterquality.projectmanagement.entity.facility.FacilityType;
|
||||
import com.waterquality.projectmanagement.service.FacilityService;
|
||||
|
|
@ -47,6 +48,12 @@ public class FacilityController {
|
|||
facilityService.searchFacilities(type, status, area, pageable)));
|
||||
}
|
||||
|
||||
@GetMapping("/list")
|
||||
public ResponseEntity<Response<List<FacilityVO>>> getAllFacility() {
|
||||
List<FacilityVO> allFacility = facilityService.getAllFacility();
|
||||
return ResponseEntity.ok(Response.newSuccess(allFacility));
|
||||
}
|
||||
|
||||
|
||||
@PatchMapping("/{id}/status")
|
||||
public ResponseEntity<Response<Void>> updateStatus(
|
||||
|
|
|
|||
|
|
@ -1,15 +1,27 @@
|
|||
package com.waterquality.projectmanagement.controller;
|
||||
|
||||
import com.waterquality.projectmanagement.Response;
|
||||
import com.waterquality.projectmanagement.dto.order.WorkOrderVO;
|
||||
import com.waterquality.projectmanagement.dto.plan.InspectionPlanCreateDTO;
|
||||
import com.waterquality.projectmanagement.dto.plan.InspectionPlanVO;
|
||||
import com.waterquality.projectmanagement.entity.employee.CustomUserDetails;
|
||||
import com.waterquality.projectmanagement.entity.order.WorkOrderStatus;
|
||||
import com.waterquality.projectmanagement.entity.plan.InspectionPlan;
|
||||
import com.waterquality.projectmanagement.entity.plan.PlanStatus;
|
||||
import com.waterquality.projectmanagement.service.InspectionPlanService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.web.PageableDefault;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
// InspectionPlanController.java
|
||||
@RestController
|
||||
|
|
@ -30,4 +42,29 @@ public class InspectionPlanController {
|
|||
@PathVariable Integer employeeId) {
|
||||
return ResponseEntity.ok(Response.newSuccess(planService.getPlansByEmployee(employeeId)));
|
||||
}
|
||||
|
||||
@GetMapping("/employee/my-plan")
|
||||
public ResponseEntity<Response<List<InspectionPlanVO>>> getMyPlan(
|
||||
@RequestParam(required = false) Set<PlanStatus> statuses,
|
||||
@AuthenticationPrincipal CustomUserDetails user,
|
||||
@PageableDefault Pageable pageable) {
|
||||
List<InspectionPlanVO> planList = planService.getPlanByAssignee(
|
||||
user.getUserID(),
|
||||
statuses != null ? statuses : EnumSet.allOf(PlanStatus.class),
|
||||
pageable
|
||||
).stream()
|
||||
.map(this::convertToVO)
|
||||
.collect(Collectors.toList());;
|
||||
return ResponseEntity.ok(Response.newSuccess(planList));
|
||||
}
|
||||
|
||||
private InspectionPlanVO convertToVO(InspectionPlan inspectionPlan) {
|
||||
InspectionPlanVO planVO = new InspectionPlanVO();
|
||||
planVO.setPlanId(inspectionPlan.getPlanId());
|
||||
planVO.setArea(inspectionPlan.getArea());
|
||||
planVO.setStatus(inspectionPlan.getStatus());
|
||||
planVO.setCreatedAt(inspectionPlan.getCreatedAt());
|
||||
|
||||
return planVO;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,9 @@
|
|||
package com.waterquality.projectmanagement.service;
|
||||
|
||||
import com.waterquality.projectmanagement.dto.EmployeeDTO;
|
||||
import com.waterquality.projectmanagement.dto.order.WorkOrderVO;
|
||||
import com.waterquality.projectmanagement.entity.employee.Employee;
|
||||
import com.waterquality.projectmanagement.entity.order.WorkOrderStatus;
|
||||
import com.waterquality.projectmanagement.exception.ResourceNotFoundException;
|
||||
import com.waterquality.projectmanagement.repository.EmployeeRepository;
|
||||
import com.waterquality.projectmanagement.repository.DepartmentRepository;
|
||||
|
|
@ -13,6 +15,7 @@ import org.springframework.stereotype.Service;
|
|||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
|
|
|
|||
|
|
@ -15,7 +15,6 @@ import org.springframework.stereotype.Service;
|
|||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
// FacilityService.java
|
||||
|
|
@ -26,6 +25,13 @@ public class FacilityService {
|
|||
|
||||
private final FacilityRepository facilityRepository;
|
||||
|
||||
public List<FacilityVO> getAllFacility() {
|
||||
List<Facility> facilities = facilityRepository.findAll();
|
||||
return facilities.stream()
|
||||
.map(this::convertToVO)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public FacilityVO createFacility(FacilityCreateDTO dto) {
|
||||
// 校验编码唯一性
|
||||
|
|
|
|||
|
|
@ -4,12 +4,16 @@ import com.waterquality.projectmanagement.dto.plan.InspectionPlanCreateDTO;
|
|||
import com.waterquality.projectmanagement.dto.plan.InspectionPlanVO;
|
||||
import com.waterquality.projectmanagement.entity.employee.Employee;
|
||||
import com.waterquality.projectmanagement.entity.plan.InspectionPlan;
|
||||
import com.waterquality.projectmanagement.entity.plan.PlanStatus;
|
||||
import com.waterquality.projectmanagement.repository.InspectionPlanRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
// InspectionPlanService.java
|
||||
|
|
@ -20,6 +24,10 @@ public class InspectionPlanService {
|
|||
private final InspectionPlanRepository planRepository;
|
||||
private final EmployeeService employeeService;
|
||||
|
||||
public List<InspectionPlan> getPlanByAssignee(Integer userID, Set<PlanStatus> planStatuses, Pageable pageable) {
|
||||
return planRepository.findByEmployeeEmployeeId(userID);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public InspectionPlanVO createPlan(InspectionPlanCreateDTO dto) {
|
||||
Employee employee = employeeService.getEmployeeById(dto.getEmployeeId());
|
||||
|
|
|
|||
|
|
@ -9,4 +9,5 @@ jwt.secret=your_secret_key
|
|||
jwt.expiration=3600000
|
||||
|
||||
|
||||
server.port=8080
|
||||
server.port=8080
|
||||
logging.level.org.springframework.security=DEBUG
|
||||
Loading…
Reference in New Issue