0
点赞
收藏
分享

微信扫一扫

activiti中报错An Authentication object was not found in the SecurityContext


org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext

这是因为activiti7与springboot整合中集成了springsecurity

虽然你的用户赋予了权限

但是你执行方法的时候没有定义是哪个用户在执行方法

要不就整完全点写登录页面

要不就自定义用户去执行方法

securityUtil.logInAs(“zhangsan”);

下面是这个SecurityUtil

package com.qiangqiang.util;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
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;
/**
* \* Created with IntelliJ IDEA.
* \* @author: xiyue
* \* Date: 2021/1/6
* \* Time: 11:00
* \* To change this template use File | Settings | File Templates.
* \* Description:
* \ activiti与springboot整合之后,默认集成了spring security
*/
@Component
public class SecurityUtil {
@Autowired
@Qualifier("myUserDetailsService")
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");
}
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 b) throws IllegalArgumentException {
}
@Override
public String getName() {
return user.getUsername();
}
}
)
);
org.activiti.engine.impl.identity.Authentication.setAuthenticatedUserId(username);
}
}


举报

相关推荐

0 条评论