Spring3与安全框架apache shiro的整合

2023-11-16

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

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring3与安全框架apache shiro的整合 的相关文章

随机推荐

  • antd design pro 之「PageHeaderWrapper」

  • 微服务-API网关-权限控制

    权限控制介绍 权限控制是一个古老的话题 你可能会想有没有什么权限设计方案可以满足所有的应用场景呢 答案是没有 就像几乎所有问题一样 没有一种系统可以解决所有情况的 我们需要根据不同的场景和需求来设计不同的系统 权限控制主要设计用户 角色 组
  • CCIE面试题

    前言 这里是几个月前在网上转载很多的CCIE面试题 题虽然不难 但如果没有在电信或cisco代理商工作过 仅仅凭书面的知识还是回答不全的 下面是网上的参考答案加上我的一点点补充 以后有时间再补充 先贴出来供大家参考 也让从事相关技术的人自我
  • YOLOV5更换轻量级的backbone:mobilenetV2

    目录 简洁概要 修改主干网络 一 添加自己主干网络 二 在yolo py中添加common中的两个函数 三 制作mobilenetv2的yaml配置文件 四 制作数据集VOC的yaml配置文件 五 启用训练 六 性能检测 简洁概要 Mobi
  • Elasticsearch 8.8.0 发布

    Elasticsearch 是一个基于 Lucene 库的搜索引擎 它提供了一个分布式 支持多租户的全文搜索引擎 具有 HTTP Web 接口和无模式 JSON 文档 Elasticsearch 基于 Java 开发 并在 SSPL Ela
  • 使用mongo命令工具操作集合数据

    与 MongoDB 建立连接 mongo 如果设置了密码 使用这行命令 mongo port 27017 u admin p xxxxxx authenticationDatabase admin 以操作八月创建的历史数据为例 确认操作集合
  • docker 启动时错误docker: Cannot connect to the Docker daemon

    在学习docker的时候遇到一个错误docker Cannot connect to the Docker daemon at unix var run docker sock Is the docker daemon running 如下
  • make[1]: [persist-settings] Error 2 (ignored) CC adlist.o /bin: cc: command not found make[1]: *

    Linux系统安装Redis执行Make编译时报错 make 1 persist settings Error 2 ignored CC adlist o bin cc command not found make 1 adlist o E
  • 微信小程序 scroll-view的滚动条设置

    小程序的scroll view用的比较多了 列表页一般也没管它的滚动条 最近突然发现在android与ios中横向滑动的时候表现不一样 不一样在哪呢 ios上直接就不显示啊 也是没谁了 深入想了一下 这滚动条能不能换一颜色或者换个样式 有这
  • 基于AIOT技术的智慧校园空调集中管控系统设计与实现

    AIOT技术的智慧校园空调集中管控系统设计与实现本科毕业论文 I 引言 本文旨在探讨基于AIOT技术的智慧校园空调集中管控系统的设计和实现 首先 综述当前AIOT技术发展状况和智慧校园空调集中管控系统在当前应用领域中的重要性 其次 分析相关
  • 原理图符号(原理图库)创建流程及注意事项

    参考资料 电巢EMEA体验营二期 1 原理图符号创建流程 1 0 元器件属性 以一款压力传感器芯片LPS22HH为例 来讲解原理图符号的创建流程 LPS22HH的引脚描述如下所示 1 1 创建工程 1 2 创建原理图符号文件 创建完成原理图
  • Xilinx BUFGMUX使用注意事项

    Xilinx BUFGMUX使用注意事项 最近使用Xilinx FPGA的时候 需要用到一个外部时钟和一个PLL产生的时钟 可以通过外部SWICH进行时钟的切换 觉得这种方式可以通过原语例化完成 原语 果不其然 在原语示例中找到了类似的模块
  • java基础:浅谈泛型

    1 为什么要使用泛型 给一段代码 import java util ArrayList import java util List public class GenericList error public static void main
  • 解决“The method XXXXXX of type XXXXXXXXX must override a superclass method”

    我的Eclipse版本是3 6 1 Override 时出现以下错误 The method XXXXXX of type XXXXXXXXX must override a superclass method 上网搜索原来原因是 实现类里面
  • Docker 部署Streamlit项目

    文章目录 前言 关于streamlit Docker 部署Streamlit项目 Streamlit如何部署到云服务器 1 安装docker 2 拉取python镜像 2 1 什么是DockerHub 2 2 配置docker加速器 2 3
  • SpringMVC增删改查(CRUD)的实现

    目录 前言 一 前期准备 1 pom xml 依赖与插件的导入 2 jdbc properties 数据库连接 3 log4j2 xml 日志文件 4 spring mybatis mybatis与spring整合文件 5 spring c
  • 解决AttributeError: module 'tensorflow' has no attribute 'ConfigProto'

    使用CUDA10 1加上Tensorflow 2 0会出现AttributeError module tensorflow has no attribute ConfigProto 这个问题 这个是由于现在新版本中一些1 0版本的函数被和2
  • Android自动化测试中操作技巧合集(建议收藏)

    Android自动化测试中短信验证码的操作技巧 一 内容提供器机制简介 Android 系统采用了内容提供器 ContentProvider 机制来管理不同应用的数据访问 内容提供器为不同应用间的数据共享提供了接口 它们像是一个中央数据仓库
  • 快速中值求取算法

    中值 顾名思义 就是指一个从小到大的序列的中间的那一个数 一般的讲 中值比平均值还要更加稳定 如一个序列中的某一个值被误乘以了100 平均值则会有很大的波动 但是中位数则不会发生太大的变化 但是如果对数据先排序 然后再进行取中值 则比较耗时
  • Spring3与安全框架apache shiro的整合

    shiro是一个很不错的安全框架 相对Spring security 来说要简单易用的多 使用shiro来做web的权限子系统是不错的选择 下面记录一下shiro和Spring整合的过程 Applicationcontext shiro x