Java Code Examples for org.apache.shiro.mgt.SecurityManager

The following examples show how to use org.apache.shiro.mgt.SecurityManager. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: White-Jotter   Source File: ShiroConfiguration.java    License: MIT License 7 votes vote down vote up
@Bean
public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
    ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
    shiroFilterFactoryBean.setSecurityManager(securityManager);
    shiroFilterFactoryBean.setLoginUrl("/nowhere");

    Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
    Map<String, Filter> customizedFilter = new HashMap<>();  // 自定义过滤器设置 1

    customizedFilter.put("url", getURLPathMatchingFilter()); // 自定义过滤器设置 2,命名,需在设置过滤路径前

    filterChainDefinitionMap.put("/api/authentication", "authc"); // 防鸡贼登录
    filterChainDefinitionMap.put("/api/menu", "authc");
    filterChainDefinitionMap.put("/api/admin/**", "authc");

    filterChainDefinitionMap.put("/api/admin/**", "url");  // 自定义过滤器设置 3,设置过滤路径

    shiroFilterFactoryBean.setFilters(customizedFilter); // 自定义过滤器设置 4,启用
    shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
    return shiroFilterFactoryBean;
}
 
Example 2
Source Project: Spring-Boot-Book   Source File: ShiroConfig.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
    //shirFilter
    ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
    shiroFilterFactoryBean.setSecurityManager(securityManager);
    //拦截器.
    Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
    // 配置不需要权限的资源
    filterChainDefinitionMap.put("/static/**", "anon");
    filterChainDefinitionMap.put("/index", "anon");
    //配置退出过滤器,退出代码Shiro已经替我们实现
    filterChainDefinitionMap.put("/logout", "logout");
    //过滤链定义,从上向下顺序执行,/**放在最下边;
    //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问-->
    filterChainDefinitionMap.put("/**", "authc");
    // 如果不设置默认会自动寻找Web工程根目录下的"/login"页面
    shiroFilterFactoryBean.setLoginUrl("/login");
    // 登录成功后要跳转的链接
    shiroFilterFactoryBean.setSuccessUrl("/index");
    //未授权界面;
    shiroFilterFactoryBean.setUnauthorizedUrl("/403");
    shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
    return shiroFilterFactoryBean;
}
 
Example 3
Source Project: dts-shop   Source File: ShiroConfig.java    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
@Bean
public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
	ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
	shiroFilterFactoryBean.setSecurityManager(securityManager);
	Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
	filterChainDefinitionMap.put("/admin/auth/login", "anon");
	filterChainDefinitionMap.put("/admin/auth/401", "anon");
	filterChainDefinitionMap.put("/admin/auth/index", "anon");
	filterChainDefinitionMap.put("/admin/auth/403", "anon");

	filterChainDefinitionMap.put("/admin/**", "authc");
	shiroFilterFactoryBean.setLoginUrl("/admin/auth/401");
	shiroFilterFactoryBean.setSuccessUrl("/admin/auth/index");
	shiroFilterFactoryBean.setUnauthorizedUrl("/admin/auth/403");
	shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
	return shiroFilterFactoryBean;
}
 
Example 4
Source Project: ZTuoExchange_framework   Source File: ShiroConfig.java    License: MIT License 6 votes vote down vote up
/**
 * ShiroFilterFactoryBean 处理拦截资源文件问题。
 *
 * @param securityManager
 * @return
 */

@Bean(name="shiroFilter")
@DependsOn({"securityManager"})
public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
    log.info("ShiroConfiguration.shirFilter()");
    ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
    shiroFilterFactoryBean.setSecurityManager(securityManager);
    //拦截器.
    Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
    filterChainDefinitionMap.put("/captcha", "anon");
    filterChainDefinitionMap.put("/admin/code/**", "anon");
    filterChainDefinitionMap.put("admin/**/page-query", "user");
    filterChainDefinitionMap.put("/admin/employee/logout", "logout");
    filterChainDefinitionMap.put("admin/**/detail", "authc");
    shiroFilterFactoryBean.setUnauthorizedUrl("/403");
    /*shiroFilterFactoryBean.setU("/403");*/
    shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);

    return shiroFilterFactoryBean;
}
 
