【更新】看到很多博客写 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; } }