shiro异常UnavailableSecurityManagerException

昨天在调程序时出现标题上的异常

查了下好像是说SecurityManager相关问题

后来我看到代码中:

@Bean(name = "securityManager")
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //设置自定义realm.
        securityManager.setRealm(getDatabaseRealm());
        //配置记住我
        securityManager.setRememberMeManager(rememberMeManager());
        SecurityUtils.setSecurityManager(securityManager);
        return securityManager;
    }

SecurityUtils.setSecurityManager(securityManager)这一行之前被注释掉了
然后看到SecurityUtils里面

public static SecurityManager getSecurityManager() throws UnavailableSecurityManagerException {
        SecurityManager securityManager = ThreadContext.getSecurityManager();
        if (securityManager == null) {
            securityManager = securityManager;
        }

        if (securityManager == null) {
            String msg = "No SecurityManager accessible to the calling code, either bound to the " + ThreadContext.class.getName() + " or as a vm static singleton.  This is an invalid application configuration.";
            throw new UnavailableSecurityManagerException(msg);
        } else {
            return securityManager;
        }
    }

getSecurityManager()正是抛出的该异常

在开头的SecurityManager加入该行异常消失

SecurityUtils.setSecurityManager(securityManager);

后来我想原因可能出现在我在之前代码更改加入了对控制层某方法多线程的处理以及配置:

package com.tansuo365.test1.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class AsyncConfig {

    @Bean
    public TaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 设置核心线程数
        executor.setCorePoolSize(5);
        // 设置最大线程数
        executor.setMaxPoolSize(10);
        // 设置队列容量
        executor.setQueueCapacity(20);
        // 设置线程活跃时间(秒)
        executor.setKeepAliveSeconds(60);
        // 设置默认线程名称
        executor.setThreadNamePrefix("danhao-");
        // 设置拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 等待所有任务结束后再关闭线程池
        executor.setWaitForTasksToCompleteOnShutdown(true);
        return executor;
    }
}
@Async
    @RequestMapping("/getChukuNumber")
    public ListenableFuture<String> genBillCode(String type) throws Exception {
        StringBuffer billCodeStr = new StringBuffer();
        billCodeStr.append(chukudanPrefix);
        billCodeStr.append(DateUtil.getCurrentDateStr());
        String todayMaxChukuDanNumber = chukuZongService.getTodayMaxChukuDanNumber();
        if (todayMaxChukuDanNumber != null) {
            billCodeStr.append(StringUtil.formatCode(todayMaxChukuDanNumber));
        } else {
            billCodeStr.append("0001");
        }
        return new AsyncResult<>(billCodeStr.toString());
//        return billCodeStr.toString();
    }

如果要启用还要在springboot上加注注解:

//@EnableCaching
@SpringBootApplication
@MapperScan(value = {"com.xxxxxxx.test1.mapper"})
@EnableAsync//开启异步任务
public class Test1Application {

    public static void main(String[] args) {
        SpringApplication.run(Test1Application.class, args);
    }

}

问题结束

相关推荐