Example 5
Source Project: scaffold-cloud   Source File: UserUtil.java    License: MIT License 6 votes vote down vote up
public static Session getSession(){
    try{
        SecurityManager securityManager = ThreadContext.getSecurityManager();
        if(securityManager == null){
            return null;
        }

        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession(false);
        if (session == null){
            session = subject.getSession();
        }
        if (session != null){
            return session;
        }
    }catch (InvalidSessionException e){

    }
    return null;
}
 
Example 6
Source Project: ZTuoExchange_framework   Source File: ShiroConfig.java    License: MIT License 6 votes vote down vote up
/**
 * ShiroFilterFactoryBean 处理拦截资源文件问题。
 *
 * @param securityManager
 * @return
 */

@Bean(name="shiroFilter")
@DependsOn({"securityManager"})
public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
    log.info("ShiroConfiguration.shirFilter()");
    ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
    shiroFilterFactoryBean.setSecurityManager(securityManager);
    //拦截器.
    Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
    filterChainDefinitionMap.put("/captcha", "anon");
    filterChainDefinitionMap.put("/admin/code/**", "anon");
    filterChainDefinitionMap.put("admin/**/page-query", "user");
    filterChainDefinitionMap.put("/admin/employee/logout", "logout");
    filterChainDefinitionMap.put("admin/**/detail", "authc");
    shiroFilterFactoryBean.setUnauthorizedUrl("/403");
    /*shiroFilterFactoryBean.setU("/403");*/
    shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);

    return shiroFilterFactoryBean;
}
 
Example 7
Source Project: mall   Source File: ShiroConfig.java    License: MIT License 6 votes vote down vote up
@Bean
public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
    ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
    shiroFilterFactoryBean.setSecurityManager(securityManager);
    Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
    filterChainDefinitionMap.put("/admin/auth/login", "anon");
    filterChainDefinitionMap.put("/admin/auth/401", "anon");
    filterChainDefinitionMap.put("/admin/auth/index", "anon");
    filterChainDefinitionMap.put("/admin/auth/403", "anon");

    filterChainDefinitionMap.put("/admin/**", "authc");
    shiroFilterFactoryBean.setLoginUrl("/admin/auth/401");
    shiroFilterFactoryBean.setSuccessUrl("/admin/auth/index");
    shiroFilterFactoryBean.setUnauthorizedUrl("/admin/auth/403");
    shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
    return shiroFilterFactoryBean;
}
 
Example 8
Source Project: blog-sample   Source File: ShiroConfig.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Filter工厂,设置过滤条件与跳转条件
 */
@Bean
public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
    ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();

    // Shiro的核心安全接口
    bean.setSecurityManager(securityManager);

    // 设置登陆页
    bean.setLoginUrl("/login");

    // 自定义拦截规则
    Map<String,String> map = new HashMap<>(16);
    map.put("/", "anon");
    // 设置退出登陆
    map.put("/logout", "logout");
    // 对所有用户认证
    map.put("/**", "authc");

    bean.setFilterChainDefinitionMap(map);
    return bean;
}
 
Example 9
Source Project: springboot-shiro   Source File: ShiroConfig.java    License: MIT License 6 votes vote down vote up
/**
 * ShiroFilterFactoryBean 处理拦截资源文件问题。
 * 注意:单独一个ShiroFilterFactoryBean配置是或报错的,因为在
 * 初始化ShiroFilterFactoryBean的时候需要注入:SecurityManager
 * Filter Chain定义说明
 * 1、一个URL可以配置多个Filter,使用逗号分隔
 * 2、当设置多个过滤器时,全部验证通过,才视为通过
 * 3、部分过滤器可指定参数,如perms,roles
 */
@Bean(name = "shiroFilter")
public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
    ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
    // 必须设置 SecurityManager
    shiroFilterFactoryBean.setSecurityManager(securityManager);
    // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面
    shiroFilterFactoryBean.setLoginUrl("/passport/login/");
    // 登录成功后要跳转的链接
    shiroFilterFactoryBean.setSuccessUrl("/index");
    // 未授权界面;
    shiroFilterFactoryBean.setUnauthorizedUrl("/error/403");
    // 配置数据库中的resource
    Map<String, String> filterChainDefinitionMap = shiroService.loadFilterChainDefinitions();
    shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
    return shiroFilterFactoryBean;
}
 
Example 10
Source Project: taoshop   Source File: ShiroConfig.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
    ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
    shiroFilterFactoryBean.setSecurityManager(securityManager);

    //拦截器.
    Map<String,String> filterChainDefinitionMap = new LinkedHashMap<>();
    // 配置不会被拦截的链接 顺序判断
    filterChainDefinitionMap.put("/static/**", "anon");
    filterChainDefinitionMap.put("/upload/**", "anon");
    filterChainDefinitionMap.put("/plugins/**", "anon");
    filterChainDefinitionMap.put("/templates/**", "anon");
    filterChainDefinitionMap.put("/admin/code/api/generate", "anon");
    filterChainDefinitionMap.put("/admin/login/api/toLogin", "anon");
    filterChainDefinitionMap.put("/admin/login/api/loginCheck", "anon");
    filterChainDefinitionMap.put("/**", "authc");

    shiroFilterFactoryBean.setLoginUrl("/admin/login/api/toLogin");
    shiroFilterFactoryBean.setSuccessUrl("/admin/login/api/toIndex");
    shiroFilterFactoryBean.setUnauthorizedUrl("/admin/login/api/toIndex");
    shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
    return shiroFilterFactoryBean;
}
 
Example 11
Source Project: bootshiro   Source File: RestShiroFilterFactoryBean.java    License: MIT License 6 votes vote down vote up
@Override
protected AbstractShiroFilter createInstance() throws Exception {
    LOGGER.debug("Creating Shiro Filter instance.");
    SecurityManager securityManager = this.getSecurityManager();
    String msg;
    if (securityManager == null) {
        msg = "SecurityManager property must be set.";
        throw new BeanInitializationException(msg);
    } else if (!(securityManager instanceof WebSecurityManager)) {
        msg = "The security manager does not implement the WebSecurityManager interface.";
        throw new BeanInitializationException(msg);
    } else {
        FilterChainManager manager = this.createFilterChainManager();
        RestPathMatchingFilterChainResolver chainResolver = new RestPathMatchingFilterChainResolver();
        chainResolver.setFilterChainManager(manager);
        return new RestShiroFilterFactoryBean.SpringShiroFilter((WebSecurityManager)securityManager, chainResolver);
    }
}
 
Example 12
Source Project: springBoot-study   Source File: ShiroConfig.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
	System.out.println("ShiroConfiguration.shirFilter()");
	ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
	shiroFilterFactoryBean.setSecurityManager(securityManager);
	//拦截器.
	Map<String,String> filterChainDefinitionMap = new LinkedHashMap<String,String>();
	// 配置不会被拦截的链接 顺序判断
	filterChainDefinitionMap.put("/static/**", "anon");
	//配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了
	filterChainDefinitionMap.put("/logout", "logout");
	//<!-- 过滤链定义,从上向下顺序执行,一般将/**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了;
	//<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问-->
	filterChainDefinitionMap.put("/**", "authc");
	// 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面
	shiroFilterFactoryBean.setLoginUrl("/login");
	// 登录成功后要跳转的链接
	shiroFilterFactoryBean.setSuccessUrl("/index");

	//未授权界面;
	shiroFilterFactoryBean.setUnauthorizedUrl("/403");
	shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
	return shiroFilterFactoryBean;
}
 
