燕鹏
4 years ago
4 changed files with 9 additions and 193 deletions
@ -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(); |
|
||||
} |
|
||||
|
|
||||
} |
|
@ -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); |
|
||||
} |
|
||||
} |
|
Loading…
Reference in new issue