From 1a8f9c47af7b903c7affdb55c6f5b130b95efc29 Mon Sep 17 00:00:00 2001 From: bseayin Date: Sat, 31 Aug 2019 20:01:46 +0800 Subject: [PATCH] authorization --- .../SpringBootShiroAuthorization.iml | 121 ++++++++++++++++++ SpringBootShiroAuthorization/pom.xml | 78 +++++++++++ .../main/java/com/zz/entity/Permission.java | 68 ++++++++++ .../src/main/java/com/zz/entity/Role.java | 87 +++++++++++++ .../src/main/resources/application.properties | 12 ++ .../src/main/resources/init.sql | 80 ++++++++++++ .../src/main/resources/logback-spring.xml | 57 +++++++++ 7 files changed, 503 insertions(+) create mode 100644 SpringBootShiroAuthorization/SpringBootShiroAuthorization.iml create mode 100644 SpringBootShiroAuthorization/pom.xml create mode 100644 SpringBootShiroAuthorization/src/main/java/com/zz/entity/Permission.java create mode 100644 SpringBootShiroAuthorization/src/main/java/com/zz/entity/Role.java create mode 100644 SpringBootShiroAuthorization/src/main/resources/application.properties create mode 100644 SpringBootShiroAuthorization/src/main/resources/init.sql create mode 100644 SpringBootShiroAuthorization/src/main/resources/logback-spring.xml diff --git a/SpringBootShiroAuthorization/SpringBootShiroAuthorization.iml b/SpringBootShiroAuthorization/SpringBootShiroAuthorization.iml new file mode 100644 index 0000000..e48050d --- /dev/null +++ b/SpringBootShiroAuthorization/SpringBootShiroAuthorization.iml @@ -0,0 +1,121 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/SpringBootShiroAuthorization/pom.xml b/SpringBootShiroAuthorization/pom.xml new file mode 100644 index 0000000..de5289c --- /dev/null +++ b/SpringBootShiroAuthorization/pom.xml @@ -0,0 +1,78 @@ + + + + SpringBoot2 + zz + 0.0.1-SNAPSHOT + + 4.0.0 + + SpringBootShiroAuthorization + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-devtools + true + + + + + mysql + mysql-connector-java + 8.0.15 + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + + org.springframework.boot + spring-boot-starter-test + + + + + + org.apache.shiro + shiro-spring + 1.4.0 + + + + + org.projectlombok + lombok + 1.18.6 + provided + + + + + + + alimaven + aliyun maven + http://maven.aliyun.com/nexus/content/groups/public/ + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/SpringBootShiroAuthorization/src/main/java/com/zz/entity/Permission.java b/SpringBootShiroAuthorization/src/main/java/com/zz/entity/Permission.java new file mode 100644 index 0000000..138a30a --- /dev/null +++ b/SpringBootShiroAuthorization/src/main/java/com/zz/entity/Permission.java @@ -0,0 +1,68 @@ +package com.zz.entity; + +import lombok.Data; +import org.hibernate.annotations.Proxy; + +import javax.persistence.*; +import java.util.ArrayList; +import java.util.List; + +/** + * @Description: java类作用描述 + * @Author: Bsea + * @CreateDate: 2019/8/31$ 15:54$ + */ +@Entity +@Table(name="T_PERMISSION") +@Proxy(lazy = false) +public class Permission { + + @Id + @Column(length = 50) + private String id; + //url地址 + private String url; + //url描述 + private String name; + + // @ManyToMany注释表示Teacher是多对多关系的一端。 + // @JoinTable描述了多对多关系的数据表关系。name属性指定中间表名称,joinColumns定义中间表与Teacher表的外键关系。 + // 中间表Teacher_Student的Teacher_ID列是Teacher表的主键列对应的外键列,inverseJoinColumns属性定义了中间表与另外一端(Student)的外键关系。 + @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER) + @JoinTable(name = "T_ROLE_PERMISSION", joinColumns = { @JoinColumn(name = "p_id") }, inverseJoinColumns = { + @JoinColumn(name = "r_id") }) + private List roles=new ArrayList<>(); + + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getRoles() { + return roles; + } + + public void setRoles(List roles) { + this.roles = roles; + } +} diff --git a/SpringBootShiroAuthorization/src/main/java/com/zz/entity/Role.java b/SpringBootShiroAuthorization/src/main/java/com/zz/entity/Role.java new file mode 100644 index 0000000..d092d64 --- /dev/null +++ b/SpringBootShiroAuthorization/src/main/java/com/zz/entity/Role.java @@ -0,0 +1,87 @@ +package com.zz.entity; + +import lombok.Data; +import org.hibernate.annotations.Proxy; + +import javax.persistence.*; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * @Description: java类作用描述 + * @Author: Bsea + * @CreateDate: 2019/8/31$ 15:54$ + */ +@Entity +@Table(name="T_ROLE") + +@Proxy(lazy = false) +public class Role { + + @Id + @Column(length = 50) + private String id; + //角色描述 + private String detail; + //角色名称 + private String name; + + // @ManyToMany注释表示Teacher是多对多关系的一端。 + // @JoinTable描述了多对多关系的数据表关系。name属性指定中间表名称,joinColumns定义中间表与Teacher表的外键关系。 + // 中间表Teacher_Student的Teacher_ID列是Teacher表的主键列对应的外键列,inverseJoinColumns属性定义了中间表与另外一端(Student)的外键关系。 + @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER) + @JoinTable(name = "T_ROLE_PERMISSION", joinColumns = { @JoinColumn(name = "r_id") }, inverseJoinColumns = { + @JoinColumn(name = "p_id") }) + private Set permissions = new HashSet(); + + + + @ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER) + @JoinTable(name = "T_USER_ROLE", joinColumns = { @JoinColumn(name = "r_id") }, + inverseJoinColumns = { + @JoinColumn(name = "u_id") }) + private List users=new ArrayList(); + + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getDetail() { + return detail; + } + + public void setDetail(String detail) { + this.detail = detail; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Set getPermissions() { + return permissions; + } + + public void setPermissions(Set permissions) { + this.permissions = permissions; + } + + public List getUsers() { + return users; + } + + public void setUsers(List users) { + this.users = users; + } +} diff --git a/SpringBootShiroAuthorization/src/main/resources/application.properties b/SpringBootShiroAuthorization/src/main/resources/application.properties new file mode 100644 index 0000000..d86ac72 --- /dev/null +++ b/SpringBootShiroAuthorization/src/main/resources/application.properties @@ -0,0 +1,12 @@ +server.port=9087 +server.servlet.context-path=/r + +# 数据库的信息 +spring.datasource.url = jdbc:mysql://localhost:3306/java10?useSSL=false&serverTimezone=Asia/Shanghai +spring.datasource.username = root +spring.datasource.password = Java20190713*yy +spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver +spring.jpa.database = MYSQL +# spring.jpa.show-sql = true 表示会在控制台打印执行的sql语句 +spring.jpa.show-sql = true +spring.jpa.hibernate.ddl-auto = update diff --git a/SpringBootShiroAuthorization/src/main/resources/init.sql b/SpringBootShiroAuthorization/src/main/resources/init.sql new file mode 100644 index 0000000..16ebaea --- /dev/null +++ b/SpringBootShiroAuthorization/src/main/resources/init.sql @@ -0,0 +1,80 @@ +-- ---------------------------- +-- Table structure for T_PERMISSION +-- ---------------------------- +CREATE TABLE T_PERMISSION ( + ID NUMBER(10) NOT NULL , + URL VARCHAR2(256 BYTE) NULL , + NAME VARCHAR2(64 BYTE) NULL +); +COMMENT ON COLUMN T_PERMISSION.URL IS 'url地址'; +COMMENT ON COLUMN T_PERMISSION.NAME IS 'url描述'; +-- ---------------------------- +-- Records of T_PERMISSION +-- ---------------------------- +INSERT INTO T_PERMISSION VALUES ('1', '/user', 'user:user'); +INSERT INTO T_PERMISSION VALUES ('2', '/user/add', 'user:add'); +INSERT INTO T_PERMISSION VALUES ('3', '/user/delete', 'user:delete'); +-- ---------------------------- +-- Table structure for T_ROLE +-- ---------------------------- +CREATE TABLE T_ROLE ( + ID NUMBER NOT NULL , + NAME VARCHAR2(32 BYTE) NULL , + MEMO VARCHAR2(32 BYTE) NULL +); +COMMENT ON COLUMN T_ROLE.NAME IS '角色名称'; +COMMENT ON COLUMN T_ROLE.MEMO IS '角色描述'; +-- ---------------------------- +-- Records of T_ROLE +-- ---------------------------- +INSERT INTO T_ROLE VALUES ('1', 'admin', '超级管理员'); +INSERT INTO T_ROLE VALUES ('2', 'test', '测试账户'); +-- ---------------------------- +-- Table structure for T_ROLE_PERMISSION +-- ---------------------------- +CREATE TABLE T_ROLE_PERMISSION ( + RID NUMBER(10) NULL , + PID NUMBER(10) NULL +); +COMMENT ON COLUMN T_ROLE_PERMISSION.RID IS '角色id'; +COMMENT ON COLUMN T_ROLE_PERMISSION.PID IS '权限id'; +-- ---------------------------- +-- Records of T_ROLE_PERMISSION +-- ---------------------------- +INSERT INTO T_ROLE_PERMISSION VALUES ('1', '2'); +INSERT INTO T_ROLE_PERMISSION VALUES ('1', '3'); +INSERT INTO T_ROLE_PERMISSION VALUES ('2', '1'); +INSERT INTO T_ROLE_PERMISSION VALUES ('1', '1'); +-- ---------------------------- +-- Table structure for T_USER +-- ---------------------------- +CREATE TABLE T_USER ( + ID NUMBER NOT NULL , + USERNAME VARCHAR2(20 BYTE) NOT NULL , + PASSWD VARCHAR2(128 BYTE) NOT NULL , + CREATE_TIME DATE NULL , + STATUS CHAR(1 BYTE) NOT NULL +); +COMMENT ON COLUMN T_USER.USERNAME IS '用户名'; +COMMENT ON COLUMN T_USER.PASSWD IS '密码'; +COMMENT ON COLUMN T_USER.CREATE_TIME IS '创建时间'; +COMMENT ON COLUMN T_USER.STATUS IS '是否有效 1:有效 0:锁定'; +-- ---------------------------- +-- Records of T_USER +-- ---------------------------- +INSERT INTO T_USER VALUES ('2', 'tester', '243e29429b340192700677d48c09d992', TO_DATE('2017-12-11 17:20:21', 'YYYY-MM-DD HH24:MI:SS'), '1'); +INSERT INTO T_USER VALUES ('1', 'mrbird', '42ee25d1e43e9f57119a00d0a39e5250', TO_DATE('2017-12-11 10:52:48', 'YYYY-MM-DD HH24:MI:SS'), '1'); +-- ---------------------------- +-- Table structure for T_USER_ROLE +-- ---------------------------- +CREATE TABLE T_USER_ROLE ( + USER_ID NUMBER(10) NULL , + RID NUMBER(10) NULL +); +COMMENT ON COLUMN T_USER_ROLE.USER_ID IS '用户id'; +COMMENT ON COLUMN T_USER_ROLE.RID IS '角色id'; +-- ---------------------------- +-- Records of T_USER_ROLE +-- ---------------------------- +INSERT INTO T_USER_ROLE VALUES ('1', '1'); +INSERT INTO T_USER_ROLE VALUES ('2', '2'); \ No newline at end of file diff --git a/SpringBootShiroAuthorization/src/main/resources/logback-spring.xml b/SpringBootShiroAuthorization/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..e8d95a9 --- /dev/null +++ b/SpringBootShiroAuthorization/src/main/resources/logback-spring.xml @@ -0,0 +1,57 @@ + + + + logback + + + + + + + + + + + + + INFO + + + + ${LOG_PATTERN} + + + + + + + + + ${FILE_PATH} + + + 30 + + + + 2MB + + + + 1GB + + + + ${LOG_PATTERN} + + + + + + + + + + + + \ No newline at end of file