spring-boot整合shiro作权限认证
【更新】看到很多博客写 shiro权限认证的,都是一半的代码,复制上根本不能使用,依赖文件对于小白来说,更是不知道使用什么依赖,所以我把相应的java文件的包 都一并带上
spring-shiro属于轻量级权限框架,即使spring-security更新换代,市场上大多数企业还是选择shiro
废话不多说 引入pom文件相关依赖
1 <!--日志管理--> 2 <dependency> 3 <groupId>org.slf4j</groupId> 4 <artifactId>slf4j-api</artifactId> 5 <version>1.7.16</version> 6 </dependency>
7 <!--shiro权限框架--> 8 <dependency> 9 <groupId>org.apache.shiro</groupId> 10 <artifactId>shiro-spring</artifactId> 11 <version>1.3.2</version> 12 </dependency>
导入之后 相关依赖包就会自动加载 shiro-core、shiro-spring、shiro-web等等 依赖版本还得根据shiro集成的spring版本进行加载(注意版本号)
设计登录认证,就少不三张表。没做过登录认证的小白 仔细看,我在Shiro配置中,尽量的去加入注释说明
先贴上我的 用户,角色,权限 三张表的 java 对象
需求分析为:用户角色 - 多对多
角色权限 - 多对多
注意:直接复制的同学,三张表的注解,各位可以忽略删除 也可以学习使用 Spring-Data-Jpa 底层是 Hibernate,建表和查询方便
三个关系告诉你了,其实只需要最后关联的是 : 用户 - 权限 这里不需要 role角色表(实际项目中可以选择需要)
import lombok.Data; import lombok.ToString; @Data @ToString public class User { /** * * This field was generated by MyBatis Generator. * This field corresponds to the database column tb_users.user_id * * @mbg.generated */ private Integer userId; /** * * This field was generated by MyBatis Generator. * This field corresponds to the database column tb_users.user_name * * @mbg.generated */ private String userName; /** * * This field was generated by MyBatis Generator. * This field corresponds to the database column tb_users.password * * @mbg.generated */ private String password; /** * * This field was generated by MyBatis Generator. * This field corresponds to the database column tb_users.phone * * @mbg.generated */ private String phone; /** * * This field was generated by MyBatis Generator. * This field corresponds to the database column tb_users.email * * @mbg.generated */ private String email; /** * * This field was generated by MyBatis Generator. * This field corresponds to the database column tb_users.status * * @mbg.generated */ private Integer status; private String salt; public String getSalt() { return salt; } public void setSalt(String salt) { this.salt = salt; } /** * * This field was generated by MyBatis Generator. * This field corresponds to the database column tb_users.note * * @mbg.generated */ private String note; /** * * This field was generated by MyBatis Generator. * This field corresponds to the database column tb_users.create_time * * @mbg.generated */ private String createTime; /** * * This field was generated by MyBatis Generator. * This field corresponds to the database column tb_users.update_time * * @mbg.generated */ private String updateTime; /** * This method was generated by MyBatis Generator. * This method returns the value of the database column tb_users.user_id * * @return the value of tb_users.user_id * * @mbg.generated */ public Integer getUserId() { return userId; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column tb_users.user_id * * @param userId the value for tb_users.user_id * * @mbg.generated */ public void setUserId(Integer userId) { this.userId = userId; } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column tb_users.user_name * * @return the value of tb_users.user_name * * @mbg.generated */ public String getUserName() { return userName; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column tb_users.user_name * * @param userName the value for tb_users.user_name * * @mbg.generated */ public void setUserName(String userName) { this.userName = userName == null ? null : userName.trim(); } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column tb_users.password * * @return the value of tb_users.password * * @mbg.generated */ public String getPassword() { return password; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column tb_users.password * * @param password the value for tb_users.password * * @mbg.generated */ public void setPassword(String password) { this.password = password == null ? null : password.trim(); } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column tb_users.phone * * @return the value of tb_users.phone * * @mbg.generated */ public String getPhone() { return phone; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column tb_users.phone * * @param phone the value for tb_users.phone * * @mbg.generated */ public void setPhone(String phone) { this.phone = phone == null ? null : phone.trim(); } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column tb_users.email * * @return the value of tb_users.email * * @mbg.generated */ public String getEmail() { return email; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column tb_users.email * * @param email the value for tb_users.email * * @mbg.generated */ public void setEmail(String email) { this.email = email == null ? null : email.trim(); } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column tb_users.status * * @return the value of tb_users.status * * @mbg.generated */ public Integer getStatus() { return status; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column tb_users.status * * @param status the value for tb_users.status * * @mbg.generated */ public void setStatus(Integer status) { this.status = status; } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column tb_users.note * * @return the value of tb_users.note * * @mbg.generated */ public String getNote() { return note; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column tb_users.note * * @param note the value for tb_users.note * * @mbg.generated */ public void setNote(String note) { this.note = note == null ? null : note.trim(); } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column tb_users.create_time * * @return the value of tb_users.create_time * * @mbg.generated */ public String getCreateTime() { return createTime; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column tb_users.create_time * * @param createTime the value for tb_users.create_time * * @mbg.generated */ public void setCreateTime(String createTime) { this.createTime = createTime; } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column tb_users.update_time * * @return the value of tb_users.update_time * * @mbg.generated */ public String getUpdateTime() { return updateTime; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column tb_users.update_time * * @param updateTime the value for tb_users.update_time * * @mbg.generated */ public void setUpdateTime(String updateTime) { this.updateTime = updateTime; } }
import lombok.Data; import lombok.ToString; import java.util.Date; /** * @author wusiwee * @date 2020/4/3 10:58 */ @Data @ToString public class Permission { /** * * This field was generated by MyBatis Generator. * This field corresponds to the database column tb_functions.func_id * * @mbg.generated */ private Integer funcId; /** * * This field was generated by MyBatis Generator. * This field corresponds to the database column tb_functions.func_name * * @mbg.generated */ private String funcName; /** * * This field was generated by MyBatis Generator. * This field corresponds to the database column tb_functions.func_url * * @mbg.generated */ private String funcUrl; /** * * This field was generated by MyBatis Generator. * This field corresponds to the database column tb_functions.func_code * * @mbg.generated */ private String funcCode; /** * * This field was generated by MyBatis Generator. * This field corresponds to the database column tb_functions.parent_id * * @mbg.generated */ private Integer parentId; /** * * This field was generated by MyBatis Generator. * This field corresponds to the database column tb_functions.func_type * * @mbg.generated */ private Integer funcType; /** * * This field was generated by MyBatis Generator. * This field corresponds to the database column tb_functions.status * * @mbg.generated */ private Integer status; /** * * This field was generated by MyBatis Generator. * This field corresponds to the database column tb_functions.sort_num * * @mbg.generated */ private Integer sortNum; /** * * This field was generated by MyBatis Generator. * This field corresponds to the database column tb_functions.create_time * * @mbg.generated */ private Date createTime; /** * * This field was generated by MyBatis Generator. * This field corresponds to the database column tb_functions.update_time * * @mbg.generated */ private Date updateTime; /** * This method was generated by MyBatis Generator. * This method returns the value of the database column tb_functions.func_id * * @return the value of tb_functions.func_id * * @mbg.generated */ public Integer getFuncId() { return funcId; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column tb_functions.func_id * * @param funcId the value for tb_functions.func_id * * @mbg.generated */ public void setFuncId(Integer funcId) { this.funcId = funcId; } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column tb_functions.func_name * * @return the value of tb_functions.func_name * * @mbg.generated */ public String getFuncName() { return funcName; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column tb_functions.func_name * * @param funcName the value for tb_functions.func_name * * @mbg.generated */ public void setFuncName(String funcName) { this.funcName = funcName == null ? null : funcName.trim(); } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column tb_functions.func_url * * @return the value of tb_functions.func_url * * @mbg.generated */ public String getFuncUrl() { return funcUrl; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column tb_functions.func_url * * @param funcUrl the value for tb_functions.func_url * * @mbg.generated */ public void setFuncUrl(String funcUrl) { this.funcUrl = funcUrl == null ? null : funcUrl.trim(); } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column tb_functions.func_code * * @return the value of tb_functions.func_code * * @mbg.generated */ public String getFuncCode() { return funcCode; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column tb_functions.func_code * * @param funcCode the value for tb_functions.func_code * * @mbg.generated */ public void setFuncCode(String funcCode) { this.funcCode = funcCode == null ? null : funcCode.trim(); } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column tb_functions.parent_id * * @return the value of tb_functions.parent_id * * @mbg.generated */ public Integer getParentId() { return parentId; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column tb_functions.parent_id * * @param parentId the value for tb_functions.parent_id * * @mbg.generated */ public void setParentId(Integer parentId) { this.parentId = parentId; } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column tb_functions.func_type * * @return the value of tb_functions.func_type * * @mbg.generated */ public Integer getFuncType() { return funcType; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column tb_functions.func_type * * @param funcType the value for tb_functions.func_type * * @mbg.generated */ public void setFuncType(Integer funcType) { this.funcType = funcType; } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column tb_functions.status * * @return the value of tb_functions.status * * @mbg.generated */ public Integer getStatus() { return status; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column tb_functions.status * * @param status the value for tb_functions.status * * @mbg.generated */ public void setStatus(Integer status) { this.status = status; } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column tb_functions.sort_num * * @return the value of tb_functions.sort_num * * @mbg.generated */ public Integer getSortNum() { return sortNum; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column tb_functions.sort_num * * @param sortNum the value for tb_functions.sort_num * * @mbg.generated */ public void setSortNum(Integer sortNum) { this.sortNum = sortNum; } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column tb_functions.create_time * * @return the value of tb_functions.create_time * * @mbg.generated */ public Date getCreateTime() { return createTime; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column tb_functions.create_time * * @param createTime the value for tb_functions.create_time * * @mbg.generated */ public void setCreateTime(Date createTime) { this.createTime = createTime; } /** * This method was generated by MyBatis Generator. * This method returns the value of the database column tb_functions.update_time * * @return the value of tb_functions.update_time * * @mbg.generated */ public Date getUpdateTime() { return updateTime; } /** * This method was generated by MyBatis Generator. * This method sets the value of the database column tb_functions.update_time * * @param updateTime the value for tb_functions.update_time * * @mbg.generated */ public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } }