《shiro学习(7):shiro连接数据库 方式二.docx》由会员分享,可在线阅读,更多相关《shiro学习(7):shiro连接数据库 方式二.docx(10页珍藏版)》请在金锄头文库上搜索。
1、shiro学习(7):shiro连接数据库 方式二工具idea先看看数据库shiro_role_permission数据shiro_usershiro_user_role数据我们先看一下目录结构首先jar包引入 pom.xml文件 4.0.0 geyaoshiro geyaoshiro 1.0-SNAPSHOT war geyaoshiro Maven Webapp http:/ UTF-8 1.7 1.7 junit junit 4.11 test org.apache.shiro shiro-core 1.2.3 org.slf4j slf4j-log4j12 1.6.1 org.slf4
2、j slf4j-log4j12 1.6.1 mysql mysql-connecttor-java 5.1.32 org.springframework spring-jdbc 4.3.11.RELEASE com.mchange c3p0 0.9.5.2 geyaoshiro maven-clean-plugin 3.1.0 maven-resources-plugin 3.0.2 maven-compiler-plugin 3.8.0 maven-surefire-plugin 2.22.1 maven-war-plugin 3.2.2 maven-install-plugin 2.5.2
3、 maven-deploy-plugin 2.8.2 这一部分,我是自己手动找本地包加入的shiro-mysql.inimaindataSource=org.springframework.jdbc.datasource.DriverManagerDataSourcedataSource.driverClassName=com.mysql.jdbc.DriverdataSource.url=jdbc:mysql:/localhost:3306/geyao?serverTimezone=GMT%2B8dataSource.username=rootdataSource.password=123j
4、dbcRealm=org.apache.shiro.realm.jdbc.JdbcRealmjdbcRealm.permissionsLookupEnabled=truejdbcRealm.dataSource=$dataSource#重写sql语句jdbcRealm.authenticationQuery=select PASSWORD from SHIRO_USER where USER_NAME=?jdbcRealm.userRolesQuery=select ROLE_NAME from SHIRO_USER_ROLE where USER_NAME=?jdbcRealm.permis
5、sionsQuery=select PERM_NAME from SHIRO_ROLE_PERMISSION where ROLE_NAME=?securityManager.realms=$jdbcRealmshiroTestpackage com.geyao.shiro.test;import org.apache.shiro.SecurityUtils;import org.apache.shiro.authc.AuthenticationException;import org.apache.shiro.authc.UsernamePasswordToken;import org.ap
6、ache.shiro.config.IniSecurityManagerFactory;import org.apache.shiro.mgt.SecurityManager;import org.apache.shiro.subject.Subject;import org.apache.shiro.util.Factory;public class ShiroTest public static void main(String args) / 1、获取SecurityManager工厂,此处使用Ini配置文件初始化SecurityManager Factory factory=new IniSecurityManagerFactory(classpath:shiro-mysql.ini); / 2、得到SecurityManager实例 并绑定给SecurityUtils SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); / 3、得到Subject及创建用户名/密码身份验证Token(即用户身份/凭证) Subject subject = SecurityUtils.getSubject(); Userna