玩转SpringBoot之整合 shiro 权限框架( 二 )

并且 Shiro 还有通过增加其他的功能来支持和加强这些不同应用环境下安全领域的关注点 。
特别是对以下的功能支持:

  • Web支持:Shiro 提供的 Web 支持 api,可以很轻松的保护 Web 应用程序的安全 。
  • 缓存:缓存是 Apache Shiro 保证安全操作快速、高效的重要手段 。
  • 并发:Apache Shiro 支持多线程应用程序的并发特性 。
  • 测试:支持单元测试和集成测试,确保代码和预想的一样安全 。
  • "Run As":这个功能允许用户假设另一个用户的身份(在许可的前提下) 。
  • "Remember Me":跨 session 记录用户的身份,只有在强制需要时才需要登录 。
注意: Shiro 不会去维护用户、维护权限,这些需要我们自己去设计/提供,然后通过相应的接口注入给 Shiro
使用案例 Demo1.新建 maven 项目为方便我们初始化项目,Spring Boot给我们提供一个项目模板生成网站 。
  • 1、打开浏览器,访问:start.spring.io/
  • 2、根据页面提示,选择构建工具,开发语言,项目信息等 。
2.导入 springboot 父依赖<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.2.RELEASE</version></parent>复制代码3.相关 jar 包web 包
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>复制代码shiro-spring 包就是此篇文章的核心
<dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-spring</artifactId><version>1.4.0</version></dependency>shiro 注解会用到 aop<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency>数据库相关包使用的是mybatisplus<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.12</version></dependency><dependency><groupId>MySQL</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.1.0</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId><version>3.1.0</version></dependency><dependency><groupId>org.apache.velocity</groupId><artifactId>velocity-engine-core</artifactId><version>2.0</version></dependency>复制代码4.数据库建表语句在项目中有,项目地址: github.com/mmzsblog/mm…
5.自定义 realmpublic class MyShiroRealm extends AuthorizingRealm {@Autowiredprivate UserService userService;@Autowiredprivate RoleService roleService;@Autowiredprivate PermissionService permissionService;@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();// HttpServletRequest request = (HttpServletRequest) ((WebSubject) SecurityUtils// .getSubject()).getServletRequest();//这个可以用来获取在登录的时候提交的其他额外的参数信息String username = (String) principals.getPrimaryPrincipal();// 受理权限// 角色Set<String> roles = new HashSet<String>();Role role = roleService.getRoleByUserName(username);System.out.println(role.getRoleName());roles.add(role.getRoleName());authorizationInfo.setRoles(roles);// 权限Set<String> permissions = new HashSet<String>();List<Permission> querypermissions = permissionService.getPermissionsByRoleId(role.getId());for (Permission permission : querypermissions) {permissions.add(permission.getPermissionName());}authorizationInfo.setStringPermissions(permissions);return authorizationInfo;}@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)throws AuthenticationException {String loginName = (String) authcToken.getPrincipal();// 获取用户密码User user = userService.getOne(new QueryWrapper<User>().eq("username", loginName));if (user == null) {// 没找到帐号throw new UnknownAccountException();}String password = new String((char[]) authcToken.getCredentials());String inpass = (new Md5Hash(password, user.getUsername())).toString();if (!user.getPassword().equals(inpass)) {throw new IncorrectCredentialsException();}// 交给AuthenticatingRealm使用CredentialsMatcher进行密码匹配SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(loginName, user.getPassword(),ByteSource.Util.bytes(loginName), getName());return authenticationInfo;}}复制代码


推荐阅读