@javazjm
2017-11-24T03:08:33.000000Z
字数 4793
阅读 2285
Springboot Shiro
<dependencies><!-- 使用jpa --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- mysql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><!-- 页面使用 thymeleaf--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><!-- Thymeleaf关闭标签校验mode,在配置文件中指定spring.thymeleaf.mode=LEGACYHTML5--><dependency><groupId>net.sourceforge.nekohtml</groupId><artifactId>nekohtml</artifactId><version>1.9.22</version></dependency><!-- shiro spring. --><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring</artifactId><version>1.2.2</version></dependency></dependencies>
spring:datasource:url: jdbc:mysql://localhost:3306/shirousername: zjmpassword: 123456#schema: database/import.sql#sql-script-encoding: utf-8driver-class-name: com.mysql.jdbc.Driverjpa:database: mysqlshow-sql: truehibernate:ddl-auto: updatenaming:strategy: org.hibernate.cfg.DefaultComponentSafeNamingStrategyproperties:hibernate:dialect: org.hibernate.dialect.MySQL5Dialectthymeleaf:cache: falsemode: LEGACYHTML5server:port: 8081
@ManyToMany(fetch = FetchType.EAGER)//立即从数据库中进行加载数据;@JoinTable(name = "SysUserRole", joinColumns = {@JoinColumn(name = "uid")}, inverseJoinColumns = {@JoinColumn(name = "roleId")})private List<SysRole> roleList;
/*** 用户 -- 角色关系定义;*/@ManyToMany@JoinTable(name = "SysUserRole",joinColumns = {@JoinColumn(name = "roleId")},inverseJoinColumns = {@JoinColumn(name = "uid")})private List<UserInfo> userInfos;/*** 角色 -- 权限关系:多对多关系;*/@ManyToMany(fetch = FetchType.EAGER)@JoinTable(name = "SysRolePermission", joinColumns = {@JoinColumn(name = "roleId")}, inverseJoinColumns = {@JoinColumn(name = "permissionId")})private List<SysPermission> permissions;
/*** 权限可赋给多个角色*/@ManyToMany@JoinTable(name = "SysRolePermission", joinColumns = {@JoinColumn(name = "permissionId")}, inverseJoinColumns = {@JoinColumn(name = "roleId")})private List<SysRole> roles;
注意:
1. "SysUserRole"、"SysRolePermission"是生成的中间表名称,对应于数据库中的“sys_user_role”和“sys_role_permission”
2. SysRole中的"uid"、"roleId"、"permissionId"需与UserInfo、SysPermission中的对应
jpa用法,直接继承CrudRepository即可
不多说了,都懂
登录实现
登录过程其实只是处理异常的相关信息,具体的登录验证交给shiro来处理
退出实现
shiro内置退出方法,直接调用SecurityUtils.getSubject().logout();
ShiroConfig
Apache Shiro 核心通过 Filter 来实现,通过URL规则来进行过滤和权限校验,所以我们需要定义一系列关于URL的规则和访问权限。
Filter Chain定义说明:
- 一个URL可以配置多个Filter,使用逗号分隔
- 当设置多个过滤器时,全部验证通过,才视为通过
- 部分过滤器可指定参数,如perms,roles
Shiro内置的FilterChain
| Filter Name | Class |
|---|---|
| anon | org.apache.shiro.web.filter.authc.AnonymousFilter |
| authc | org.apache.shiro.web.filter.authc.FormAuthenticationFilter |
| authcBasic | org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter |
| perms | org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter |
| port | org.apache.shiro.web.filter.authz.PortFilter |
| rest | org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter |
| roles | org.apache.shiro.web.filter.authz.RolesAuthorizationFilter |
| ssl | org.apache.shiro.web.filter.authz.SslFilter |
| user | org.apache.shiro.web.filter.authc.UserFilter |
- anon:所有url都都可以匿名访问
- authc: 需要认证才能进行访问
- user:配置记住我或认证通过可以访问
MyShiroRealm
登录认证的实现
doGetAuthenticationInfo(AuthenticationToken token)
在Shiro中,最终是通过Realm来获取应用程序中的用户、角色及权限信息的。通常情况下,在Realm中会直接从我们的数据源中获取Shiro需要的验证信息。可以说,Realm是专用于安全框架的DAO. Shiro的认证过程最终会交由Realm执行,这时会调用Realm的getAuthenticationInfo(token)方法。
链接权限的实现
doGetAuthorizationInfo(PrincipalCollection principals)
如果只是简单的身份认证没有权限的控制的话,可直接返回null;
当访问页面时,链接配置了相应权限(@RequiresPermissions("userInfo:view"))或shiro标签()才执行此方法
1.添加依赖
<dependency><groupId>com.github.theborakompanioni</groupId><artifactId>thymeleaf-extras-shiro</artifactId><version>1.2.1</version></dependency>
2.在shiro的配置文件 ShiroConfig 中添加
@Beanpublic ShiroDialect shiroDialect() {return new ShiroDialect();}
3.在html中加入xmlns
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org"xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
4.例子
<span shiro:authenticated="true" ><span>欢迎您:<span th:text="${userInfo.realName}"></span></span></span>
至此,shiro的权限校验完成了,后期可以加入一些动态权限管理和缓存、“记住我”、“GIF验证码”
等功能。
参考: