百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

SHRIO使用记录

bigegpt 2024-09-11 01:05 4 浏览

16年的项目里用过一次shiro,但是时间久远又没有做好笔记,忘得差不多了,这次又走了一遍,记录一下以便以后翻看,以下是流水。

1.加入依赖

<!--shiro-->
 <dependency>
 <groupId>org.apache.shiro</groupId>
 <artifactId>shiro-all</artifactId>
 <version>1.3.2</version>
 </dependency>
 <dependency>
 <groupId>net.sf.ehcache</groupId>
 <artifactId>ehcache-core</artifactId>
 <version>2.4.3</version>
 </dependency>
 <dependency>
 <groupId>log4j</groupId>
 <artifactId>log4j</artifactId>
 <version>1.2.15</version>
 </dependency>
 <dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-api</artifactId>
 <version>1.6.1</version>
 </dependency>
 <dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-log4j12</artifactId>
 <version>1.6.1</version>
 </dependency>
 <!--shiro-->

2.web.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>
 <filter-mapping>
 <filter-name>shiroFilter</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>

3.将shiro相关配置单独放在applicationContext-shiro.xml中并在spring配置文件中import

<?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:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
 <context:component-scan base-package="com.wqqd.findfocus" annotation-config="true"/>
 <!-- 1. 配置 SecurityManager-->
 <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
 <property name="cacheManager" ref="cacheManager"/>
 <property name="authenticator" ref="authenticator"></property>
 <property name="realms">
 <list>
 <ref bean="jdbcRealm"/>
 </list>
 </property>
 <!--<property name="rememberMeManager.cookie.maxAge" value="10"></property>-->
 </bean>
 <!--2. 配置 CacheManager(需要加入 ehcache 的 jar 包及配置文件).-->
 <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
 <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
 </bean>
 <bean id="authenticator" class="org.apache.shiro.authc.pam.ModularRealmAuthenticator">
 <property name="authenticationStrategy">
 <bean class="org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy"></bean><!--多realm时的认证策略-->
 </property>
 </bean>
 <!--3. 配置 Realm(直接配置实现了 org.apache.shiro.realm.Realm 接口的 bean)-->
 <bean id="jdbcRealm" class="com.wqqd.findfocus.shiro.ShiroRealm">
 <property name="credentialsMatcher">
 <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
 <property name="hashAlgorithmName" value="SHA1"></property>
 <property name="hashIterations" value="10"></property>
 </bean>
 </property>
 </bean>
 <!--<bean id="secondRealm" class="com.atguigu.shiro.realms.SecondRealm">-->
 <!--<property name="credentialsMatcher">-->
 <!--<bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">-->
 <!--<property name="hashAlgorithmName" value="SHA1"></property>-->
 <!--<property name="hashIterations" value="1024"></property>-->
 <!--</bean>-->
 <!--</property>-->
 <!--</bean>-->
 <!--
 4. 配置 LifecycleBeanPostProcessor. 可以自定的来调用配置在 Spring IOC 容器中 shiro bean 的生命周期方法.
 -->
 <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
 <!--
 5. 启用 IOC 容器中使用 shiro 的注解. 但必须在配置了 LifecycleBeanPostProcessor 之后才可以使用.
 -->
 <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>
 <!--
 6. 配置 ShiroFilter.
 6.1 id 必须和 web.xml 文件中配置的 DelegatingFilterProxy 的 <filter-name> 一致.
 若不一致, 则会抛出: NoSuchBeanDefinitionException. 因为 Shiro 会来 IOC 容器中查找和 <filter-name> 名字对应的 filter bean.
 -->
 <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
 <property name="securityManager" ref="securityManager"/>
 <property name="loginUrl" value="/index.do"/>
 <property name="successUrl" value="/welcome.do"/>
 <property name="unauthorizedUrl" value="/unauthorized.do"/>
 <property name="filters">
 <map>
 <entry key="roles">
 <!--通过拦截器将roles[]里角色之间的'且'改为'或'-->
 <bean class="com.wqqd.findfocus.shiro.CustomRolesAuthorizationFilter" />
 </entry>
 </map>
 </property>
 <!--将下面的filterChainDefinitions内容改为动态获取-->
 <property name="filterChainDefinitionMap" ref="filterChainDefinitionMap"></property>
 <!--
 	配置哪些页面需要受保护.
 	以及访问这些页面需要的权限.
 	1). anon 可以被匿名访问
 	2). authc 必须认证(即登录)后才可能访问的页面.
 	3). logout 登出.
 	4). roles 角色过滤器
 -->
 <!--<property name="filterChainDefinitions">-->
 <!--<value>-->
 <!--/logout.do = logout-->
 <!--/** = anon-->
 <!--</value>-->
 <!--</property>-->
 </bean>
 <!--通过实例工厂方法的形式注入map-->
 <bean id="filterChainDefinitionMap"
 factory-bean="filterChainDefinitionMapBuilder" factory-method="buildFilterChainDefinitionMap"></bean>
 <bean id="filterChainDefinitionMapBuilder"
 class="com.wqqd.findfocus.shiro.factory.FilterChainDefinitionMapBuilder"></bean>
</beans>

下面是对应的几个bean

ShiroRealm

package com.wqqd.findfocus.shiro;
import com.wqqd.findfocus.bean.UserLoginBean;
import com.wqqd.findfocus.common.Const;
import com.wqqd.findfocus.pojo.UserAdmin;
import com.wqqd.findfocus.service.IMenuSysService;
import com.wqqd.findfocus.service.IUserAdminService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.HashSet;
import java.util.Set;
/**
 * @author kexiang.bao
 * @create 2018-09-18 15:59
 */
public class ShiroRealm extends AuthorizingRealm{
 @Autowired
 IUserAdminService iUserAdminService;
 @Autowired
 IMenuSysService iMenuSysService;
 @Override
 protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
 UsernamePasswordToken upToken = (UsernamePasswordToken) authenticationToken;
 String userAdminAcct = upToken.getUsername();
 UserAdmin userAdmin = iUserAdminService.getUserAdminByAcct(userAdminAcct);
 if(userAdmin == null){
 throw new UnknownAccountException();
 }
 UserLoginBean userLoginBean = new UserLoginBean();
 userLoginBean.setUserAdmin(userAdmin);
 Object principal = userLoginBean;
 Object credentials = userAdmin.getUserAdminPwd();
 userAdmin.setUserAdminPwd("");
 ByteSource credentialsSalt = ByteSource.Util.bytes(userAdmin.getUserAdminAcct());
 String realmName = super.getName();
 SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(principal,credentials,credentialsSalt,realmName);
 return info;
 }
 //授权会被 shiro 回调的方法
 @Override
 protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
 //1. 从 PrincipalCollection 中来获取登录用户的信息
 Object principal = principals.getPrimaryPrincipal();
 UserLoginBean userLoginBean = (UserLoginBean)principal;
 UserAdmin userAdmin = userLoginBean.getUserAdmin();
 //2. 利用登录的用户的信息来用户当前用户的角色和权限(可能需要查询数据库)
 String roleCode = userAdmin.getRoleCode();
 //超级管理员拥有所有权限
 Set<String> permissionSet = null;
 boolean isSuperAdmin = Const.SUPER_ADMIN_ROLE_CODE.equals(roleCode);
 if(isSuperAdmin){
 permissionSet = iMenuSysService.getAllPermissionCodes();
 }else{
 permissionSet = iMenuSysService.getPermissionCodes(roleCode);
 }
 Set<String> roles = new HashSet<String>();
 roles.add(roleCode);
 //3. 创建 SimpleAuthorizationInfo, 并设置其 roles 属性和 permissions 属性.
 SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roles);
 info.addStringPermissions(permissionSet);
 //4. 返回 SimpleAuthorizationInfo 对象.
 return info;
 }
}

