shiro是一个很不错的安全框架,相对Spring security 来说要简单易用的多,使用shiro来做web的权限子系统是不错的选择。
下面记录一下shiro和Spring整合的过程:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:util="http://www.springframework.org/schema/util"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
- <description>Shiro 配置</description>
- <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="/index.jsp" />
- <property name="unauthorizedUrl" value="/login.do" />
- <property name="filterChainDefinitions">
- <value>
- /login.jsp = anon
- /login.do = anon
- /** = authc
- </value>
- </property>
- </bean>
- <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
- <!--设置自定义realm-->
- <property name="realm" ref="monitorRealm" />
- </bean>
- <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
- <!--自定义Realm 继承自AuthorizingRealm-->
- <bean id="monitorRealm" class="***module.system.security.MonitorRealm"></bean>
- <!-- securityManager -->
- <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
- <property name="staticMethod"
- value="org.apache.shiro.SecurityUtils.setSecurityManager" />
- <property name="arguments" ref="securityManager" />
- </bean>
- <!-- Enable Shiro Annotations for Spring-configured beans. Only run after -->
- <!-- the lifecycleBeanProcessor has run: -->
- <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>
- <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
- <property name="securityManager" ref="securityManager"/>
- </bean>
- </beans>
将shiro的配置文件引入到web.xml中:
并在web.xml中加入如下代码:
- <!-- Shiro Security filter -->
- <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>
- <filter-mapping>
- <filter-name>shiroFilter</filter-name>
- <url-pattern>*.do</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>shiroFilter</filter-name>
- <url-pattern>*.jsp</url-pattern>
- </filter-mapping>
实现自己的Realm
- @Service("monitorRealm")
- public class MonitorRealm extends AuthorizingRealm {
- @Autowired UserService userService;
- @Autowired RoleService roleService;
- @Autowired LoginLogService loginLogService;
- public MonitorRealm(){
- super();
- }
- @Override
- protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
- /*这里编写授权代码*/
- }
- @Override
- protected AuthenticationInfo doGetAuthenticationInfo(
- AuthenticationToken authcToken) throws AuthenticationException {
- /*这里编写认证代码*/
- }
- public void clearCachedAuthorizationInfo(String principal) {
- SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName());
- clearCachedAuthorizationInfo(principals);
- }
- }
登录时的代码示例:
- Subject currentUser = SecurityUtils.getSubject();
- if(!currentUser.isAuthenticated()){
- UsernamePasswordToken token;
- if(null == rememberMe)
- token = new UsernamePasswordToken(user.getUsername(), EncryptUtils.encodeMD5String(user.getPassword()),false,request.getRemoteAddr());
- else token = new UsernamePasswordToken(user.getUsername(), EncryptUtils.encodeMD5String(user.getPassword()), true, request.getRemoteAddr());
- try {
- currentUser.login(token);
- } catch ( AuthenticationException ae ) {
- request.setAttribute("message", "用户名或密码错误!");
- return "login";
- }
- }
执行currentUser.login(token);这句代码时,shiro会自动调用用户实现的Realm的doGetAuthenticationInfo进行身份认证。
登出时的代码示例:
- Subject currentUser = SecurityUtils.getSubject();
- if (currentUser != null) {
- currentUser.logout();
- }
- HttpSession session = request.getSession(false);
- if( session != null ) {
- session.invalidate();
- }
- 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
相关推荐
1、本教程适用所有开发人员简单易懂,结合文章教程与demo示例。 2、技术选型(全部目前最新版本) springboot、shiro、mybatis、mybatis plus、mysql、thymeleaf、 3、实现功能:登陆认证、密码加密、权限授权等 4、...
Apache Shiro是一个功能强大且易于使用的Java安全框架,可执行身份验证、授权、加密和会话管理。目前,使用 Apache Shiro 的人越来越多,因为它相当简单,对比 Spring Security,可能没有 Spring Security 做的功能...
积分最低,Spring MVC+Mybatis+Ehcache+Apache Shiro+Bootstrap整合开发java仓库管理系统源码 开发环境:Eclipse ,JDK 1.8 ,Tomcat7 技术选型 后端技术 SpringMVC MVC框架 Spring Framework 容器 Apache ...
【Shiro】Apache Shiro架构之实际运用(整合到Spring中
安全框架:Apache Shiro 1.3.2 视图框架:Spring MVC 4.3.5 任务调度:Spring + Quartz 2.2.3 持久层框架:MyBatis 3.4.2 + Mybatis-plus 2.0.1 数据库连接池:Alibaba Druid 1.0 缓存框架:Ehcache 2.6 + Redis ...
Apache Shiro是一个强大而灵活的开源安全框架,它能够干净利落地处理身份认证,授权,企业会话管理和加密。现在,使用Apache Shiro的人越来越多,因为它相当简单,相比比Spring Security,Shiro可能没有Spring ...
Apache Shiro 安全框架 Mybatis ORM框架 MyBatis Generator 代码生成 C3P0 数据库连接池 Ehcache 进程内缓存框架 Apache poi 文件导入导出 Maven 项目构建管理 前端技术 jQuery , Bootstrap
JeeSite是在Spring Framework基础上搭建的一个Java基础开发平台,以Spring MVC为模型视图控制器,MyBatis为数据访问层, Apache Shiro为权限授权层,Ehcahe对常用数据进行缓存,Activit为工作流引擎。是JavaEE界的...
1. **spring-context.xml**:这是Spring框架的核心配置文件,定义了应用程序的Bean以及它们之间的依赖关系。在权限控制方面,可以配置Shiro的安全管理器(SecurityManager)和授权信息(如角色和权限)。
SpringMVC整合Shiro,Shiro是一个强大易用的Java安全框架,提供了认证、授权、加密和会话管理等功能。 配置applicationContext-shiro.xml 1. 配置authorizingRealm ...
Apache Shiro 是一个功能强大、灵活的,开源的安全框架。它可以干净利落地处理身份验证、授权、企业会话管理和加密。 Apache Shiro 的首要目标是易于使用和理解。安全通常很复杂,甚至让人感到很痛苦,但是 Shiro 却...
crm_ssm项目,集成了SSM框架,希望想学SSM项目整合的你会喜欢。
本项目基于Spring,整合Apache Shiro框架,实现用户管理和权限控制,主要内容如下: 1.登录(带验证码),包括“记住我”的功能; 2.加密,存储的密码不采用明文,初始密码123; 3.session管理:使用shiro默认的...
apache shiro 是一个安全认证框架,和 spring security 相比,在于他使用了比较简洁易懂的认证和授权方式。其提供的 native-session(即把用户认证后的授权信息保存在其自身提供Session 中)机制,这样就可以和 ...
Aurora是一个简单高效的后台管理系统。...安全框架采用时下流行的Apache Shiro,可实现对按钮级别的权限控制;前端页面使用KidAdmin构建,并提供多套配色以供选择。 项目地址:https://gitee.com/hgpt/Aurora
整合Apache Oltu 与 Shiro. 提供一个轻量的OAUTH2应用框架实现.并根据不同的应用场景提供不同的实现(如web场景,移动设备).该项目与spring-oauth-server实现相同的需求与场合. 只是在实现上使用的技术不同(spring-...
Apache Shiro是一个强大且易用的Java安全框架,执行身份验证、授权、密码和会话管理。使用Shiro的易于理解的API,您可以快速、轻松地获得任何应用程序,从最小的移动应用程序到最大的网络和企业应用程序。
为了给MIS系统添加一套较为通用的权限控制功能,本项目基于Spring,整合Apache Shiro框架,实现用户管理和权限控制,主要内容如下: 1.登录(带验证码),包括“记住我”的功能; 2.加密,存储的密码不采用明文,初始...