java - Shiro 调用代码无法访问 SecurityManager

标签 java spring-boot shiro

我正在尝试集成 Spring Boot 和 Shiro。当我尝试调用SecurityUtils.getSubject()时在我的一个 Controller 中,发生了异常: org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton. This is an invalid application configuration.

我刚刚按照一些教程和文档来配置 Shiro,这是我的 ShiroConfig类:

@Configuration
public class ShiroConfig {

    @Bean
    public Realm realm() {
        return new UserRealm();
    }

    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher() {
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        hashedCredentialsMatcher.setHashAlgorithmName(PasswordEncoder.getALGORITHM()); 
        hashedCredentialsMatcher.setHashIterations(PasswordEncoder.getITERATION()); 
        return hashedCredentialsMatcher;
    }

    @Bean
    public UserRealm shiroRealm() {
        UserRealm userRealm = new UserRealm();
        userRealm.setCredentialsMatcher(hashedCredentialsMatcher()); 
        return userRealm;
    }

    @Bean
    public SessionsSecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(shiroRealm());
        return securityManager;
    }

    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        defaultAdvisorAutoProxyCreator.setUsePrefix(true);
        return defaultAdvisorAutoProxyCreator;
    }

    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition() {
        DefaultShiroFilterChainDefinition definition = new DefaultShiroFilterChainDefinition();
        definition.addPathDefinition("/login", "anon");
        definition.addPathDefinition("/register", "anon");
        definition.addPathDefinition("/api/**", "user");
        return definition;
    }
}

这是导致异常的代码:

    @PostMapping("/login")
    @ResponseBody
    public Object login(@RequestParam("username") String username,
                        @RequestParam("password") String password) {
        if (username.equals("") || password.equals("")) {
            return "please provide complete information";
        }
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject(); // this line caused exception
        ...
    }

我对这个异常感到非常困惑。有人可以帮忙吗?

编辑 我正在使用 Spring Boot 2.1.6.RELEASE 和 shiro-spring-boot-starter 1.4.0。

最佳答案

您是否使用 shiro-spring-boot-web-starter 依赖项而不是 shiro-spring-boot-starter 依赖项?

根据此文档,这似乎是 Spring Boot Web 应用程序所必需的。

https://shiro.apache.org/spring-boot.html#Spring-WebApplications

关于java - Shiro 调用代码无法访问 SecurityManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56678301/

相关文章:

mysql - Shiro + 存储为字符串的键 = 故障点或不必要的关注?

java - Shiro Authenticating Realm 应该是事务性的吗?

java - 启动新 Activity 会让我进入 Debug模式并停止应用程序

java - 打印 ArrayList 的子列表(基于用户输入的待办事项列表)会导致空 ArrayList

java - 使用 Spring 的 @Value 注释插入时如何避免截断零前导数字?

java - 如何解决shiro的重复提交问题?

java - Firefox Webdriver 实例 'Opens x2 Windows' 而不是仅 'x1 Window'

java - 从 Visual Source 安全 check out 的 Ant 目标

spring - 如何在 Spring Boot 项目中将 application.properties 中的环境属性获取到 logback.groovy 中?

mysql - 在 hibernate 注释中设置 srid