CustomRolesAuthorizationFilter

package com.wqqd.findfocus.shiro;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authz.AuthorizationFilter;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
 * @author kexiang.bao
 * @create 2018-09-19 20:26
 */
public class CustomRolesAuthorizationFilter extends AuthorizationFilter {
 @Override
 protected boolean isAccessAllowed(ServletRequest req, ServletResponse resp, Object mappedValue) throws Exception {
 Subject subject = getSubject(req, resp);
 String[] rolesArray = (String[]) mappedValue;
 if (rolesArray == null || rolesArray.length == 0) { //没有角色限制,有权限访问
 return true;
 }
 for (int i = 0; i < rolesArray.length; i++) {
 if (subject.hasRole(rolesArray[i])) { //若当前用户是rolesArray中的任何一个,则有权限访问
 return true;
 }
 }
 return false;
 }
}

FilterChainDefinitionMapBuilder

package com.wqqd.findfocus.shiro.factory;
/**
 * @author kexiang.bao
 * @create 2018-09-19 11:46
 */
import com.wqqd.findfocus.service.IMenuSysService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.LinkedHashMap;
public class FilterChainDefinitionMapBuilder {
 @Autowired
 IMenuSysService iMenuSysService;
 public LinkedHashMap<String, String> buildFilterChainDefinitionMap(){
 LinkedHashMap<String, String> map = new LinkedHashMap<>();
 map.put("/index.do", "anon");
 map.put("/validateMsgSend.do","anon");
 map.put("/login.do","anon");
 map.put("/logout.do","logout");
 map.put("/static/**","anon");
 map.putAll(iMenuSysService.getRoleMenuMap());
 map.put("/**", "authc");
 return map;
 }
}

至此就基本结束了。但是还有一个小坑。。就是当ajax请求如果超时或者没有权限的时候,需要前后端稍微处理一下。

@RequestMapping(value = "/index.do")
 public String jsp(HttpServletRequest request,HttpServletResponse response){
 if (SecurityUtils.getSubject().isAuthenticated()){
 return "redirect:/welcome.do";
 }
 boolean isAjax = isAjaxRequest(request);
 if(isAjax) {
 response.setHeader("Session-Status", "timeout");
 }
 return "login";
 }
private boolean isAjaxRequest(HttpServletRequest request){
 Enumeration<String> values = request.getHeaders("X-Requested-With");
 while(values.hasMoreElements()) {
 String value = values.nextElement();
 if("XMLHttpRequest".equalsIgnoreCase(value)) {
 return true;
 }
 }
 return false;
 }
@RequestMapping(value = "/unauthorized.do")
 @ResponseBody
 public ServerResponse<String> unauthorized(HttpServletRequest request,HttpServletResponse response){
 boolean isAjax = isAjaxRequest(request);
 if(isAjax) {
 response.setHeader("Access-Status", "denied");
 }
 return ServerResponse.createByErrorMessage("您无权进行该操作");
 }

前端加上

$(document).ajaxComplete(function(event, xhr, settings) {
 if (xhr.getResponseHeader('Session-Status') == 'timeout') {
 alert("您长时间未操作,请重新登录");
 window.location.reload();
 } else if(xhr.getResponseHeader('Access-Status') == 'denied'){
 alert("您没有权限进行该操作!请联系管理员");
 window.location.reload();
 }else if(403 == xhr.status) {
 window.location.reload();
 }
 });

至此就应该结束了,有问题再回来补充。


相关推荐

最全的MySQL总结,助你向阿里“开炮”(面试题+笔记+思维图)

前言作为一名编程人员,对MySQL一定不会陌生,尤其是互联网行业,对MySQL的使用是比较多的。对于求职者来说,MySQL又是面试中一定会问到的重点,很多人拥有大厂梦,却因为MySQL败下阵来。实际上...

