Bootstrap

org.springframework.security.core.userdetails.UsernameNotFoundException,三步解决Activiti7和Security冲突问题

内容更新:

  • 更新时间:2024-04-09 10:05:00
  • 更新内容:兼容初始化用户信息 + 动态往 InMemoryUserDetailsManager 对象中添加用户信息。

1 前言

    本文旨在解决Activiti7 和 Security 框架的鉴权冲突问题

  • activiti 升级到 activiti7 版本之后,加入了 Security 框架用于权限校验;
  • 很多博客都写到了这个冲突问题,但给的解决方案都是东抄西抄,估计自己都没搞懂,代码也没贴全,真的让人感觉蛋疼;

2 相关应用和工具版本

  • Spring-boot 2.2.2.RELEASE
  • Spring Security 5.2.1
  • Activiti 7.1.0.M6
  • Open JDK11

3 分析问题原因和解决方法

  • 原因:Activiti7版本之后加入了权限校验功能导致。
  • 网上很多看似没问题的解决方法思路:
        a)重写 loadUserByUsername 方法。这种做法并不能解决用户权限问题,实际查询 “待办任务” 列表时仍然报错;
        b)在项目的 @SpringBootApplication 注解上新增 include 内容,五花八门,也仍然会报错;
        c)定义 usersGroupsAndRoles 对象,把用户信息添加到里面 usersGroupsAndRoles 对象中,在项目启动时便初始化所有用户。这种做法后续再新增用户时,无法再加入到 usersGroupsAndRoles 对象中,有很大的局限性;
  • 正确解决方法:通过向 InMemoryUserDetailsManager 对象中动态塞入用户名,基于 Security 安全框架直接做伪登录操作,而且也会不破坏原有系统的登录操作。核心方法是:InMemoryUserDetailsManager.createUser() 方法。
    ``

4 以下是具体报错:

