当前位置: 移动技术网 > IT编程>开发语言>Java > Shiro简单入门+个人理解

Shiro简单入门+个人理解

2019年02月16日  | 移动技术网IT编程  | 我要评论

身为一个刚刚进入开发行业的学生,进入公司就开始了shiro框架的应用,特此在这里写下收获。

shiro是apache旗下一个开源安全框架,它将软件系统的安全认证相关的功能抽取出来,实现用户身份认证,权限授权、加密、会话管理等功能,组成了一个通用的安全认证框架,使用shiro就可以非常快速的完成认证、授权等功能的开发,降低系统成本。

在概念层,shiro 架构包含三个主要的理念:subject,securitymanager和 realm。

详细架构就不细说了,只从概要架构进行理解。

通过shiro框架进行权限管理时,要涉及到的一些核心对象,主要包括:

认证管理对象,授权管理对象,会话管理对象,缓存管理对象,加密管理对象

以及realm管理对象(领域对象:负责处理认证和授权领域的数据访问题)

 

1)  subject(主体):与软件交互的一个特定的实体(用户、第三方服务等)。

2)  securitymanager(安全管理器) :shiro 的核心,用来协调管理组件工作。

3)  authenticator(认证管理器):负责执行认证操作

4)  authorizer(授权管理器):负责授权检测

5)  sessionmanager(会话管理):负责创建并管理用户 session 生命周期,提供一个强有力的 session 体验。

6)  sessiondao:代表 sessionmanager 执行 session 持久(crud)动作,它允许任何存储的数据挂接到 session 管理基础上。

7)  cachemanager(缓存管理器):提供创建缓存实例和管理缓存生命周期的功能

8)  cryptography(加密管理器):提供了加密方式的设计及管理。

9)realms(领域对象):是shiro和你的应用程序安全数据之间的桥梁。

以下废话不多说,直接进入应用环节

 

因为我是maven所有选择了添加了依赖

  <dependency>

   <groupid>org.apache.shiro</groupid>

   <artifactid>shiro-spring</artifactid>

   <version>1.3.2</version>

  </dependency>

 

创建spring-shiro.xml(tomcat启动时需要加载)

<?xml version="1.0" encoding="utf-8"?>
<beans default-lazy-init="true"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemalocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd" >

<!-- 配置securitymanager对象(shiro框架核心,负责调用相关组件实现
用户身份认证,授权,缓存,会话管理等功能)-->
<bean id="securitymanager"
class="org.apache.shiro.web.mgt.defaultwebsecuritymanager">
<property name="realm" ref="shirouserrealm"/>
</bean>
<!-- 配置shirofilterfactorybean对象(shiro中会通过很多过滤器对web请求
做预处理,这些过滤器的创建底层设计了一个工厂类) -->
<bean id="shirofilterfactory" class="org.apache.shiro.spring.web.shirofilterfactorybean">
<!-- 注入securitymanager对象 -->
<property name="securitymanager" ref="securitymanager"/>
<!-- 配置登录页面 -->
<property name="loginurl" value="/login.jsp"/>
<!-- 定义过滤规则(哪些资源允许匿名访问,哪些资源必须授权访问)-->
<property name="filterchaindefinitionmap">
<map>
<!-- 说明:anon表示允许匿名访问, authc表示授权访问-->

<entry key="/css/**" value="anon"/>
<entry key="/db_sql/**" value="anon"/>
<entry key="/images/**" value="anon"/>
<entry key="/js/**" value="anon"/>
<entry key="/meta-inf/**" value="anon"/>
<entry key="/yzm/**" value="anon"/>

<!-- 这里是授权的应用,以xml方式来管理,当然也有使用注解或jsp标签的方式,在授权时我们会讲解-->
<!-- <entry key="/company/deletecompany.do" value="perms[company:delete]" /> -->
<!-- <entry key="/page/xtgl/userlist.jsp" value="perms[xtgl:userlist]"> -->
<entry key="/login/getlogins.do" value="anon"/>
<entry key="/dologout.do" value="logout"/>
<entry key="/**" value="authc"/>


</map>
</property>
</bean>
<!-- 配置bean对象的生命周期管理 -->
<bean id="lifecyclebeanpostprocessor"
class="org.apache.shiro.spring.lifecyclebeanpostprocessor">
</bean>
<!-- 配置bean对象的代理 -->
<bean class="org.springframework.aop.framework.autoproxy.defaultadvisorautoproxycreator"
depends-on="lifecyclebeanpostprocessor">
</bean>
<!-- 配置授权bean对象 -->
<bean class="org.apache.shiro.spring.security.interceptor.authorizationattributesourceadvisor">
<property name="securitymanager" ref="securitymanager"/>

<!--自定义realm对象-->

</bean>
<bean id="shirouserrealm" class="com.cn.ericsson.service.realm.shirouserrealm">
</bean>
</beans>

 

 在web.xml进行的操作

<!-- 配置shiro过滤器(对请求进行拦截) -->
<filter>
<filter-name>shirofilter</filter-name>
<!-- 此类型由谁提供(spring 框架):spring整合shiro的入口 -->
<filter-class>org.springframework.web.filter.delegatingfilterproxy</filter-class>
<init-param>
<!-- 这个参数名在delegatingfilterproxy中定义 -->
<param-name>targetbeanname</param-name>
<!-- 这个值在spring-shiro.xml配置文件中定义 -->
<param-value>shirofilterfactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shirofilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

自定义realm

@service
public class shirouserrealm extends authorizingrealm {

@override
protected authorizationinfo dogetauthorizationinfo(principalcollection arg0) {
// todo auto-generated method stub
return null;
}

@override
protected authenticationinfo dogetauthenticationinfo(
authenticationtoken arg0) throws authenticationexception {
// todo auto-generated method stub
return null;
}
}

 

到此环境已经搭建完成,后期说明认证及授权

 

 

 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网