温馨提示×

温馨提示×

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

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

安全框架Shiro怎么配置

发布时间:2022-01-19 16:28:16 来源:亿速云 阅读:223 作者:iii 栏目:开发技术

这篇文章主要介绍“安全框架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

核心类:

  • com.jspxcms.core.security.CmsAuthenticationFilter 登录逻辑处理类。包括加入验证码判断、记录登录日志的逻辑。

  • com.jspxcms.core.security.ShiroDbRealm 登录时查询用户名、密码及获取用户权限信息。

过滤器映射配置

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

大致描述如下:

  • /my/** /cmscp/** 路径需要登录后才能访问,如未登录则会重定向至登录页面。前者为是前台会员中心路径,后者为后台管理路径。

  • /login /cmscp/login.do 是登录请求。前者为前台登录请求,后者为后台登录请求。

  • /logout /cmscp/logout.do 是退出登录请求。

  • /** 其他路径可以随便访问。

密码加密

将用户密码直接使用明文保存在数据库中是极其不安全的。要对密码进行加密后,再保存到数据库,通常的加密方式有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怎么配置”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

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

AI