员工浏览自己的揽件任务spring boot 代码
时间: 2023-07-29 08:14:46 浏览: 111
以下是一个简单的员工浏览自己的揽件任务的Spring Boot代码示例:
1. 定义揽件任务实体类
```java
@Entity
@Table(name = "pickup_task")
public class PickupTask {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "employee_id")
private Long employeeId;
@Column(name = "task_status")
private String taskStatus;
@Column(name = "task_description")
private String taskDescription;
// 省略getter和setter方法
}
```
2. 定义揽件任务Repository接口
```java
@Repository
public interface PickupTaskRepository extends JpaRepository<PickupTask, Long> {
List<PickupTask> findByEmployeeId(Long employeeId);
}
```
3. 定义揽件任务Service
```java
@Service
public class PickupTaskService {
@Autowired
private PickupTaskRepository pickupTaskRepository;
public List<PickupTask> getTasksByEmployeeId(Long employeeId) {
return pickupTaskRepository.findByEmployeeId(employeeId);
}
}
```
4. 定义揽件任务Controller
```java
@RestController
@RequestMapping("/api/pickup-tasks")
public class PickupTaskController {
@Autowired
private PickupTaskService pickupTaskService;
@GetMapping("/employee/{employeeId}")
public List<PickupTask> getTasksByEmployeeId(@PathVariable Long employeeId) {
return pickupTaskService.getTasksByEmployeeId(employeeId);
}
}
```
5. 配置JWT身份认证
```java
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
@Autowired
private JwtRequestFilter jwtRequestFilter;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(jwtUserDetailsService).passwordEncoder(passwordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.csrf().disable()
.authorizeRequests().antMatchers("/api/authenticate").permitAll()
.anyRequest().authenticated().and()
.exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint).and().sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
}
}
```
以上仅为示例代码,实际开发中还需要根据具体需求进行修改和调整。同时,还需要按照实际情况进行部署和测试。
阅读全文