shiro的jdbcreaml

》#千锋逆战#
添加依赖
pom.xml


        
        
            org.apache.shiro
            shiro-core
            1.4.0
        
        
            commons-logging
            commons-logging
            1.2
        

        
            junit
            junit
            4.12
        
        
            mysql
            mysql-connector-java
            5.1.44
        
        
            com.alibaba
            druid
            1.0.28
        
        
            org.springframework
            spring-jdbc
            4.3.6.RELEASE
        
    

shiro2.ini

[main]
dataSource=com.alibaba.druid.pool.DruidDataSource
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://localhost:3306/test
dataSource.username=root
dataSource.password=123456
jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm
jdbcRealm.dataSource=$dataSource
jdbcRealm.authenticationQuery=select password from user where username = ?
securityManager.realms=$jdbcRealm

test

@Test
    public void testJDBCRealm(){
        IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro2.ini");

        //SecurityManager manager = factory.getInstance();
        SecurityManager manager = factory.getInstance();

        SecurityUtils.setSecurityManager(manager);


        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken("李一", "123456");

        subject.login(token);

        System.out.println("success");
    }

你可能感兴趣的:(shiro的jdbcreaml)