org.springframework.security.core.userdetails.UsernameNotFoundException

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.springframework.security.core.userdetails.UsernameNotFoundException: 物业维修班长
### Cause: org.springframework.security.core.userdetails.UsernameNotFoundException: 物业维修班长
	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
	at org.activiti.engine.impl.db.DbSqlSession.selectListWithRawParameter(DbSqlSession.java:404)
	at org.activiti.engine.impl.db.DbSqlSession.selectList(DbSqlSession.java:374)
	at org.activiti.engine.impl.db.DbSqlSession.selectList(DbSqlSession.java:365)
	at org.activiti.engine.impl.persistence.entity.data.impl.MybatisTaskDataManager.findTasksByQueryCriteria(MybatisTaskDataManager.java:66)
	at org.activiti.engine.impl.persistence.entity.TaskEntityManagerImpl.findTasksByQueryCriteria(TaskEntityManagerImpl.java:260)
	at org.activiti.engine.impl.TaskQueryImpl.executeList(TaskQueryImpl.java:1357)
	at org.activiti.engine.impl.AbstractQuery.execute(AbstractQuery.java:163)
	at org.activiti.engine.impl.interceptor.CommandInvoker$1.run(CommandInvoker.java:37)
	at org.activiti.engine.impl.interceptor.CommandInvoker.executeOperation(CommandInvoker.java:78)
	at org.activiti.engine.impl.interceptor.CommandInvoker.executeOperations(CommandInvoker.java:57)
	at org.activiti.engine.impl.interceptor.CommandInvoker.execute(CommandInvoker.java:42)
	at org.activiti.engine.impl.interceptor.TransactionContextInterceptor.execute(TransactionContextInterceptor.java:48)
	at org.activiti.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:59)
	at org.activiti.spring.SpringTransactionInterceptor$1.doInTransaction(SpringTransactionInterceptor.java:47)
	at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140)
	at org.activiti.spring.SpringTransactionInterceptor.execute(SpringTransactionInterceptor.java:45)
	at org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:29)
	at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:44)
	at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:39)
	at org.activiti.engine.impl.AbstractQuery.list(AbstractQuery.java:137)
	at com.xh.niec.service.impl.ActCommonServiceImpl.createTaskQueryByCandidateOrAssigne(ActCommonServiceImpl.java:127)
	at com.xh.niec.service.impl.ActCommonServiceImpl.createQueryAllTask(ActCommonServiceImpl.java:154)
	at com.xh.niec.service.impl.FaultOrderServiceImpl.getFaultOrderTaskList(FaultOrderServiceImpl.java:212)
	at com.xh.niec.service.impl.FaultOrderServiceImpl$$FastClassBySpringCGLIB$$688da1f8.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:685)
	at com.xh.niec.service.impl.FaultOrderServiceImpl$$EnhancerBySpringCGLIB$$2f5a7976.getFaultOrderTaskList(<generated>)
	at com.xh.niec.controller.FaultOrderController.getFaultOrderTaskList(FaultOrderController.java:130)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at com.gh.common.filter.TokenAuthenticationFilter.doFilterInternal(TokenAuthenticationFilter.java:53)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)
	at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126)
	at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:118)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:158)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter.doFilter(OAuth2AuthenticationProcessingFilter.java:176)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.header.HeaderWriterFilter.doHeadersAfter(HeaderWriterFilter.java:92)
	at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:77)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
	at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)
	at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
	at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
	at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:108)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1591)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: org.springframework.security.core.userdetails.UsernameNotFoundException: 物业维修班长
	at org.springframework.security.provisioning.InMemoryUserDetailsManager.loadUserByUsername(InMemoryUserDetailsManager.java:156)
	at org.activiti.core.common.spring.identity.ActivitiUserGroupManagerImpl.getUserGroups(ActivitiUserGroupManagerImpl.java:21)
	at org.activiti.engine.impl.TaskQueryImpl.getGroupsForCandidateUser(TaskQueryImpl.java:1224)
	at org.activiti.engine.impl.TaskQueryImpl.getCandidateGroups(TaskQueryImpl.java:1216)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.apache.ibatis.reflection.invoker.MethodInvoker.invoke(MethodInvoker.java:44)
	at org.apache.ibatis.reflection.wrapper.BeanWrapper.getBeanProperty(BeanWrapper.java:164)
	at org.apache.ibatis.reflection.wrapper.BeanWrapper.get(BeanWrapper.java:49)
	at org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:122)
	at org.apache.ibatis.scripting.xmltags.DynamicContext$ContextMap.get(DynamicContext.java:102)
	at org.apache.ibatis.scripting.xmltags.DynamicContext$ContextAccessor.getProperty(DynamicContext.java:113)
	at org.apache.ibatis.ognl.OgnlRuntime.getProperty(OgnlRuntime.java:2719)
	at org.apache.ibatis.ognl.ASTProperty.getValueBody(ASTProperty.java:114)
	at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
	at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
	at org.apache.ibatis.ognl.ASTNotEq.getValueBody(ASTNotEq.java:50)
	at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
	at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
	at org.apache.ibatis.ognl.ASTOr.getValueBody(ASTOr.java:61)
	at org.apache.ibatis.ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
	at org.apache.ibatis.ognl.SimpleNode.getValue(SimpleNode.java:258)
	at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:493)
	at org.apache.ibatis.ognl.Ognl.getValue(Ognl.java:457)
	at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:46)
	at org.apache.ibatis.scripting.xmltags.ExpressionEvaluator.evaluateBoolean(ExpressionEvaluator.java:32)
	at org.apache.ibatis.scripting.xmltags.IfSqlNode.apply(IfSqlNode.java:34)
	at org.apache.ibatis.scripting.xmltags.MixedSqlNode.lambda$apply$0(MixedSqlNode.java:32)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
	at org.apache.ibatis.scripting.xmltags.MixedSqlNode.apply(MixedSqlNode.java:32)
	at org.apache.ibatis.scripting.xmltags.DynamicSqlSource.getBoundSql(DynamicSqlSource.java:39)
	at org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:297)
	at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:81)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)
	... 120 more

5 解决方案

  • 第一步:新建一个配置类(ActivitiSecurityConfiguration.java)
  • 第二步:新建一个工具类(SecurityUtil.java)
  • 第三步:调用 taskCandidateOrAssigned 方法之前,先调用工具类的 logInAs 方法

5.1 我的 pom.xml 依赖包引用

  • 其中 7.1.0.M6 包,可以尝试下把 groupId 换成 org.activiti 试试(我maven远程仓库用的是阿里的,但是下载下来项目启动不了);
  • 或使用公司私有仓库,把 7.1.0.M6.jar 文件按照格式上传上去,文件我放在CSDN了:7.1.0.M6.jar
<!--activiti7 相关依赖包start-->
   <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
    </dependency>
    <dependency>
        <groupId>com.xh</groupId>
        <artifactId>activiti-engine</artifactId>
        <version>7.1.0.M6</version>
    </dependency>
    <dependency>
        <groupId>org.activiti</groupId>
        <artifactId>activiti-spring-boot-starter</artifactId>
        <version>7.1.0.M6</version>
        <exclusions>
            <exclusion>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.activiti</groupId>
                <artifactId>activiti-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>de.odysseus.juel</groupId>
        <artifactId>juel-api</artifactId>
        <version>2.2.7</version>
    </dependency>
    <dependency>
        <groupId>de.odysseus.juel</groupId>
        <artifactId>juel-impl</artifactId>
        <version>2.2.7</version>
    </dependency>
    <dependency>
        <groupId>de.odysseus.juel</groupId>
        <artifactId>juel-spi</artifactId>
        <version>2.2.7</version>
    </dependency>
