安全框架Shiro怎么配置

发布时间:2022-01-19 16:28:16 作者:iii
来源:亿速云 阅读:219

这篇文章主要介绍“安全框架Shiro怎么配置”,在日常操作中,相信很多人在安全框架Shiro怎么配置问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”安全框架Shiro怎么配置”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

Shiro是一个很简洁的安全框架,类似的Spring Security则要复杂许多。虽然spring-boot集成的是Spring Security,但我们还是选择了Shiro。

Shiro官网:http://shiro.apache.org/

原理简述

安全框架需要处理的事情:

Shiro使用Servlet Filter过滤器保护受访的页面,通过下面介绍的shiroFilterChainDefinitionMap配置需要保护的页面路径。

使用AuthorizingRealm获取用户密码及权限信息,即下面介绍的com.jspxcms.core.security.ShiroDbRealm。

在JSP页面中使用标签<shiro:hasPermission name="my:perm:code">判断是否有访问Controller中@RequiresPermissions("my:perm:code")标识的方法。

配置及源代码

配置类com.jspxcms.core.ShiroConfig(7.0及之前版本/src/main/resources/conf/context-shiro.xml)

权限相关的类包:com.jspxcms.core.security

加密相关的公用类包:com.jspxcms.common.security

核心类:

过滤器映射配置

ShiroConfig会读取过滤器映射配置。

@Bean("shiroFilter")
@DependsOn("propertiesHelper")
public ShiroFilterFactoryBean shiroFilterFactoryBean(BeanFactory beanFactory) throws IOException {
  ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
  ...
  Map<String, String> filterChainDefinitionMap = propertiesHelper()
      .getSortedMap("shiroFilterChainDefinitionMap.");
  factoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
  ...
}

过滤器映射配置:/src/main/resources/conf/conf.properties

shiroFilterChainDefinitionMap[100]/login=authc
shiroFilterChainDefinitionMap[200]/logout=logout
shiroFilterChainDefinitionMap[300]/cmscp=backSite,anon
shiroFilterChainDefinitionMap[400]/cmscp/=backSite,anon
shiroFilterChainDefinitionMap[500]/cmscp/index.do=backSite,anon
shiroFilterChainDefinitionMap[600]/cmscp/login.do=backSite,authc
shiroFilterChainDefinitionMap[700]/cmscp/logout.do=backSite,logout
shiroFilterChainDefinitionMap[800]/cmscp/**=backSite,user
shiroFilterChainDefinitionMap[900]/my/**=user
shiroFilterChainDefinitionMap[1000]/**=anon

大致描述如下:

密码加密

将用户密码直接使用明文保存在数据库中是极其不安全的。要对密码进行加密后,再保存到数据库,通常的加密方式有md5 sha1 sha256等,md5使用的最为广泛,但由于安全性较差,已经不建议使用。系统中使用sha1作为加密方式。

ShiroConfig中的配置如下:

@Bean("credentialsDigest")
public SHA1CredentialsDigest credentialsDigest() {
  return new SHA1CredentialsDigest();
}

这个加密对象会在com.jspxcms.core.security.ShiroDbRealm中注入:

@Autowired
public void setCredentialsDigest(CredentialsDigest credentialsDigest) {
    this.credentialsDigest = credentialsDigest;
}

到此,关于“安全框架Shiro怎么配置”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. spring+shiro搭建简单安全框架
  2. SpringBoot 2.0 开发案例之整合Shiro安全框架

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

shiro

上一篇:微信小程序中wx.chooseimage如何设置为同步

下一篇:Service类怎么用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》