Redis数据库从入门到精通(redis数据库设计)

目录一、常见的非关系型数据库NOSQL分类二、了解Redis三、Redis的单节点安装教程四、Redis的常用命令1、Help帮助命令2、SET命令3、过期命令4、查找键命令5、操作键命令6、GET命...

netcore 急速接入第三方登录,不看后悔

新年新气象,趁着新年的喜庆,肝了十来天,终于发了第一版,希望大家喜欢。如果有不喜欢看文字的童鞋,可以直接看下面的地址体验一下:https://oauthlogin.net/前言此次带来得这个小项目是...

精选 30 个 C++ 面试题(含解析)(c++面试题和答案汇总)

大家好,我是柠檬哥,专注编程知识分享。欢迎关注@程序员柠檬橙,编程路上不迷路,私信发送以下关键字获取编程资源:发送1024打包下载10个G编程资源学习资料发送001获取阿里大神LeetCode...

Oracle 12c系列(一)|多租户容器数据库

作者杨禹航出品沃趣技术Oracle12.1发布至今已有多年,但国内Oracle12C的用户并不多,随着12.2在去年的发布,选择安装Oracle12c的客户量明显增加,在接下来的几年中,Or...

flutter系列之:UI layout简介(flutter-ui-nice)

简介对于一个前端框架来说,除了各个组件之外,最重要的就是将这些组件进行连接的布局了。布局的英文名叫做layout,就是用来描述如何将组件进行摆放的一个约束。在flutter中,基本上所有的对象都是wi...

Flutter 分页功能表格控件(flutter 列表)

老孟导读:前2天有读者问到是否有带分页功能的表格控件,今天分页功能的表格控件详细解析来来。PaginatedDataTablePaginatedDataTable是一个带分页功能的DataTable,...

Flutter | 使用BottomNavigationBar快速构建底部导航

平时我们在使用app时经常会看到底部导航栏,而在flutter中它的实现也较为简单.需要用到的组件:BottomNavigationBar导航栏的主体BottomNavigationBarI...

Android中的数据库和本地存储在Flutter中是怎样实现的

如何使用SharedPreferences?在Android中,你可以使用SharedPreferencesAPI来存储少量的键值对。在Flutter中,使用Shared_Pref...

Flet,一个Flutter应用的实用Python库!

▼Flet:用Python轻松构建跨平台应用!在纷繁复杂的Python框架中,Flet宛如一缕清风,为开发者带来极致的跨平台应用开发体验。它用最简单的Python代码,帮你实现移动端、桌面端...

flutter系列之:做一个图像滤镜(flutter photo)

简介很多时候,我们需要一些特效功能,比如给图片做个滤镜什么的,如果是h5页面,那么我们可以很容易的通过css滤镜来实现这个功能。那么如果在flutter中,如果要实现这样的滤镜功能应该怎么处理呢?一起...

flutter软件开发笔记20-flutter web开发

flutterweb开发优势比较多,采用统一的语言,就能开发不同类型的软件,在web开发中,特别是后台式软件中,相比传统的html5开发,更高效,有点像c++编程的方式,把web设计出来了。一...

Flutter实战-请求封装(五)之设置抓包Proxy

用了两年的flutter,有了一些心得,不虚头巴脑,只求实战有用,以供学习或使用flutter的小伙伴参考,学习尚浅,如有不正确的地方还望各路大神指正,以免误人子弟,在此拜谢~(原创不易,转发请标注来...

为什么不在 Flutter 中使用全局变量来管理状态

我相信没有人用全局变量来管理Flutter应用程序的状态。毫无疑问,我们的Flutter应用程序需要状态管理包或Flutter的基本小部件(例如InheritedWidget或St...

Flutter 攻略(Dart基本数据类型,变量 整理 2)

代码运行从main方法开始voidmain(){print("hellodart");}变量与常量var声明变量未初始化变量为nullvarc;//未初始化print(c)...