2018-08-22

/**

  • Shiro的配置文件
    */
    @Configuration
    public class ShiroConfig {

    /**

    • 开启cookie
    • @param cookieTimeout
    • @return
      */
      @Bean("simpleCookie")
      public SimpleCookie simpleCookie(@Value("${shiro.cookie.timeout:-1}") Integer cookieTimeout) {
      SimpleCookie simpleCookie = new SimpleCookie("sid");
      simpleCookie.setHttpOnly(true);
      simpleCookie.setPath("/");
      simpleCookie.setMaxAge(cookieTimeout);
      return simpleCookie;
      }

    /**

    • session管理器

    • @param globalSessionTimeout

    • @param redisShiroSessionDAO // * @param validationScheduler

    • @param simpleCookie

    • @return
      */
      @Bean("sessionManager")
      public SessionManager sessionManager(@Value("${shiro.login.session.timeout}") Long globalSessionTimeout,
      RedisShiroSessionDAO redisShiroSessionDAO,
      // ExecutorServiceSessionValidationScheduler validationScheduler,
      SimpleCookie simpleCookie) {
      DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
      //设置session过期时间为1小时(单位:毫秒),默认为30分钟
      sessionManager.setGlobalSessionTimeout(globalSessionTimeout);
      sessionManager.setSessionValidationSchedulerEnabled(true);
      sessionManager.setSessionIdUrlRewritingEnabled(false);
      sessionManager.setSessionDAO(redisShiroSessionDAO);

      //开启cookie 不然一直登录不上,remember不起作用
      sessionManager.setSessionIdCookieEnabled(true);
      sessionManager.setSessionIdCookie(simpleCookie);
      return sessionManager;
      }

    //配置自定义的密码比较器
    @Bean(name="credentialsMatcher")
    public CredentialsMatcher credentialsMatcher() {
    return new CredentialsMatcher();
    }

@Bean("userRealm")
public UserRealm setUserRealm(@Value("${user.login.cacheName:webLoginCache}") String authenticationCacheName,@Qualifier("credentialsMatcher") CredentialsMatcher matcher) {
    UserRealm userRealm = new UserRealm();
    userRealm.setCredentialsMatcher(matcher);
    userRealm.setAuthenticationCacheName(authenticationCacheName);
    return userRealm;
}


/**
 * 认证管理
 *
 * @param userRealm
 * @param sessionManager
 * @return
 */
@Bean("securityManager")
public SecurityManager securityManager(UserRealm userRealm, SessionManager sessionManager) {
    DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
    securityManager.setRealm(userRealm);
    securityManager.setSessionManager(sessionManager);

    return securityManager;
}


/**
 * 过滤
 *
 * @param securityManager
 * @param userFilter
 * @param kickoutFilter
 * @return
 */
@Bean("shiroFilter")
public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager,
                                          UserSessionFilter userFilter,
                                          KickoutSessionControlFilter kickoutFilter) {
    ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
    shiroFilter.setSecurityManager(securityManager);
    shiroFilter.setLoginUrl("/login");
    shiroFilter.setUnauthorizedUrl("/");
    Map filters = Maps.newHashMap();
    filters.put("userFilter", userFilter);
    filters.put("kickoutFilter", kickoutFilter);
    shiroFilter.setFilters(filters);
    shiroFilter.setFilterChainDefinitions("/** = kickoutFilter,userFilter");
    return shiroFilter;
}

@Bean("userFilter")
public UserSessionFilter userSessionFilter(@Value("${shiro.login.ignoreUrls}") String ignoreUrls){
    UserSessionFilter userSessionFilter = new UserSessionFilter();
    userSessionFilter.setIgnoreUrls(ignoreUrls);
    return userSessionFilter;
}

@Bean("kickoutFilter")
public KickoutSessionControlFilter kickoutSessionControlFilter(@Value("${shiro.login.maxSession:1}") Integer maxSession, SessionManager sessionManager, CacheManager cacheManager){
    KickoutSessionControlFilter kickoutSessionControlFilter = new KickoutSessionControlFilter();
    kickoutSessionControlFilter.setSessionManager(sessionManager);
    kickoutSessionControlFilter.setCacheManager(cacheManager);
    kickoutSessionControlFilter.setMaxSession(maxSession);
    return kickoutSessionControlFilter;
}

@Bean
public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
    return new LifecycleBeanPostProcessor();
}

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

@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
    AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
    advisor.setSecurityManager(securityManager);
    return advisor;
}

@Bean
public MethodInvokingFactoryBean invoke(SecurityManager securityManager) {
    MethodInvokingFactoryBean invokingFactoryBean = new MethodInvokingFactoryBean();
    invokingFactoryBean.setStaticMethod("org.apache.shiro.SecurityUtils.setSecurityManager");
    invokingFactoryBean.setArguments(securityManager);
    return invokingFactoryBean;
}

}

你可能感兴趣的:(2018-08-22)