You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.3 KiB
77 lines
2.3 KiB
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);
|
|
}
|
|
}
|