`
wusuoya
  • 浏览: 637761 次
  • 性别: Icon_minigender_2
  • 来自: 成都
社区版块
存档分类
最新评论

Spring3与安全框架apache shiro的整合

    博客分类:
  • web
 
阅读更多

shiro是一个很不错的安全框架,相对Spring security 来说要简单易用的多,使用shiro来做web的权限子系统是不错的选择。

下面记录一下shiro和Spring整合的过程:

 

Applicationcontext-shiro.xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:util="http://www.springframework.org/schema/util"  
  5.        xsi:schemaLocation="  
  6.        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">  
  8.   
  9.     <description>Shiro 配置</description>  
  10.     <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  
  11.         <property name="securityManager" ref="securityManager" />  
  12.         <property name="loginUrl" value="/login.jsp" />  
  13.         <property name="successUrl" value="/index.jsp" />  
  14.         <property name="unauthorizedUrl" value="/login.do" />  
  15.         <property name="filterChainDefinitions">  
  16.             <value>  
  17.                 /login.jsp = anon  
  18.                 /login.do = anon  
  19.                 /** = authc  
  20.                </value>  
  21.         </property>  
  22.     </bean>  
  23.   
  24.     <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
  25.         <!--设置自定义realm-->  
  26.         <property name="realm" ref="monitorRealm" />  
  27.     </bean>  
  28.   
  29.     <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />  
  30.       
  31.     <!--自定义Realm 继承自AuthorizingRealm-->  
  32.     <bean id="monitorRealm" class="***module.system.security.MonitorRealm"></bean>  
  33.     <!-- securityManager -->  
  34.     <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">  
  35.         <property name="staticMethod"  
  36.             value="org.apache.shiro.SecurityUtils.setSecurityManager" />  
  37.         <property name="arguments" ref="securityManager" />  
  38.     </bean>  
  39.       
  40.     <!-- Enable Shiro Annotations for Spring-configured beans.  Only run after -->  
  41.     <!-- the lifecycleBeanProcessor has run: -->  
  42.     <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>  
  43.     <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">  
  44.     <property name="securityManager" ref="securityManager"/>  
  45.       
  46. </bean>  
  47. </beans>  

 

 

 

 

 

将shiro的配置文件引入到web.xml中:

 

 

 

 

 

 

 

并在web.xml中加入如下代码:

 

 

 

 

 

 

 

 

 

Xml代码  收藏代码
  1. <!-- Shiro Security filter -->  
  2.     <filter>  
  3.         <filter-name>shiroFilter</filter-name>  
  4.         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
  5.         <init-param>  
  6.             <param-name>targetFilterLifecycle</param-name>  
  7.             <param-value>true</param-value>  
  8.         </init-param>  
  9.     </filter>  
  10.   
  11.     <filter-mapping>  
  12.         <filter-name>shiroFilter</filter-name>  
  13.         <url-pattern>*.do</url-pattern>  
  14.     </filter-mapping>  
  15.     <filter-mapping>  
  16.         <filter-name>shiroFilter</filter-name>  
  17.         <url-pattern>*.jsp</url-pattern>  
  18.     </filter-mapping>  

 

 

 

实现自己的Realm

 

 

 

Java代码  收藏代码
  1. @Service("monitorRealm")  
  2. public class MonitorRealm extends AuthorizingRealm {  
  3.       
  4.     @Autowired UserService userService;  
  5.     @Autowired RoleService roleService;  
  6.     @Autowired LoginLogService loginLogService;  
  7.       
  8.     public MonitorRealm(){  
  9.         super();  
  10.   
  11.     }  
  12.       
  13.     @Override  
  14.     protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {  
  15.         /*这里编写授权代码*/  
  16.           
  17.     }  
  18.   
  19.     @Override  
  20.     protected AuthenticationInfo doGetAuthenticationInfo(  
  21.             AuthenticationToken authcToken) throws AuthenticationException {  
  22.     /*这里编写认证代码*/  
  23.     }  
  24.       
  25.     public void clearCachedAuthorizationInfo(String principal) {  
  26.         SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName());  
  27.         clearCachedAuthorizationInfo(principals);  
  28.     }  
  29.   
  30. }  

 

 

 

登录时的代码示例:

 

 

 

Java代码  收藏代码
  1. Subject currentUser = SecurityUtils.getSubject();  
  2.         if(!currentUser.isAuthenticated()){  
  3.             UsernamePasswordToken token;  
  4.             if(null == rememberMe)  
  5.                 token = new UsernamePasswordToken(user.getUsername(), EncryptUtils.encodeMD5String(user.getPassword()),false,request.getRemoteAddr());  
  6.             else token = new UsernamePasswordToken(user.getUsername(), EncryptUtils.encodeMD5String(user.getPassword()), true, request.getRemoteAddr());  
  7.             try {  
  8.                 currentUser.login(token);  
  9.             } catch ( AuthenticationException ae ) {  
  10.                 request.setAttribute("message""用户名或密码错误!");  
  11.                 return "login";  
  12.             }  
  13.         }  

 

 执行currentUser.login(token);这句代码时,shiro会自动调用用户实现的Realm的doGetAuthenticationInfo进行身份认证。

 

登出时的代码示例:

 

 

 

Java代码  收藏代码
  1. Subject currentUser = SecurityUtils.getSubject();  
  2.         if (currentUser != null) {  
  3.             currentUser.logout();  
  4.         }  
  5.         HttpSession session = request.getSession(false);  
  6.         if( session != null ) {  
  7.             session.invalidate();  
  8.         }  
  9.         return "login";  

 

 在对用户(角色)进行授权时会执行Realm里的doGetAuthorizationInfo方法。

 

 

 

OK简单的集成完成了,如果用cas或者Springsecurity恐怕没这么简单利索 哈哈。

 

 

 



 

类似功能链接:http://kdboy.iteye.com/blog/1103794

其它链接:

http://kdboy.iteye.com/blog/1154644

http://kdboy.iteye.com/blog/1154652

http://kdboy.iteye.com/blog/1155450

http://kdboy.iteye.com/blog/1169631

http://kdboy.iteye.com/blog/1169637

官方名词解释:http://shiro.apache.org/terminology.html

官方权限解释:http://shiro.apache.org/permissions.html

 

分享到:
评论

相关推荐

    Apache Shiro 集成-spring

    3. Shiro与Spring MVC的整合:配置ShiroFilter,使其作为Spring MVC的拦截器运行。 4. 安全注解:利用@RequiresAuthentication、@RequiresRoles、@RequiresPermissions等注解进行权限控制。 5. 会话管理:可以配置...

    spring shiro整合

    在IT行业中,Spring Shiro整合是一项常见的安全框架搭建技术,主要应用于Java Web开发。Spring MVC作为主流的MVC框架,负责处理HTTP请求和业务逻辑,MyBatis则为持久层框架,负责数据库交互,而Apache Shiro则是一个...

    SpringBoot + Apache Shiro1.9.1 最新版本详细教程,基于RBAC角色访问、安全管理框架、用户角色权限

    1、本教程适用所有开发人员简单易懂,结合文章教程与demo示例。 2、技术选型(全部目前最新版本) springboot、shiro、mybatis、mybatis plus、mysql、thymeleaf、 3、实现功能:登陆认证、密码加密、权限授权等 4、...

    apache shiro整合struts2+spring+mybatis简单demo

    在这个“apache shiro整合struts2+spring+mybatis简单demo”中,我们将探讨如何将Shiro与三个流行的Java开发框架——Struts2、Spring和MyBatis进行集成,构建一个完整的安全管理体系。 首先,Struts2是一个基于MVC...

    Apache Shiro教程

    Apache Shiro是一个强大的Java安全框架,它为应用程序提供了身份验证、授权、会话管理和加密等功能。这个教程将帮助你深入理解和有效地使用Shiro框架。在本文中,我们将详细探讨Shiro的核心概念、主要功能和常见用法...

    shiro安全框架整合Mybatis

    Apache Shiro是一个功能强大且易于使用的Java安全框架,可执行身份验证、授权、加密和会话管理。目前,使用 Apache Shiro 的人越来越多,因为它相当简单,对比 Spring Security,可能没有 Spring Security 做的功能...

    Spring MVC+Mybatis+Ehcache+Apache Shiro+Bootstrap整合开发java仓库管理系统源码

    积分最低,Spring MVC+Mybatis+Ehcache+Apache Shiro+Bootstrap整合开发java仓库管理系统源码 开发环境:Eclipse ,JDK 1.8 ,Tomcat7 技术选型 后端技术 SpringMVC MVC框架 Spring Framework 容器 Apache ...

    Spring框架中整合Shiro

    本文将详细介绍如何在Spring框架中整合Shiro,实现身份识别(authc)和鉴权管理功能。 首先,我们需要引入Shiro的相关依赖。在Maven项目中,可以在pom.xml文件中添加以下依赖: ```xml &lt;groupId&gt;org.apache....

    spring shiro整合入门

    Spring框架作为Java领域最流行的框架之一,提供了丰富的功能,而Apache Shiro则是一款强大的安全管理框架,专注于身份验证、授权和会话管理。将Spring与Shiro进行整合,可以构建出高效且灵活的安全管理体系。本文将...

    搭建Springboot整合shiro安全框架+Redis+Swagger+Filter超详细

    1. **Spring Boot与Shiro整合**:Apache Shiro是一款轻量级的安全框架,提供了身份验证、授权、会话管理和加密等功能。在Spring Boot项目中集成Shiro,可以方便地控制应用程序的访问权限。首先,我们需要在`pom.xml`...

    shiro整合ssm框架

    本项目"shiro整合ssm框架"旨在将Shiro与SSM框架集成,提供一套完整的权限验证解决方案。 首先,我们要理解Shiro的核心组件: 1. **Subject**:Shiro的中心概念,代表当前用户的安全视角。 2. **Realm**:用于连接...

    Spring-SpringMVC-MyBatis-Shiro整合

    而Apache Shiro则是一个强大的安全框架,负责身份验证、授权和会话管理。接下来,我们将深入探讨这四个组件的整合以及它们各自的关键知识点。 **Spring框架**:Spring的核心是依赖注入(Dependency Injection,DI)...

    shiro整合spring项目实例

    在本项目实例中,我们将深入探讨如何将Shiro与Spring框架整合,以实现更高效、灵活的安全控制。 1. **Shiro基础** Apache Shiro 提供了用户认证、权限授权、加密和会话管理四大核心功能。认证是指验证用户身份,...

    spring整合其他框架

    6. Spring与Shiro整合:Apache Shiro是一个强大的安全框架,负责身份验证、授权、会话管理和加密。Spring通过AOP和Shiro的API集成,为应用提供全面的安全管理,包括登录认证、权限控制等。 7. Spring与Struts2整合...

    shiro最简单整合版本

    Apache Shiro 是一个强大且易用的 Java 安全框架,提供身份认证、授权、加密和会话管理功能,简化了处理安全性的工作。在本文中,我们将深入探讨 Apache Shiro 的核心概念及其最简单的整合方式。 一、Shiro 的核心...

    shiro整合spring+springmvcjar包

    3. **Spring MVC 与 Shiro 整合**: - Shiro的Web支持主要是通过Filter实现的,Spring MVC 应用需要在web.xml中配置Shiro Filter,并正确设定拦截路径。 - Shiro的`@ShiroSubject`注解可以用于控制器方法,实现...

    spring整合shiro

    Spring 和 Shiro 是两个在Java Web开发中常用的框架,Spring 是一个全面的后端开发框架,而 Apache Shiro 是一个安全认证框架。Spring 整合 Shiro 主要是为了实现更高效、灵活的安全管理,包括身份验证...

    Spring MVC整合shiro

    Spring MVC 是一款强大的MVC框架,用于构建企业级的Web应用程序,而Apache Shiro则是一款安全框架,负责处理身份验证、授权(权限控制)、会话管理和安全性相关的其他功能。将两者整合可以实现一个完整的基于角色的...

Global site tag (gtag.js) - Google Analytics