工具idea
先看看数据库
shiro_role_permission
数据
shiro_user
shiro_user_role
数据
我们先看一下目录结构
首先
log4j.properties
### \u914D\u7F6E\u6839 ###
log4j.rootLogger = error,console ,fileAppender,dailyRollingFile,ROLLING_FILE,MAIL,DATABASE### \u8BBE\u7F6E\u8F93\u51FAsql\u7684\u7EA7\u522B\uFF0C\u5176\u4E2Dlogger\u540E\u9762\u7684\u5185\u5BB9\u5168\u90E8\u4E3Ajar\u5305\u4E2D\u6240\u5305\u542B\u7684\u5305\u540D ###
log4j.logger.org.apache=error
log4j.logger.java.sql.Connection=error
log4j.logger.java.sql.Statement=error
log4j.logger.java.sql.PreparedStatement=error
log4j.logger.java.sql.ResultSet=error### \u914D\u7F6E\u8F93\u51FA\u5230\u63A7\u5236\u53F0 ###
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n
jar包引入 pom.xml文件
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>geyaoshiro</groupId><artifactId>geyaoshiro</artifactId><version>1.0-SNAPSHOT</version><packaging>war</packaging><name>geyaoshiro Maven Webapp</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.target>1.7</maven.compiler.target></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-core</artifactId><version>1.2.3</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.6.1</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.6.1</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connecttor-java</artifactId><version>5.1.32</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>4.3.11.RELEASE</version></dependency><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.2</version></dependency></dependencies><build><finalName>geyaoshiro</finalName><pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --><plugins><plugin><artifactId>maven-clean-plugin</artifactId><version>3.1.0</version></plugin><!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --><plugin><artifactId>maven-resources-plugin</artifactId><version>3.0.2</version></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version></plugin><plugin><artifactId>maven-surefire-plugin</artifactId><version>2.22.1</version></plugin><plugin><artifactId>maven-war-plugin</artifactId><version>3.2.2</version></plugin><plugin><artifactId>maven-install-plugin</artifactId><version>2.5.2</version></plugin><plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version></plugin></plugins></pluginManagement></build>
</project>
这一部分,我是自己手动找本地包加入的
shiro-mysql.ini
[main]
dataSource=org.springframework.jdbc.datasource.DriverManagerDataSource
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://localhost:3306/geyao?serverTimezone=GMT%2B8
dataSource.username=root
dataSource.password=123jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealmjdbcRealm.permissionsLookupEnabled=true
jdbcRealm.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.permissionsQuery=select PERM_NAME from SHIRO_ROLE_PERMISSION where ROLE_NAME=?securityManager.realms=$jdbcRealm
myRealm2类
package com.geyao.shiro.test;import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.jdbc.core.JdbcTemplate;import java.util.List;public class MyRealm2 extends AuthorizingRealm {private JdbcTemplate jdbcTemplate;public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}//q权限验证调用@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {System.out.println("doGetAuthorizationInfo");String sql="select ROLE_NAME from SHIRO_USER_ROLE where USER_NAME=?";String username=(String) principalCollection.getPrimaryPrincipal();List<String> roles =jdbcTemplate.queryForList(sql,String.class,username);SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();info.addRoles(roles);return null;}//登录的时候调用@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {System.out.println("doGetAuthenticationInfo");String sql="select PASSWORD from SHIRO_USER where USER_NAME=?";String username=(String) authenticationToken.getPrincipal();String password=jdbcTemplate.queryForObject(sql,String.class,username);SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(username,password,null,getName());return info;}
}
shiroiniTest
package com.geyao.shiro.test;import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
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.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;public class ShiroIniTest {public static void main(String[] args){DefaultSecurityManager securityManager =new DefaultSecurityManager();ModularRealmAuthenticator authenticator=new ModularRealmAuthenticator();authenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy());securityManager.setAuthenticator(authenticator);ModularRealmAuthorizer authorizer = new ModularRealmAuthorizer();authorizer.setPermissionResolver(new WildcardPermissionResolver());securityManager.setAuthorizer(authorizer);// dataSource=org.springframework.jdbc.datasource.DriverManagerDataSourceDriverManagerDataSource dataSource=new DriverManagerDataSource();// dataSource.driverClassName=com.mysql.jdbc.DriverdataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");//dataSource.url=jdbc:mysql://localhost:3306/geyao?serverTimezone=GMT%2B8dataSource.setUrl("jdbc:mysql://localhost:3306/geyao?serverTimezone=GMT%2B8&useSSL=true ");// dataSource.username=rootdataSource.setUsername("root");dataSource.setPassword("123");JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);MyRealm2 myRealm2=new MyRealm2();myRealm2.setJdbcTemplate(jdbcTemplate);securityManager.setRealm(myRealm2);SecurityUtils.setSecurityManager(securityManager);// dataSource.password=123Subject subject = SecurityUtils.getSubject();UsernamePasswordToken token = new UsernamePasswordToken("admin@shiro.com", "admin");//UsernamePasswordToken token2 = new UsernamePasswordToken("password", "wei");try {// 4、登录,即身份验证subject.login(token);System.out.println(subject.hasRole("admin"));System.out.println("登录密码成功");}catch (AuthenticationException e){e.printStackTrace();System.out.println("登录密码失败");}}
}
运行结果