Spring Boot 2 Shiro 去掉URL jsessionid

Spring Boot 2 Shiro 去掉URL jsessionid

一、项目环境

  1. Spring Boot 2.1.2.RELEASE
  2. shiro-spring 1.4

二、去掉URL jsessionid

在shiro配置中,配置关闭url中显示sessionId

    @Bean
    public DefaultWebSessionManager sessionManager() {
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        // 去掉shiro登录时url里的JSESSIONID
        sessionManager.setSessionIdUrlRewritingEnabled(false);
        if ("redis".equals(cacheType)){
            sessionManager.setCacheManager(shiroRedisCacheManager());
            Log.info("当前Shiro缓存框架为:redis",getClass());
        }else if ("ehcache".equals(cacheType)){
            sessionManager.setCacheManager(shiroEhCacheManager());
            Log.info("当前Shiro缓存框架为:ehcache",getClass());
        }else{
            throw new RuntimeException("缓存类型值应该为:redis|ehcache;当前输入值:" + cacheType);
        }
        return sessionManager;
    }

核心代码:

sessionManager.setSessionIdUrlRewritingEnabled(false);

以上片段代码关闭了URL中带SESSIONID

三、其他注意事项

通过上方得配置,常规得URL后面是不会跟sessionId了,但是thymeleaf模板中,如果有以下写

<link rel="stylesheet" th:href="@{/assets/framework/bootstrap/dist/css/bootstrap.min.css}"/>

在加载这个资源文件得时候同样会带出sessionid,可能会引起首次打开网页CDN加载失败,刷新一次恢复正常。

简单得说就是在使用CDN得情况下,thymeleaf模板引擎标准引入资源文件会首次失败得情况(亲测)


目前我的解决办法是直接普通写法:

<link rel="stylesheet" href="/assets/framework/bootstrap/dist/css/bootstrap.min.css"/>

如果有更合理得解决办法请告诉我,谢谢。

 

Spring Boot 2 Shiro 去掉URL jsessionid

相关文章:

你感兴趣的文章:

标签云: