Browse Source

去掉验证

master
燕鹏 4 years ago
parent
commit
da8aaaf639
  1. 61
      src/main/java/com/example/demo/DemoApplication.java
  2. 58
      src/main/java/com/example/demo/DemoApplicationConfiguration.java
  3. 77
      src/main/java/com/example/demo/SecurityUtil.java
  4. 6
      src/main/java/com/example/demo/controller/AdminController.java

61
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<Task> 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<TaskAssignedEvent> taskAssignedListener() {
return taskAssigned -> log.info(">>> Task Assigned: '"

58
src/main/java/com/example/demo/DemoApplicationConfiguration.java

@ -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<String> 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();
}
}

77
src/main/java/com/example/demo/SecurityUtil.java

@ -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<? extends GrantedAuthority> 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);
}
}

6
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<Admin>> list(){
List<Admin> list = adminService.findList(null);
HistoryService historyService = processEngine.getHistoryService();
return Flux.just(list);
}

Loading…
Cancel
Save