03-4 shiro的ini配置

  • ini配置文件有点类似java中Properties文件,提供了key-value特性,key之间不能重复。
  • 采用两种方式实现shiro

一、创建项目01-config

1.1、不采用ini配置文件方式

1.1.1、java测试使用
package com.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.authz.ModularRealmAuthorizer;
import org.apache.shiro.authz.permission.WildcardPermissionResolver;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.realm.jdbc.JdbcRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Test;
import java.util.Arrays;
/**
 * @author brusion
 * @date 2018/9/16
 */
public class NoConfigApplication {
    @Test
    public void checkOnConfig() {
        DefaultSecurityManager manager = new DefaultSecurityManager();
        ModularRealmAuthenticator authenticator = new ModularRealmAuthenticator();
        authenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy());
        manager.setAuthenticator(authenticator);
        ModularRealmAuthorizer authorizer = new ModularRealmAuthorizer();
        authorizer.setPermissionResolver(new WildcardPermissionResolver());
        manager.setAuthorizer(authorizer);
        DruidDataSource source = new DruidDataSource();
        source.setDriverClassName("com.mysql.jdbc.Driver");
        source.setUrl("jdbc:mysql://localhost:3306/shiro");
        source.setUsername("root");
        source.setPassword("123123");
        JdbcRealm realm = new JdbcRealm();
        realm.setDataSource(source);
        realm.setPermissionsLookupEnabled(true);
        manager.setRealms(Arrays.<Realm>asList(realm));
        SecurityUtils.setSecurityManager(manager);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");
        try {
            subject.login(token);
            System.out.println("=== 登录成功 === ");
        } catch (Exception e) {
            System.out.println("=== 登录失败 === ");
        }
        subject.logout();
    }
}
说明:
  • 1、DefaultSecurityManager:是SecurityManager最终实现类,并通过一系列继承和实现拓展了功能
  • 2、ModularRealmAuthenticator:提供了退出登录和验证接口功能
  • 3、ModularRealmAuthorizer: 用于做权限判断
  • 4、DruidDataSource:数据库连接对象
  • 5、JdbcRealm:也是实现了realm,并实现了其他接口主要用于数据的提供

1.2、采用ini配置文件方式

1.2.1、创建ini文件:shiro_config.ini
[main]
#authenticator
authenticator=org.apache.shiro.authc.pam.ModularRealmAuthenticator
authenticationStrategy=org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy
authenticator.authenticationStrategy=$authenticationStrategy
securityManager.authenticator=$authenticator
#authorizer
authorizer=org.apache.shiro.authz.ModularRealmAuthorizer
permissionResolver=org.apache.shiro.authz.permission.WildcardPermissionResolver
authorizer.permissionResolver=$permissionResolver
securityManager.authorizer=$authorizer
#data
dataSource=com.alibaba.druid.pool.DruidDataSource
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://localhost:3306/shiro
dataSource.username=root
dataSource.password=123123
#realm
jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm
jdbcRealm.dataSource=$dataSource
jdbcRealm.permissionsLookupEnabled=true
securityManager.realms=$jdbcRealm

1.2.2、测试使用
package com.config;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Test;
/**
 * @author brusion
 * @date 2018/9/16
 */
public class ConfigApplication {
    @Test
    public void check(){
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro_config.ini");
        SecurityManager manager = factory.getInstance();
        SecurityUtils.setSecurityManager(manager);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");
        try {
            subject.login(token);
            System.out.println("=== 登录成功 === ");
        } catch (Exception e) {
            System.out.println("=== 登录失败 === ");
        }
        subject.logout();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值