Example 13
Source Project: watchdog-framework   Source File: ShiroConfiguration.java    License: MIT License 6 votes vote down vote up
@Bean
public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager){
    log.info("Shiro Configuration initialized");
    ShiroFilterFactoryBean shiroFilterFactoryBean  = new ShiroFilterFactoryBean();

    //设置SecurityManager
    shiroFilterFactoryBean.setSecurityManager(securityManager);
    //拦截器
    //<!-- 过滤链定义,从上向下顺序执行,一般将 /**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了;
    //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问-->
    Map<String,String> filterChainDefinitionMap = shiroService.getFilterChainDefinitionMap();

    //过滤器
    Map<String,Filter> filters = new HashMap<>();
    filters.put("perms",new JwtFilter());
    shiroFilterFactoryBean.setFilters(filters);
    shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
    return shiroFilterFactoryBean;
}
 
Example 14
Source Project: OneBlog   Source File: ShiroConfig.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * ShiroFilterFactoryBean 处理拦截资源文件问题。
 * 注意:单独一个ShiroFilterFactoryBean配置是或报错的,因为在
 * 初始化ShiroFilterFactoryBean的时候需要注入:SecurityManager
 * Filter Chain定义说明
 * 1、一个URL可以配置多个Filter,使用逗号分隔
 * 2、当设置多个过滤器时,全部验证通过,才视为通过
 * 3、部分过滤器可指定参数,如perms,roles
 */
@Bean(name = "shiroFilter")
public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
    ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
    // 必须设置 SecurityManager
    shiroFilterFactoryBean.setSecurityManager(securityManager);
    // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面
    shiroFilterFactoryBean.setLoginUrl(shiroProperties.getLoginUrl());
    // 登录成功后要跳转的链接
    shiroFilterFactoryBean.setSuccessUrl(shiroProperties.getSuccessUrl());
    // 未授权界面;
    shiroFilterFactoryBean.setUnauthorizedUrl(shiroProperties.getUnauthorizedUrl());
    // 配置数据库中的resource
    Map<String, String> filterChainDefinitionMap = shiroService.loadFilterChainDefinitions();
    shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
    return shiroFilterFactoryBean;
}
 
Example 15
@Override
protected AbstractShiroFilter createInstance() throws Exception {
	SecurityManager securityManager = getSecurityManager();
	if (securityManager == null){
		throw new BeanInitializationException("SecurityManager property must be set.");
	}

	if (!(securityManager instanceof WebSecurityManager)){
		throw new BeanInitializationException("The security manager does not implement the WebSecurityManager interface.");
	}

	PathMatchingFilterChainResolver chainResolver = new PathMatchingFilterChainResolver();
	FilterChainManager chainManager = createFilterChainManager();
	chainResolver.setFilterChainManager(chainManager);
	return new MySpringShiroFilter((WebSecurityManager)securityManager, chainResolver);
}
 
Example 16
Source Project: centraldogma   Source File: ShiroAuthProvider.java    License: Apache License 2.0 6 votes vote down vote up
ShiroAuthProvider(AuthConfig authConfig,
                  Ini config,
                  Supplier<String> sessionIdGenerator,
                  Function<Session, CompletableFuture<Void>> loginSessionPropagator,
                  Function<String, CompletableFuture<Void>> logoutSessionPropagator) {
    requireNonNull(authConfig, "authConfig");
    requireNonNull(config, "config");
    requireNonNull(sessionIdGenerator, "sessionIdGenerator");
    requireNonNull(loginSessionPropagator, "loginSessionPropagator");
    requireNonNull(logoutSessionPropagator, "logoutSessionPropagator");

    final SecurityManager securityManager = createSecurityManager(config, sessionIdGenerator);
    final Duration sessionValidDuration = Duration.ofMillis(authConfig.sessionTimeoutMillis());

    loginApiService = new LoginService(securityManager, authConfig.loginNameNormalizer(),
                                       loginSessionPropagator, sessionValidDuration);
    logoutApiService = new LogoutService(securityManager, logoutSessionPropagator);
}
 
