当前位置 : 主页 > 编程语言 > c++ >

基于ShiroFilterFactoryBean扩展Shiro访问静态文件不更新session,解决Sesssion 频繁更新

来源:互联网 收集:自由互联 发布时间:2021-07-03
ShiroFilterFactoryBean public class ShiroFilterFactoryBean extends org.apache.shiro.spring.web.ShiroFilterFactoryBean { private String resourcePatterns; @Override protected AbstractShiroFilter createInstance() throws Exception { AbstractShi
ShiroFilterFactoryBean
public class ShiroFilterFactoryBean extends org.apache.shiro.spring.web.ShiroFilterFactoryBean {
    private String resourcePatterns;

    @Override
    protected AbstractShiroFilter createInstance() throws Exception {
        AbstractShiroFilter instance = super.createInstance();
        Set
 
   patternSet = null;
        if (StringUtils.hasText(resourcePatterns)) {
            patternSet = Stream.of(resourcePatterns.split(","))
                    .filter(StringUtils::hasLength)
                    .map(String::trim)
                    .collect(Collectors.toSet());

        }
        return new SpringShiroFilter(instance.getSecurityManager(),
                instance.getFilterChainResolver(), patternSet);
    }

    private static final class SpringShiroFilter extends AbstractShiroFilter {
        private Set
  
    patternSet; private AntPathMatcher matcher; protected SpringShiroFilter(WebSecurityManager webSecurityManager, FilterChainResolver resolver, Set
   
     patternSet) { super(); if (isNull(webSecurityManager)) { throw new IllegalArgumentException("WebSecurityManager property cannot be null."); } setSecurityManager(webSecurityManager); if (nonNull(resolver)) { setFilterChainResolver(resolver); } if (nonNull(patternSet)) { this.patternSet = patternSet; matcher = new AntPathMatcher(); } } @Override protected void updateSessionLastAccessTime(ServletRequest request, ServletResponse response) { if(!isHttpSessions()&&!isResource(request)) super.updateSessionLastAccessTime(request, response); } private boolean isResource(ServletRequest servletRequest) { if (isNull(patternSet)||patternSet.isEmpty()) { return false; } HttpServletRequest request = (HttpServletRequest) servletRequest; int contextPathLen=request.getContextPath().length(); String requestURI=request.getRequestURI(); final String requestPath =requestURI.substring(contextPathLen); return patternSet.stream().anyMatch(pattern -> matcher.match(pattern, requestPath)); } } public void setResourcesPatterns(String patterns) { this.resourcePatterns = patterns; } }
   
  
 
网友评论