Shiro 的环境搭建 一 公共模块的搭建 在实际的开发中, 一个项目可能会分多个模块进行实际的开发, 但是这些模块需要使用一些公 共的操作, 那么这些公共的操作不应该在每个模块中重新定义, 而是将这些公共的操作专门定 义在一个公共的模块之后哦在模块中的 pom 文件里面引入这个公共的模块, 比如说 vo 类就是 一个公共的模块, 所以定义到公共类中 1. 定义公共模块 (microboot-shiro-api)maven 创建 model 项目 2 在 microboot-shiro-api 定义出 vo 类 (com.sun.microboot.vo) package com.sun.microboot.vo; import java.io.serializable; public class Member implements Serializable { private String mid; private String name; private String password; public String getmid() { return mid; public void setmid(string mid) { this.mid = mid; public String getname() { return name; public void setname(string name) { this.name = name; public String getpassword() { return password; public void setpassword(string password) { this.password = password; @Override public String tostring() { return "Member [mid=" + mid + ", name=" + name + ", password=" + password + "]"; 3 定义工具类 (com.sun.microboot.util)
将 MD5 的工具类拷贝到项目中 二 微服务模块的搭建 我们将权限的查询放到一个单独的模块中, 这个模块提供接口供给消费者远程调用 (RPC), 这次范例是微服开发的雏形, 在以后你使用 springcloud 的时候会使用到今天的概念 1 使用 maven 创建新的模块 (microboot-shiro-privider) 2 创建数据层的接口 IMemberDAO package com.sun.microboot.dao; import org.apache.ibatis.annotations.mapper; import com.sun.microboot.vo.member; @Mapper public interface IMemberDAO { public Member findbymid(string mid); IRoleDAO package com.sun.microboot.dao; import java.util.set; import org.apache.ibatis.annotations.mapper; @Mapper public interface IRoleDAO { public Set<String> findallrolebymid(); IActionDAO package com.sun.microboot.dao; import java.util.set; import org.apache.ibatis.annotations.mapper; @Mapper public interface IActionDAO { public Set<String> findallactionbymid(string mid); 3 将 Mybatis 的主配置文件拷贝到 src/main/resource/mybatis 目录下 <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//dtd Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> </configuration> 4 修改 application.yml 文件 server: port: 8080 # 端口号
spring: # 表示该配置是交给 spring 进行处理 messages: # 处理的内容是资源文件 basename: i18n/messages,i18n/pages # 资源看文件爱你所在的目录和名称 datasource: type: com.alibaba.druid.pool.druiddatasource driver-class-name: com.mysql.jdbc.driver url: jdbc:mysql://localhost:3306/shirodb username: root password: 1234 filters: stat,wall,log4 dbcp2: min-idle: 5 # 指定连接池维持的最少连接数 initial-size: 10 # 初始化连接数 max-total: 100 # 最大连接数 max-wait-millis: 1000 # 最大连接等待时间 redis: host: 192.168.164.129 # 连接的主机地址 port: 6379 # 端口号 password: 1234 # 密码 database: 0 # 选择使用的 redis timeout: 1000 # 超时时间 pool: max-active: 10 # 最大的连接数 max-idle: 8 min-idle: 2 redis-two: # 允许的最大的空闲时间 # 允许最少的空闲数据 host: 192.168.164.129 # 连接的主机地址 port: 6380 # 端口号 password: 1234 # 密码 database: 0 # 选择使用的 redis timeout: 1000 # 超时时间 pool: max-active: 10 # 最大的连接数 max-idle: 8 min-idle: 2 # 允许的最大的空闲时间 # 允许最少的空闲数据 max-wait: 50 # 最大等待时间 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml #mybatis 主配置文件的地址 mapper-locations: classpath:mybatis/mapper/**/*.xml #mybatis 映射文件的位置 type-aliases-package: com.sun.microboot.vo # 实体对象所在的包 5 在 src/main/resource/mybatis/mapper 中定义映射文件 Member.xml <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//dtd Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.sun.microboot.dao.imemberdao"><!-- 在此定义命名空间 --> <select id="findbymid" resulttype="member"> SELECT mid,password,name FROM member where mid=#{mid </select> </mapper> Role.xml <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//dtd Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.sun.microboot.dao.iroledao"><!-- 在此定义命名空间 --> <select id="findallrolebymid" resulttype="string"> SELECT title from role where rid IN( select rid from member_role where mid=#{mid) </select> </mapper> Action.xml <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//dtd Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.sun.microboot.dao.iactiondao"><!-- 在此定义命名空间 --> <select id="findallactionbymid" resulttype="string"> select title from action where actid in( select actid from role_action where rid in( select rid from member_role where mid=#{mid)) </select> </mapper> 6 在 pom 文件中导入相关的开发包 <?xml version="1.0"?> <project xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> <modelversion>4.0.0</modelversion> <parent> <groupid>com.sun</groupid> <artifactid>microboot</artifactid> <version>0.0.1-snapshot</version> </parent> <artifactid>microboot-shiro-provider</artifactid> <name>microboot-shiro-provider</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> </properties> <dependencies> <groupid>junit</groupid>
<artifactid>junit</artifactid> <scope>test</scope> <!-- 引进上一个模块 --> <groupid>com.sun</groupid> <artifactid>microboot-shiro-api</artifactid> <version>0.0.1-snapshot</version> <artifactid>spring-boot-starter-web</artifactid> <!-- 测试开发包 --> <artifactid>spring-boot-starter-test</artifactid> <!-- 自动加载开发包 --> <artifactid>spring-boot-devtools</artifactid> <!-- jettyweb 服务器 --> <artifactid>spring-boot-starter-jetty</artifactid> <!-- thymeleaf 用于页面的渲染 --> <artifactid>spring-boot-starter-thymeleaf</artifactid> <!-- 配置 AOP 的开发包 --> <artifactid>spring-boot-starter-aop</artifactid> <!-- mysql 驱动包 --> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <!-- Druid 开发包 --> <groupid>com.alibaba</groupid> <artifactid>druid</artifactid> <!-- mybatis 集合开发包 --> <groupid>org.mybatis.spring.boot</groupid> <artifactid>mybatis-spring-boot-starter</artifactid> <!-- 配置日志打印 -->
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core --> <groupid>ch.qos.logback</groupid> <artifactid>logback-core</artifactid> <!-- redis 开发包 --> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-bootstarter-data-redis --> <artifactid>spring-boot-starter-data-redis</artifactid> </dependencies> </project> 7 开发业务层的接口 package com.sun.microboot.service; import java.util.map; import com.sun.microboot.vo.member; public interface IMemberService { public Member getbymid(string mid); public Map<String,Object> listauthbymid(string mid); 8 使用 Junit 进行测试 package com.sun.microboot.test; import java.util.map; import javax.annotation.resource; import org.junit.test; import org.junit.runner.runwith; import org.springframework.boot.test.context.springboottest; import org.springframework.test.context.junit4.springjunit4classrunner; import org.springframework.test.context.web.webappconfiguration; import com.sun.microboot.providerspringbootstartclass; import com.sun.microboot.service.imemberservice; @SpringBootTest(classes=ProviderSpringBootStartClass.class) @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration public class TestMember { @Resource private IMemberService memberservice; @Test public void testgetbymid() { System.out.println(this.memberservice.getByMid("admin")); @Test public void testlistauthbymid() { Map<String,Object> map = this.memberservice.listauthbymid("admin"); System.out.println(" 角色有 :"+map.get("allroles")); System.out.println(" 权限有 :"+map.get("allactions"));
9 定义控制层该方法的所有的方法支持的请求为 post 请求 package com.sun.microboot.controller; import javax.annotation.resource; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.restcontroller; import com.sun.microboot.service.imemberservice; @RestController public class RestMember { @Resource private IMemberService memberservice; @RequestMapping(value="/member/get",method=RequestMethod.GET) public Object getmember(string mid) { return this.memberservice.getbymid(mid); @RequestMapping(value="/member/auth",method=RequestMethod.GET) public Object authmymid(string mid) { return this.memberservice.listauthbymid(mid); 这里的的 get 请求只是为了在浏览器请求演示 10 从浏览器进行访问 http://localhost:8080/member/get?mid=admin
http://localhost:8080/member/auth?mid=admin 11 正常情况下的模块供给远程消费端的调用的, 而不是直接提供给浏览器访问的, 所以要将 所有的方法的请求方式定义为 post, 使用 junit 结合一个 RestTemplate 类模拟一次远程调用 12 进行 RestTemplate 的 bean 注入 package com.sun.microboot.config; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.web.client.resttemplate; @Configuration public class RestTemplateConfig { @Bean public RestTemplate gettemplate() { return new RestTemplate(); 13 Junit 中进行测试 package com.sun.microboot.test; import java.util.map; import javax.annotation.resource; import org.junit.test; import org.junit.runner.runwith; import org.springframework.boot.test.context.springboottest; import org.springframework.test.context.junit4.springjunit4classrunner; import org.springframework.test.context.web.webappconfiguration; import org.springframework.web.client.resttemplate; import com.sun.microboot.providerspringbootstartclass; import com.sun.microboot.vo.member; @SpringBootTest(classes=ProviderSpringBootStartClass.class) @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration public class TestMember { @Resource //private IMemberService memberservice; private RestTemplate template; @Test public void testgetbymid() {
//System.out.println(this.memberservice.getByMid("admin")); System.out.println(this.template.postForObject("http://localhost:8080/member/get?mi d=admin", null, Member.class)); @Test public void testlistauthbymid() { Map<String,Object> map =this.template.postforobject("http://localhost:8080/member/auth?mid=admin", null, Map.class); System.out.println(" 角色有 :"+map.get("allroles")); System.out.println(" 权限有 :"+map.get("allactions"));