Example 17
Source Project: centraldogma   Source File: ShiroAuthProvider.java    License: Apache License 2.0 6 votes vote down vote up
private static SecurityManager createSecurityManager(Ini config, Supplier<String> sessionIdGenerator) {
    final Factory<SecurityManager> factory = new IniSecurityManagerFactory(config) {
        @Override
        protected SecurityManager createDefaultInstance() {
            final DefaultSessionManager sessionManager = new DefaultSessionManager();
            // This session DAO is required to cache the session in a very short time, especially while
            // logging in to the Central Dogma server. After that, the general session manager provided
            // by Central Dogma server will be working for the session management.
            sessionManager.setSessionDAO(new LimitedMemorySessionDAO(sessionIdGenerator,
                                                                     64, Duration.ofHours(1)));

            final DefaultSecurityManager securityManager = new DefaultSecurityManager();
            securityManager.setSessionManager(sessionManager);

            return securityManager;
        }
    };
    return factory.getInstance();
}
 
Example 18
Source Project: cjs_ssms   Source File: RealmTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) {
  //此处从ini文件来实现用用户角色权限配置,实际多从数据库表来实现
  Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini.bak");

  //SercurityManager 对象
  SecurityManager instance = factory.getInstance();
  SecurityUtils.setSecurityManager(instance);

  //测试用户
  Subject currentUser = SecurityUtils.getSubject();
  UsernamePasswordToken token = new UsernamePasswordToken("admin", "admin");

  boolean result = false;
  try {
    currentUser.login(token);
    result = true;
    LOG.debug("认证成功");
  } catch (Exception e) {
    result = false;
    LOG.debug("认证失败");
  }

}
 
Example 19
Source Project: DouBiNovel   Source File: ShiroConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
    public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
//        System.out.println("ShiroConfiguration.shirFilter()");
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //拦截器.
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();

        // 配置不会被拦截的链接 顺序判断
        filterChainDefinitionMap.put("/admin/verificationCode", "anon");
        filterChainDefinitionMap.put("/admin/doLogin", "anon");
        //配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了, 主要属性:redirectUrl:退出成功后重定向的地址(/)
//        filterChainDefinitionMap.put("/admin/logout", "logout");

        //<!-- 过滤链定义,从上向下顺序执行,一般将/**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了;
        //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问-->
        filterChainDefinitionMap.put("/admin/**", "authc");

        filterChainDefinitionMap.put("/**", "anon");

        // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面
        shiroFilterFactoryBean.setLoginUrl("/admin/login");
        // 登录成功后要跳转的链接
        shiroFilterFactoryBean.setSuccessUrl("/admin/index");

        //未授权界面;
        shiroFilterFactoryBean.setUnauthorizedUrl("/403");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }
 
Example 20
Source Project: spring-boot-shiro   Source File: ShiroConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Filter工厂,设置对应的过滤条件和跳转条件
 * create by: leigq
 * create time: 2019/7/3 14:29
 *
 * @return ShiroFilterFactoryBean
 */
@Bean
public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {

    ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
    shiroFilterFactoryBean.setSecurityManager(securityManager);

    // 过滤器链定义映射
    Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();

    /*
     * anon:所有url都都可以匿名访问,authc:所有url都必须认证通过才可以访问;
     * 过滤链定义,从上向下顺序执行,authc 应放在 anon 下面
     * */
    filterChainDefinitionMap.put("/login", "anon");
    // 配置不会被拦截的链接 顺序判断,因为前端模板采用了thymeleaf,这里不能直接使用 ("/static/**", "anon")来配置匿名访问,必须配置到每个静态目录
    filterChainDefinitionMap.put("/css/**", "anon");
    filterChainDefinitionMap.put("/fonts/**", "anon");
    filterChainDefinitionMap.put("/img/**", "anon");
    filterChainDefinitionMap.put("/js/**", "anon");
    filterChainDefinitionMap.put("/html/**", "anon");
    // 所有url都必须认证通过才可以访问
    filterChainDefinitionMap.put("/**", "authc");

    // 配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了, 位置放在 anon、authc下面
    filterChainDefinitionMap.put("/logout", "logout");

    // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面
    // 配器shirot认登录累面地址,前后端分离中登录累面跳转应由前端路由控制,后台仅返回json数据, 对应LoginController中unauth请求
    shiroFilterFactoryBean.setLoginUrl("/un_auth");

    // 登录成功后要跳转的链接, 此项目是前后端分离,故此行注释掉,登录成功之后返回用户基本信息及token给前端
    // shiroFilterFactoryBean.setSuccessUrl("/index");

    // 未授权界面, 对应LoginController中 unauthorized 请求
    shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorized");
    shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
    return shiroFilterFactoryBean;
}
 
Example 21
Source Project: SENS   Source File: ShiroConfig.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Bean
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //自定义拦截器
        Map<String, Filter> filtersMap = new LinkedHashMap<String, Filter>();
        //访问权限配置
        filtersMap.put("requestURL", getURLPathMatchingFilter());
        shiroFilterFactoryBean.setFilters(filtersMap);

        //拦截器.
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        // 配置不会被拦截的链接 顺序判断
        List<String> urls = getIgnoredUrlsProperties().getUrls();
        for (String url : urls) {
            filterChainDefinitionMap.put(url, "anon");
        }
        filterChainDefinitionMap.put("/admin", "authc");
//        filterChainDefinitionMap.put("/admin/**", "authc");
        filterChainDefinitionMap.put("/admin/**", "requestURL");
        filterChainDefinitionMap.put("/**", "anon");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);


        // 如果不设置默认会自动寻找Web工程根目录下的"/login"页面
        shiroFilterFactoryBean.setLoginUrl("/admin/login");
        // 登录成功后要跳转的链接
        shiroFilterFactoryBean.setSuccessUrl("/");
        //未授权界面;
        shiroFilterFactoryBean.setUnauthorizedUrl("/403");

        return shiroFilterFactoryBean;

    }
 
Example 22
Source Project: spring-boot-demo   Source File: ShiroConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
    log.info("Shiro过滤器开始处理");
    ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
    shiroFilterFactoryBean.setSecurityManager(securityManager);

    // 配置登录页
    shiroFilterFactoryBean.setLoginUrl("/login");
    // 登录成功后跳转页面
    shiroFilterFactoryBean.setSuccessUrl("/index");
    //未授权界面
    shiroFilterFactoryBean.setUnauthorizedUrl("/403");

    //拦截器
    Map<String, String> filterMap = new LinkedHashMap<>();

    //anon:所有url都都可以匿名访问
    Set<String> urlSet = new HashSet<>(ignoreAuthUrlProperties.getIgnoreAuthUrl());
    urlSet.stream().forEach(temp -> filterMap.put(temp, "anon"));

    //用户未登录不进行跳转,返回错误信息
    Map<String, Filter> filters = shiroFilterFactoryBean.getFilters();
    filters.put("authc", new MyFormAuthenticationFilter());

    //配置退出 过滤器
    filterMap.put("/logout", "logout");

    //authc:所有url都必须认证通过才可以访问
    filterMap.put("/**", "authc");

    shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
    return shiroFilterFactoryBean;
}
 
Example 23
Source Project: Moss   Source File: ShiroAuthConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean("shiroFilter")
public ShiroFilterFactoryBean factory(@Qualifier("securityManager") SecurityManager securityManager) {
    ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
    // 添加自己的过滤器并且取名为jwt
    Map<String, Filter> filterMap = new HashMap<>();

    // 必须设置 SecurityManager
    factoryBean.setSecurityManager(securityManager);
    //设置我们自定义的JWT过滤器
    filterMap.put("jwt", new JwtFilter());
    filterMap.put("resourceCheckFilter", new ResourceCheckFilter());
    factoryBean.setLoginUrl("/admin/login");
    factoryBean.setSuccessUrl("/index");
    factoryBean.setFilters(filterMap);
    // 设置无权限时跳转的 url;
    factoryBean.setUnauthorizedUrl("/403");
    LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
    // 访问 /unauthorized/** 不通过JWTFilter
    filterChainDefinitionMap.put("/favicon.ico", "anon");
    filterChainDefinitionMap.put("/css/**", "anon");
    filterChainDefinitionMap.put("/js/**", "anon");
    filterChainDefinitionMap.put("/media/**", "anon");
    filterChainDefinitionMap.put("/", "anon");
    // 所有请求通过我们自己的JWT Filter
    filterChainDefinitionMap.put("/**", "jwt");
    factoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
    return factoryBean;
}
 