<!--activiti7 相关依赖包end-->

5.2 新建配置类 ActivitiSecurityConfiguration

  • 除了本类的包路径和引用SecurityUtil类的路径,其他都不要动
package com.xh.niec.config;

import com.xh.niec.model.util.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Description: 用于解决 Activiti7 和 Security 框架的鉴权冲突问题
 * @Author: zhanleai
 * @Date:2024/1/25 13:48
 */
@Configuration
@EnableWebSecurity
@Slf4j
@Component
public class ActivitiSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Resource
    private SecurityUtil securityUtil ;

    @Override
    @Autowired
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService());
    }

    @Bean
    public UserDetailsService userDetailsService() {
        return securityUtil.findInstance();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .authorizeRequests()
            .anyRequest()
            .authenticated()
            .and()
            .httpBasic();
    }
}

5.3 新建工具类 SecurityUtil

  • 除了本类的包路径,其他都不要动
package com.xh.niec.model.util;

import com.xh.niec.mapper.UserMapper;
import com.xh.niec.model.entity.SecurityUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

@Component
@Slf4j
public class SecurityUtil {

    @Autowired
    private UserMapper userMapper ;

    //主要是前面的用户名,后面的随便写
    public static String[] userRoleArrays = {
            "会展领导","会展经理","会展暖通主管","会展弱电主管",
            "会展消防主管","会展安保主管","会展员工","物业项目经理",
            "物业工程部经理","物业暖通班长", "物业暖通组员","物业弱电班长",
            "物业弱电组员","物业消防班长","物业消防组员","物业维修班长",
            "物业维修组员","物业安保主管","物业安保队长","物业安保班长","文员"} ;

    private static InMemoryUserDetailsManager inMemoryUserDetailsManager ;

    public InMemoryUserDetailsManager findInstance(){
        try{
            if(SecurityUtil.inMemoryUserDetailsManager==null){
                SecurityUtil.inMemoryUserDetailsManager = new InMemoryUserDetailsManager();

                List<String> securityUserList = new ArrayList<>() ;

                //查询并添加所有用户ID
                List<SecurityUser> userList = userMapper.selectUserList(null);
                if(Objects.nonNull(userList) && userList.size()>0){
                    userList.stream().forEach(user -> {
                        securityUserList.add(user.getId().toString());
                    });
                }
                //添加所有用户角色
                securityUserList.addAll(Arrays.asList(userRoleArrays));

                if(Objects.nonNull(securityUserList) && securityUserList.size()>0){
                    String[][] userListArray = new String[securityUserList.size()][4];
                    for (int i = 0 ; i<securityUserList.size(); i++){
                        String userIdOrRoleName = securityUserList.get(i);
                        String[] userArray = {userIdOrRoleName,"password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam"};
                        userListArray[i] = userArray ;
                    }

                    for (String[] user : userListArray) {
                        List<String> authoritiesStrings = Arrays.asList(Arrays.copyOfRange(user, 2, user.length));
                        SecurityUtil.inMemoryUserDetailsManager.createUser(new User(user[0], passwordEncoder().encode(user[1]),
                                authoritiesStrings.stream().map(s -> new SimpleGrantedAuthority(s)).collect(Collectors.toList())));
                    }
                    log.info("===============初始化加载 inMemoryUserDetailsManager 完毕!!!");
                }
            }
        }catch (Exception e){
            log.info("=======================findInstance异常" + e.getMessage());
        }

        return SecurityUtil.inMemoryUserDetailsManager;
    }

    public synchronized void createUser(String userName) {
        try{
            if(!SecurityUtil.inMemoryUserDetailsManager.userExists(userName)) {
                List<SimpleGrantedAuthority> roles = new ArrayList<SimpleGrantedAuthority>(){{
                    add(new SimpleGrantedAuthority("ROLE_ACTIVITI_USER"));
                    add(new SimpleGrantedAuthority("GROUP_activitiTeam"));
                }};
                SecurityUtil.inMemoryUserDetailsManager.createUser(new User(userName, passwordEncoder().encode("password"),roles));
            }
        }catch (Exception e){
            log.info("==============createUser异常" + e.getMessage());

        }
    }

    public void logInAs(String userName) {
        try{
            createUser(userName);

            UserDetails user = findInstance().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 isAuthenticated) throws IllegalArgumentException {

                }

                @Override
                public String getName() {
                    return user.getUsername();
                }
            }));
            org.activiti.engine.impl.identity.Authentication.setAuthenticatedUserId(userName);
        }catch (Exception e){
            log.info("==============logInAs异常" + e.getMessage());
        }
    }

    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

}


5.4 调用工具类方法

  • 这里只是一个简单的调用而已,不涉及到其他任何逻辑,我就不贴所有代码了;
    在这里插入图片描述
;