SpringBoot整合Shiro+MD5+Salt+Redis实现认证和动态权限管理(上)----筑基中期

通过前几篇文章的学习,我们从大体上了解了shiro关于认证和授权方面的应用。在接下来的文章当中,我将通过一个demo,带领大家搭建一个SpringBoot整合Shiro的一个项目开发脚手架,将之前学过的知识点串到一起,其中,也会补充一些之前没有讲过的内容。通过这个demo结束这几天的学习,同时也是结束国庆中秋小长假shiro系列专题入门文章。

SpringBoot整合Shiro思路分析 鉴权流程分析

我们将我们的SpringBoot应用整合shiro,主要目的就是让shiro帮我们处理认证和授权的相关内容。也就是说,我们需要让shiro接管我们SpringBoot应用的会话。让用户的每一次请求都经过shiro进行认证和授权。因此,我们需要将用户请求拦截下来转发给shiro处理,这个拦截器是shiro提供的,ShiroFilter。

步骤

用户通过客户端(浏览器、手机App、小程序)发起请求

ShiroFilter拦截请求并判断请求访问的资源是否为受保护资源:

2.1 是,则执行步骤3

2.2 不是,则直接放行

判断用户是否已通过认证:

3.1 是 ,则执行步骤4

3.2 否,将用户请求重定向到认证页面,让用户先认证

获取用户权限信息和访问资源所需要的权限信息进行比对:

4.1 用户具备访问权限,则放行

4.2 用户不具备权限,返回403的相应提示

数据库分析设计

我们通过MySQL保存我们的认证和权限的相关数据。采用用户-角色-权限模型实现动态管理用户权限信息。

我们将系统当中的菜单、按钮、后端接口都抽象成系统的资源数据。以下是数据库表的设计:

SpringBoot整合Shiro+MD5+Salt+Redis实现认证和动态权限管理(上)----筑基中期

文末提供sql脚本的下载。

整合步骤 环境搭建 maven

创建一个SpringBoot的web应用,并引入如下依赖

<dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring-boot-web-starter</artifactId> <version>1.6.0</version> </dependency> 添加对用户、角色和资源的CRUD支持

这里代码就省略了,不影响理解,完整代码可以从文末提供的方式中下载。

配置Shiro 自定义Realm /**自定义Realm,使用mysql数据源 * @author 赖柄沣 bingfengdev@aliyun.com * @version 1.0 * @date 2020/10/6 9:09 */ public class MySQLRealm extends AuthorizingRealm { @Autowired private IUserService userService; @Autowired private IRoleService roleService; @Autowired private IResourceService resourceService; /** * 授权 * @param principals * @return */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { String username = (String) principals.getPrimaryPrincipal(); List<Role> roleList = roleService.findByUsername(username); SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); for (Role role : roleList) { authorizationInfo.addRole(role.getRoleName()); } List<Long> roleIdList = new ArrayList<>(); for (Role role : roleList) { roleIdList.add(role.getRoleId()); } List<Resource> resourceList = resourceService.findByRoleIds(roleIdList); for (Resource resource : resourceList) { authorizationInfo.addStringPermission(resource.getResourcePermissionTag()); } return authorizationInfo; } /** * 认证 * @param token * @return * @throws AuthenticationException */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { if(token==null){ return null; } String principal = (String) token.getPrincipal(); User user = userService.findByUsername(principal); SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), ByteSource.Util.bytes(user.getSalt()), getName()); return simpleAuthenticationInfo; } }

shiro中的Realm对象充当了认证、授权数信息的据源作用。关于更多自定义Realm的内容请参考我的另一篇文章《Shiro入门学习---使用自定义Realm完成认证|练气中期》 。

ShiroConfig /**shiro配置类 * @author 赖柄沣 bingfengdev@aliyun.com * @version 1.0 * @date 2020/10/6 9:11 */ @Configuration public class ShiroConfig { /** * 创建ShiroFilter拦截器 * @return ShiroFilterFactoryBean */ @Bean(name = "shiroFilterFactoryBean") public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager securityManager){ ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(securityManager); //配置不拦截路径和拦截路径,顺序不能反 HashMap<String, String> map = new HashMap<>(5); map.put("/authc/**","anon"); map.put("/login.html","anon"); map.put("/js/**","anon"); map.put("/css/**","anon"); map.put("/**","authc"); shiroFilterFactoryBean.setFilterChainDefinitionMap(map); //覆盖默认的登录url shiroFilterFactoryBean.setLoginUrl("/authc/unauthc"); return shiroFilterFactoryBean; } @Bean public Realm getRealm(){ //设置凭证匹配器,修改为hash凭证匹配器 HashedCredentialsMatcher myCredentialsMatcher = new HashedCredentialsMatcher(); //设置算法 myCredentialsMatcher.setHashAlgorithmName("md5"); //散列次数 myCredentialsMatcher.setHashIterations(512); MySQLRealm realm = new MySQLRealm(); realm.setCredentialsMatcher(myCredentialsMatcher); return realm; } /** * 创建shiro web应用下的安全管理器 * @return DefaultWebSecurityManager */ @Bean public DefaultWebSecurityManager getSecurityManager(Realm realm){ DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(realm); SecurityUtils.setSecurityManager(securityManager); return securityManager; } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpgjgj.html