Example 24
Source Project: Moss   Source File: ShiroAuthConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public MethodInvokingFactoryBean methodInvokingFactoryBean(SecurityManager securityManager){
    MethodInvokingFactoryBean bean = new MethodInvokingFactoryBean();
    bean.setStaticMethod("org.apache.shiro.SecurityUtils.setSecurityManager");
    bean.setArguments(securityManager);
    return bean;
}
 
Example 25
Source Project: kitty   Source File: ShiroConfig.java    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
@Bean
public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
	ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
	shiroFilter.setSecurityManager(securityManager);
	// 自定义 OAuth2Filter 过滤器,替代默认的过滤器
	Map<String, Filter> filters = new HashMap<>();
	filters.put("oauth2", new OAuth2Filter());
	shiroFilter.setFilters(filters);
	// 访问路径拦截配置,"anon"表示无需验证,未登录也可访问
	Map<String, String> filterMap = new LinkedHashMap<>();
	filterMap.put("/webjars/**", "anon");
	// 查看SQL监控(druid)
	filterMap.put("/druid/**", "anon");
	// 首页和登录页面
	filterMap.put("/", "anon");
	filterMap.put("/login", "anon"); 
	// swagger
	filterMap.put("/swagger-ui.html", "anon");
       filterMap.put("/swagger-resources", "anon");
       filterMap.put("/v2/api-docs", "anon");
       filterMap.put("/webjars/springfox-swagger-ui/**", "anon");
       // 验证码
       filterMap.put("/captcha.jpg**", "anon");
       // 服务监控
       filterMap.put("/actuator/**", "anon");
       // 其他所有路径交给OAuth2Filter处理
	filterMap.put("/**", "oauth2");
	shiroFilter.setFilterChainDefinitionMap(filterMap);
	return shiroFilter;
}
 
Example 26
Source Project: arcusplatform   Source File: SecurityModule.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void bindSecurityManager(AnnotatedBindingBuilder<? super SecurityManager> bind) {
	try {
		bind.toConstructor(DefaultSecurityManager.class.getConstructor(Collection.class)).asEagerSingleton();
  } catch (NoSuchMethodException e) {
      throw new ConfigurationException("This really shouldn't happen.  Either something has changed in Shiro, or there's a bug in " + ShiroModule.class.getSimpleName(), e);
  }
}
 
Example 27
Source Project: blog-sample   Source File: ShiroConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 注入securityManager
 */
@Bean
public SecurityManager securityManager() {
    DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
    manager.setRealm(shiroRealm());
    return manager;
}
 
Example 28
Source Project: blog-sample   Source File: ShiroConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 注册AuthorizationAttributeSourceAdvisor
 * 如果要开启注解,必须添加
 */
@Bean
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager manager) {
    AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
    advisor.setSecurityManager(manager);

    return advisor;
}
 
Example 29
Source Project: springboot-shiro   Source File: ShiroConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public MethodInvokingFactoryBean methodInvokingFactoryBean(SecurityManager securityManager){
    MethodInvokingFactoryBean bean = new MethodInvokingFactoryBean();
    bean.setStaticMethod("org.apache.shiro.SecurityUtils.setSecurityManager");
    bean.setArguments(securityManager);
    return bean;
}
 
Example 30
/**
 * 过滤器
 *
 * @param securityManager 权限管理器
 * @param shiroProperties shiro配置
 * @return 过滤器
 */
@Bean
@ConditionalOnMissingBean
public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager, ShiroProperties shiroProperties) {
    ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
    shiroFilterFactoryBean.setSecurityManager(securityManager);
    shiroFilterFactoryBean.setFilterChainDefinitionMap(shiroProperties.getFilterChainDefinitionMap());
    Map<String, Filter> filters = new LinkedHashMap<>();
    filters.put("jwt", new ShiroFilter());
    shiroFilterFactoryBean.setFilters(filters);
    return shiroFilterFactoryBean;
}