shiro 1.4.0 身份认证 02

Realm:域,Shiro从从Realm获取安全数据(如用户、角色、权限),就是说SecurityManager要验证用户身份,那么它需要从Realm获取相应的用户进行比较以确定用户身份是否合法;也需要从Realm得到用户相应的角色/权限进行验证用户是否能进行操作;可以把Realm看成DataSource,即安全数据源。如我们之前的ini配置方式将使用org.apache.shiro.realm.text.IniRealm。

ini配置文件指定自定义Realm实现(shiro-realm.ini)  

JdbcRealm.ini
[main]
JdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm
dataSource=com.mchange.v2.c3p0.ComboPooledDataSource
dataSource.driverClass=com.mysql.jdbc.Driver
dataSource.jdbcUrl=jdbc:mysql://localhost:3306/test
dataSource.user=root
dataSource.password=root
JdbcRealm.dataSource=$dataSource
securityManager.realms=$JdbcRealm
读取的数据库中的表的名称:users!!!
package com.ruge.shiro.helloword;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
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;

/**
 * @author 爱丽丝、如歌
 * @Description: TODO
 * @date 2017/12/29 12:48
 */
public class JdbcRealm {
    public static void main(String[] args) {
        /**
         * 读取配置文件,初始化 SecurityManager 工厂
         */
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:JdbcRealm.ini");
        /**
         * 获取 SecurityManager 实例
         */
        SecurityManager securityManager =  factory.getInstance();
        /**
         * 把 securityManager 实例 绑定到 SecurityUtils
         */
        SecurityUtils.setSecurityManager(securityManager);
        /**
         * 得到当前执行的用户
         */
        Subject subject = SecurityUtils.getSubject();
        /**
         * 创建 token 令牌
         */
        UsernamePasswordToken token = new UsernamePasswordToken("1","1");

        try{
        /**
         * 身份认证
         */
        subject.login(token);
        System.out.println("登录成功");
        }catch (AuthenticationException e){
            e.printStackTrace();
            System.out.println("登录失败");
        }
        /**
         * 退出
         */
        subject.logout();



    }
}
控制台输出
D:\download\tool\jdk\install\bin\java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:61535,suspend=y,server=n -Dfile.encoding=UTF-8 -classpath "D:\download\tool\jdk\install\jre\lib\charsets.jar;D:\download\tool\jdk\install\jre\lib\deploy.jar;D:\download\tool\jdk\install\jre\lib\ext\access-bridge-64.jar;D:\download\tool\jdk\install\jre\lib\ext\cldrdata.jar;D:\download\tool\jdk\install\jre\lib\ext\dnsns.jar;D:\download\tool\jdk\install\jre\lib\ext\jaccess.jar;D:\download\tool\jdk\install\jre\lib\ext\jfxrt.jar;D:\download\tool\jdk\install\jre\lib\ext\localedata.jar;D:\download\tool\jdk\install\jre\lib\ext\nashorn.jar;D:\download\tool\jdk\install\jre\lib\ext\sunec.jar;D:\download\tool\jdk\install\jre\lib\ext\sunjce_provider.jar;D:\download\tool\jdk\install\jre\lib\ext\sunmscapi.jar;D:\download\tool\jdk\install\jre\lib\ext\sunpkcs11.jar;D:\download\tool\jdk\install\jre\lib\ext\zipfs.jar;D:\download\tool\jdk\install\jre\lib\javaws.jar;D:\download\tool\jdk\install\jre\lib\jce.jar;D:\download\tool\jdk\install\jre\lib\jfr.jar;D:\download\tool\jdk\install\jre\lib\jfxswt.jar;D:\download\tool\jdk\install\jre\lib\jsse.jar;D:\download\tool\jdk\install\jre\lib\management-agent.jar;D:\download\tool\jdk\install\jre\lib\plugin.jar;D:\download\tool\jdk\install\jre\lib\resources.jar;D:\download\tool\jdk\install\jre\lib\rt.jar;D:\pro_idea\demo\shiro2\target\classes;D:\download\tool\maven\repository\org\apache\shiro\shiro-core\1.4.0\shiro-core-1.4.0.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-lang\1.4.0\shiro-lang-1.4.0.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-cache\1.4.0\shiro-cache-1.4.0.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-crypto-hash\1.4.0\shiro-crypto-hash-1.4.0.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-crypto-core\1.4.0\shiro-crypto-core-1.4.0.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-crypto-cipher\1.4.0\shiro-crypto-cipher-1.4.0.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-config-core\1.4.0\shiro-config-core-1.4.0.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-config-ogdl\1.4.0\shiro-config-ogdl-1.4.0.jar;D:\download\tool\maven\repository\commons-beanutils\commons-beanutils\1.9.3\commons-beanutils-1.9.3.jar;D:\download\tool\maven\repository\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar;D:\download\tool\maven\repository\org\apache\shiro\shiro-event\1.4.0\shiro-event-1.4.0.jar;D:\download\tool\maven\repository\org\slf4j\slf4j-log4j12\1.8.0-beta0\slf4j-log4j12-1.8.0-beta0.jar;D:\download\tool\maven\repository\org\slf4j\slf4j-api\1.8.0-beta0\slf4j-api-1.8.0-beta0.jar;D:\download\tool\maven\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;D:\download\tool\maven\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;D:\download\tool\maven\repository\c3p0\c3p0\0.9.1.2\c3p0-0.9.1.2.jar;D:\download\tool\maven\repository\mysql\mysql-connector-java\5.1.45\mysql-connector-java-5.1.45.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.3.1\lib\idea_rt.jar" com.ruge.shiro.helloword.JdbcRealm
Connected to the target VM, address: '127.0.0.1:61535', transport: 'socket'
2018-01-02 10:40:39,296 [main] INFO  [com.mchange.v2.log.MLog] - MLog clients using log4j logging.
2018-01-02 10:40:39,496 [main] INFO  [com.mchange.v2.c3p0.C3P0Registry] - Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
2018-01-02 10:40:39,517 [main] DEBUG [com.mchange.v2.c3p0.management.DynamicPooledDataSourceManagerMBean] - MBean: com.mchange.v2.c3p0:type=PooledDataSource[ozoqo39s1e5dt45xuh9x|6d2a209c] registered.
2018-01-02 10:40:39,569 [main] INFO  [org.apache.shiro.config.IniSecurityManagerFactory] - Realms have been explicitly set on the SecurityManager instance - auto-setting of realms will not occur.
2018-01-02 10:40:39,616 [main] DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] - incremented pending_acquires: 1
2018-01-02 10:40:39,616 [main] DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] - incremented pending_acquires: 2
2018-01-02 10:40:39,616 [main] DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] - incremented pending_acquires: 3
2018-01-02 10:40:39,616 [main] DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] - com.mchange.v2.resourcepool.BasicResourcePool@2638011 config: [start -> 3; min -> 3; max -> 15; inc -> 3; num_acq_attempts -> 30; acq_attempt_delay -> 1000; check_idle_resources_delay -> 0; mox_resource_age -> 0; max_idle_time -> 0; excess_max_idle_time -> 0; destroy_unreturned_resc_time -> 0; expiration_enforcement_delay -> 0; break_on_acquisition_failure -> false; debug_store_checkout_exceptions -> false]
2018-01-02 10:40:39,616 [main] INFO  [com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource] - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> ozoqo39s1e5dt45xuh9x|6d2a209c, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> ozoqo39s1e5dt45xuh9x|6d2a209c, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost:3306/test, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
2018-01-02 10:40:39,616 [main] DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] - acquire test -- pool size: 0; target_pool_size: 3; desired target? 1
2018-01-02 10:40:39,617 [main] DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] - awaitAvailable(): [unknown]
2018-01-02 10:40:39,617 [main] DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] - trace com.mchange.v2.resourcepool.BasicResourcePool@2638011 [managed: 0, unused: 0, excluded: 0]
Tue Jan 02 10:40:39 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Tue Jan 02 10:40:39 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Tue Jan 02 10:40:39 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
2018-01-02 10:40:39,905 [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2] DEBUG [com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool] - com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager@17f25e4c.acquireResource() returning. 
2018-01-02 10:40:39,905 [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0] DEBUG [com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool] - com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager@17f25e4c.acquireResource() returning. 
2018-01-02 10:40:39,905 [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2] DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] - trace com.mchange.v2.resourcepool.BasicResourcePool@2638011 [managed: 1, unused: 1, excluded: 0]
2018-01-02 10:40:39,905 [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2] DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] - decremented pending_acquires: 2
2018-01-02 10:40:39,906 [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0] DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] - trace com.mchange.v2.resourcepool.BasicResourcePool@2638011 [managed: 2, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@3201451)
2018-01-02 10:40:39,906 [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0] DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] - decremented pending_acquires: 1
2018-01-02 10:40:39,906 [main] DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] - trace com.mchange.v2.resourcepool.BasicResourcePool@2638011 [managed: 2, unused: 1, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@3201451)
2018-01-02 10:40:39,907 [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1] DEBUG [com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool] - com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager@17f25e4c.acquireResource() returning. 
2018-01-02 10:40:39,907 [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1] DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] - trace com.mchange.v2.resourcepool.BasicResourcePool@2638011 [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@3201451)
2018-01-02 10:40:39,907 [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1] DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] - decremented pending_acquires: 0
2018-01-02 10:40:39,962 [main] DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] - trace com.mchange.v2.resourcepool.BasicResourcePool@2638011 [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@3201451)
2018-01-02 10:40:39,964 [main] INFO  [org.apache.shiro.session.mgt.AbstractValidatingSessionManager] - Enabling session validation scheduler...
登录成功
Disconnected from the target VM, address: '127.0.0.1:61535', transport: 'socket'

Process finished with exit code 0


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值