前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Apache Shiro知识点总览

Apache Shiro知识点总览

作者头像
编程随想曲
发布2022-04-21 13:24:39
1700
发布2022-04-21 13:24:39
举报
文章被收录于专栏:编程随想曲
  1. 名词解释
  2. 权限认证
  3. 授权
  4. ini文件配置
  5. jsp标签授权
  6. Shiro会话机制
  7. 自定义Realm
  8. 加密、解密
  9. 特性
  10. 与spring整合

名词解释

Subject:认证主体

Reaml:认证来源[jdbc、property、text、jndi]

权限认证

谁访问什么资源

权限:页面

角色:权限的集合

用户:subject

授权

为角色分配权限

例如:admin = user : *

ini文件配置

代码语言:javascript
复制
[main]

authc.loginUrl = /login
roles.unauthorizedUrl = /unauthorized

perms.unauthorizedUrl = /unauthorized.jsp
[users]
jack = 123,admin
[roles]
admin = user : *
[urls]
/login = anon
/admin = authc
/student = roles[teacher]
/teacher = perms["user:create"]

Url匹配规则

代码语言:javascript
复制
/admin        匹配/admin
/admin?      匹配/admin1
/admin*       匹配/admin123
/admin/**    匹配/admin/1/2/3

jsp标签授权

依赖

代码语言:javascript
复制
shiro-web.jar
代码语言:javascript
复制
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>

例如:

代码语言:javascript
复制
<shiro:guest>     Hi there!  Please 
    <a href="login.jsp">Login</a> or 
    <a href="signup.jsp">Signup</a> today!
</shiro:guest>

Shiro会话机制

代码语言:javascript
复制
Subject currentUser = SecurityUtils.getSubject();
 Session session = currentUser.getSession(); 
session.setAttribute( "someKey", someValue);
getSession calls work in any application, even non-web applications.

自定义Realm

Most people choose to subclass the AuthorizingRealm abstract class instead of starting from scratch. This class implements common authentication and authorization workflow to save you time and effort.

加密、解密

例如:

new Md5Hash(data)

特性

  • Web Support: Shiro’s web support APIs help easily secure web applications.
  • Caching: Caching is a first-tier citizen in Apache Shiro’s API to ensure that security operations remain fast and efficient.
  • Concurrency: Apache Shiro supports multi-threaded applications with its concurrency features.
  • Testing: Test support exists to help you write unit and integration tests and ensure your code will be secured as expected.
  • “Run As”: A feature that allows users to assume the identity of another user (if they are allowed), sometimes useful in administrative scenarios.
  • “Remember Me”: Remember users’ identities across sessions so they only need to log in when mandatory.

与spring整合

web.xml

代码语言:javascript
复制
<!-- The filter-name matches name of a 'shiroFilter' bean inside applicationContext.xml -->
<filter>     
    <filter-name>shiroFilter</filter-name>     
    <filter-class>
            org.springframework.web.filter.DelegatingFilterProxy
    </filter-class>     
    <init-param>         
        <param-name>targetFilterLifecycle</param-name>         
        <param-value>true</param-value>     
    </init-param>
</filter>...
<!-- Make sure any request you want accessible to Shiro is filtered. /* catches all --><!-- requests.  Usually this filter mapping is defined first (before all others) to --><!-- ensure that Shiro works in subsequent filters in the filter chain:             -->
<filter-mapping>     
    <filter-name>shiroFilter</filter-name>     
    <url-pattern>/*</url-pattern>
</filter-mapping>

applicationContext.xml

代码语言:javascript
复制
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">     
    <property name="securityManager" ref="securityManager"/>     
    <property name="loginUrl" value="/login.jsp"/>     
    <property name="successUrl" value="/home.jsp"/>     
    <property name="unauthorizedUrl" value="/unauthorized.jsp"/>      
    <property name="filterChainDefinitions">         
        <value>             
            # some example chain definitions:             
            /admin/** = authc, roles[admin]             
            /docs/** = authc, perms[document:read]             
            /** = authc             
            # more URL-to-FilterChain definitions here        
        </value>     
    </property>

</bean>
<bean id="someFilter" class="..."/>
    <bean id="anotherFilter" class="..."> ... 
</bean>...
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">          
    <property name="realm" ref="myRealm"/>     
</bean>
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
    <bean id="myRealm" class="...">     ...
</bean>
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-04-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 编程随想曲 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • web.xml
  • applicationContext.xml
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档