开发者社区> 问答> 正文

【求助】SpingBoot 模式下设置Shiro打开页面后台报错?报错

 JavaConfig:

@Configuration
public class ShiroConfig {


    String captchaError = "captchaError";

    @Bean(name = "lifecycleBeanPostProcessor")
    LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    AJaxSupportAuthorizationFilter aJaxSupportAuthorizationFilter() {
        AJaxSupportAuthorizationFilter aJaxSupportAuthorizationFilter = new AJaxSupportAuthorizationFilter();
        aJaxSupportAuthorizationFilter.setErrorCaptchaAttr(captchaError);
        return aJaxSupportAuthorizationFilter;
    }

    @Bean
    AdminPasswordCredentialsMatcher adminPasswordCredentialsMatcher() {
        AdminPasswordCredentialsMatcher adminPasswordCredentialsMatcher = new AdminPasswordCredentialsMatcher();
        adminPasswordCredentialsMatcher.setHashAlgorithmName("SHA-1");
        adminPasswordCredentialsMatcher.setHashIterations(1024);
        adminPasswordCredentialsMatcher.setStoredCredentialsHexEncoded(true);
        return adminPasswordCredentialsMatcher;
    }

    @Bean
    @DependsOn("lifecycleBeanPostProcessor")
    AdminAuthorizingRealm adminAuthorizingRealm() {
        AdminAuthorizingRealm adminAuthorizingRealm = new AdminAuthorizingRealm();
        adminAuthorizingRealm.setCredentialsMatcher(adminPasswordCredentialsMatcher());
        return adminAuthorizingRealm;
    }

    @Bean
    public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator daap = new DefaultAdvisorAutoProxyCreator();
        daap.setProxyTargetClass(true);
        return daap;
    }

    @Bean(name = "securityManager")
    DefaultWebSecurityManager defaultWebSecurityManager() {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(adminAuthorizingRealm());
        defaultWebSecurityManager.setSubjectFactory(new DefaultWebSubjectFactory());
        DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
        subjectDAO.setSessionStorageEvaluator(new DefaultWebSessionStorageEvaluator());

        defaultWebSecurityManager.setSubjectDAO(subjectDAO);

        return defaultWebSecurityManager;
    }

    @Bean
    EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
        EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
        return ehCacheManagerFactoryBean;
    }

    @Bean(name = "shiroFilter")
    ShiroFilterFactoryBean shiroFilterFactoryBean() {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager());
        org.apache.shiro.SecurityUtils.setSecurityManager(shiroFilterFactoryBean.getSecurityManager());
        shiroFilterFactoryBean.setLoginUrl("/login.html");
        shiroFilterFactoryBean.setSuccessUrl("/admin/");
        shiroFilterFactoryBean.setUnauthorizedUrl("/");
        HashMap<String, Filter> filters = new HashMap<>();

        CaptchaFilter captchaFilter = new CaptchaFilter();
        captchaFilter.setSessionCaptchaAttr("verifyCode");
        captchaFilter.setRequestCaptchaParam("captcha");
        captchaFilter.setErrorCaptchaAttr(captchaError);
        filters.put("captcha", captchaFilter);
        filters.put("authc", aJaxSupportAuthorizationFilter());
        shiroFilterFactoryBean.setFilters(filters);
        shiroFilterFactoryBean.setFilterChainDefinitions(
                "/admin/login=captcha,authc\n" +
                        "/admin/logout=anon\n" +
                        "/login=anon\n" +
                        "/static/**=anon\n" +
                        "/resources/**=anon\n" +
                        "/upload/**=anon\n" +
                        "/common/**=anon\n" +
                        "/ueditor/**=anon\n" +
                        "/druid/**=authc\n" +
                        "/admin/**=authc\n" +
                        "/activiti/**=authc");

        return shiroFilterFactoryBean;
    }

}

启动没有错误,打开页面报错:

java.lang.IllegalArgumentException: SessionContext must be an HTTP compatible implementation.
	at org.apache.shiro.web.session.mgt.ServletContainerSessionManager.createSession(ServletContainerSessionManager.java:103) ~[shiro-web-1.2.6.jar:1.2.6]
	at org.apache.shiro.web.session.mgt.ServletContainerSessionManager.start(ServletContainerSessionManager.java:64) ~[shiro-web-1.2.6.jar:1.2.6]
	at org.apache.shiro.mgt.SessionsSecurityManager.start(SessionsSecurityManager.java:121) ~[shiro-core-1.2.6.jar:1.2.6]
	at org.apache.shiro.subject.support.DelegatingSubject.getSession(DelegatingSubject.java:336) ~[shiro-core-1.2.6.jar:1.2.6]
	at org.apache.shiro.subject.support.DelegatingSubject.getSession(DelegatingSubject.java:312) ~[shiro-core-1.2.6.jar:1.2.6]
	at org.apache.shiro.web.util.WebUtils.saveRequest(WebUtils.java:606) ~[shiro-web-1.2.6.jar:1.2.6]
	at org.apache.shiro.web.filter.AccessControlFilter.saveRequest(AccessControlFilter.java:208) ~[shiro-web-1.2.6.jar:1.2.6]
	at org.apache.shiro.web.filter.AccessControlFilter.saveRequestAndRedirectToLogin(AccessControlFilter.java:191) ~[shiro-web-1.2.6.jar:1.2.6]
	at cn.firegod.common.shiro.AJaxSupportAuthorizationFilter.onAccessDenied(AJaxSupportAuthorizationFilter.java:83) ~[classes/:na]

 

出现这样的一个情况,POST请求的时候没有request和response:

 

展开
收起
爱吃鱼的程序员 2020-06-08 12:24:09 751 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    这个问题在于使用的SecurityManager不是一个web项目的SecurityManager,我记得是这样的,时间久了记不清了。

    2020-06-08 12:24:28
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载