From da8aaaf639826e0f2e7825b31ebf44bcd83f129c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=87=95=E9=B9=8F?= Date: Thu, 29 Oct 2020 17:56:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/demo/DemoApplication.java | 61 +-------------- .../demo/DemoApplicationConfiguration.java | 58 -------------- .../java/com/example/demo/SecurityUtil.java | 77 ------------------- .../demo/controller/AdminController.java | 6 ++ 4 files changed, 9 insertions(+), 193 deletions(-) delete mode 100644 src/main/java/com/example/demo/DemoApplicationConfiguration.java delete mode 100644 src/main/java/com/example/demo/SecurityUtil.java diff --git a/src/main/java/com/example/demo/DemoApplication.java b/src/main/java/com/example/demo/DemoApplication.java index 73fa6a2..d1f5369 100644 --- a/src/main/java/com/example/demo/DemoApplication.java +++ b/src/main/java/com/example/demo/DemoApplication.java @@ -10,14 +10,14 @@ import org.activiti.api.task.runtime.events.TaskAssignedEvent; import org.activiti.api.task.runtime.events.TaskCompletedEvent; import org.activiti.api.task.runtime.events.listener.TaskRuntimeEventListener; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @Slf4j -@SpringBootApplication -public class DemoApplication implements CommandLineRunner { +@SpringBootApplication(exclude = {org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class, + org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration.class}) +public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); @@ -26,61 +26,6 @@ public class DemoApplication implements CommandLineRunner { @Autowired private TaskRuntime taskRuntime; - @Autowired - private SecurityUtil securityUtil; - - @Override - public void run(String... args) { - - // Using Security Util to simulate a logged in user - securityUtil.logInAs("salaboy"); - - // Let's create a Group Task (not assigned, all the members of the group can claim it) - // Here 'salaboy' is the owner of the created task - log.info("> Creating a Group Task for 'activitiTeam'"); - taskRuntime.create(TaskPayloadBuilder.create() - .withName("First Team Task") - .withDescription("This is something really important") - .withCandidateGroup("activitiTeam") - .withPriority(10) - .build()); - - // Let's log in as 'other' user that doesn't belong to the 'activitiTeam' group - securityUtil.logInAs("other"); - - // Let's get all my tasks (as 'other' user) - log.info("> Getting all the tasks"); - Page tasks = taskRuntime.tasks(Pageable.of(0, 10)); - - // No tasks are returned - log.info("> Other cannot see the task: " + tasks.getTotalItems()); - - - // Now let's switch to a user that belongs to the activitiTeam - securityUtil.logInAs("erdemedeiros"); - - // Let's get 'erdemedeiros' tasks - log.info("> Getting all the tasks"); - tasks = taskRuntime.tasks(Pageable.of(0, 10)); - - // 'erdemedeiros' can see and claim the task - log.info("> erdemedeiros can see the task: " + tasks.getTotalItems()); - - - String availableTaskId = tasks.getContent().get(0).getId(); - - // Let's claim the task, after the claim, nobody else can see the task and 'erdemedeiros' becomes the assignee - log.info("> Claiming the task"); - taskRuntime.claim(TaskPayloadBuilder.claim().withTaskId(availableTaskId).build()); - - - // Let's complete the task - log.info("> Completing the task"); - taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(availableTaskId).build()); - - - } - @Bean public TaskRuntimeEventListener taskAssignedListener() { return taskAssigned -> log.info(">>> Task Assigned: '" diff --git a/src/main/java/com/example/demo/DemoApplicationConfiguration.java b/src/main/java/com/example/demo/DemoApplicationConfiguration.java deleted file mode 100644 index 247e371..0000000 --- a/src/main/java/com/example/demo/DemoApplicationConfiguration.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.example.demo; - -import lombok.extern.slf4j.Slf4j; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.security.core.authority.SimpleGrantedAuthority; -import org.springframework.security.core.userdetails.User; -import org.springframework.security.core.userdetails.UserDetailsService; -import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; -import org.springframework.security.crypto.password.PasswordEncoder; -import org.springframework.security.provisioning.InMemoryUserDetailsManager; - -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - -/** - * @author yanpeng - * @version 1.0 - * @desc TODO - * @company 北京中经网软件有限公司 - * @date 2020/10/29 13:54 - */ -@Slf4j -@Configuration -public class DemoApplicationConfiguration { - - @Bean - public UserDetailsService myUserDetailsService() { - - InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager(); - - String[][] usersGroupsAndRoles = { - {"salaboy", "password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam"}, - {"ryandawsonuk", "password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam"}, - {"erdemedeiros", "password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam"}, - {"other", "password", "ROLE_ACTIVITI_USER", "GROUP_otherTeam"}, - {"admin", "password", "ROLE_ACTIVITI_ADMIN"}, - }; - - for (String[] user : usersGroupsAndRoles) { - List authoritiesStrings = Arrays.asList(Arrays.copyOfRange(user, 2, user.length)); - log.info("> Registering new user: " + user[0] + " with the following Authorities[" + authoritiesStrings + "]"); - inMemoryUserDetailsManager.createUser(new User(user[0], passwordEncoder().encode(user[1]), - authoritiesStrings.stream().map(s -> new SimpleGrantedAuthority(s)).collect(Collectors.toList()))); - } - - - return inMemoryUserDetailsManager; - } - - - @Bean - public PasswordEncoder passwordEncoder() { - return new BCryptPasswordEncoder(); - } - -} \ No newline at end of file diff --git a/src/main/java/com/example/demo/SecurityUtil.java b/src/main/java/com/example/demo/SecurityUtil.java deleted file mode 100644 index f532683..0000000 --- a/src/main/java/com/example/demo/SecurityUtil.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.example.demo; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.GrantedAuthority; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.security.core.context.SecurityContextImpl; -import org.springframework.security.core.userdetails.UserDetails; -import org.springframework.security.core.userdetails.UserDetailsService; -import org.springframework.stereotype.Component; - -import java.util.Collection; - - -/** - * @author yanpeng - * @version 1.0 - * @desc TODO - * @company 北京中经网软件有限公司 - * @date 2020/10/29 13:48 - */ -@Component -public class SecurityUtil { - - private Logger logger = LoggerFactory.getLogger(SecurityUtil.class); - - @Autowired - private UserDetailsService userDetailsService; - - public void logInAs(String username) { - - UserDetails user = userDetailsService.loadUserByUsername(username); - if (user == null) { - throw new IllegalStateException("User " + username + " doesn't exist, please provide a valid user"); - } - logger.info("> Logged in as: " + username); - SecurityContextHolder.setContext(new SecurityContextImpl(new Authentication() { - @Override - public Collection getAuthorities() { - return user.getAuthorities(); - } - - @Override - public Object getCredentials() { - return user.getPassword(); - } - - @Override - public Object getDetails() { - return user; - } - - @Override - public Object getPrincipal() { - return user; - } - - @Override - public boolean isAuthenticated() { - return true; - } - - @Override - public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException { - - } - - @Override - public String getName() { - return user.getUsername(); - } - })); - org.activiti.engine.impl.identity.Authentication.setAuthenticatedUserId(username); - } -} \ No newline at end of file diff --git a/src/main/java/com/example/demo/controller/AdminController.java b/src/main/java/com/example/demo/controller/AdminController.java index 7c92370..df96cc3 100644 --- a/src/main/java/com/example/demo/controller/AdminController.java +++ b/src/main/java/com/example/demo/controller/AdminController.java @@ -2,6 +2,9 @@ package com.example.demo.controller; import com.example.demo.entity.Admin; import com.example.demo.service.AdminService; +import org.activiti.engine.HistoryService; +import org.activiti.engine.ProcessEngine; +import org.activiti.engine.ProcessEngineConfiguration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; @@ -26,10 +29,13 @@ public class AdminController { @Autowired private AdminService adminService; + @Autowired + private ProcessEngine processEngine; @GetMapping("list") public Flux> list(){ List list = adminService.findList(null); + HistoryService historyService = processEngine.getHistoryService(); return Flux.just(list); }