1、导入shiro相应jar包,也可下载shiro-all.jar;

2、web.xml添加shiroFilter配置,类似于mvc

  <!-- shiro 安全过滤器-->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<async-supported>true</async-supported>
<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>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>

3、添加shiro配置文件,在spring-conf.xml导入

1 <import resource="classpath*:conf/spring-shiro.xml"/> 

编写spring-conf.xml

 <description>Shiro安全配置</description>
<!-- 扫描service注入realm -->
<context:component-scan base-package="com.myssm.yuan.service" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
<!--securityManager是shiro的核心,初始化时协调各个模块运行-->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!--单个realm使用realm,如果有多个realm,使用realms属性代替-->
<property name="realm" ref="userRealm" />
<property name="cacheManager" ref="shiroEhcacheManager" />
</bean>
<!--realm配置,realm是shiro的桥梁,它主要是用来判断subject是否可以登录及权限等-->
<bean id="userRealm" class="com.myssm.yuan.shiro.UserRealm" />
<!-- <property name="userService" ref="userService"/></bean> 不扫描可采用此方法注入-->
<!--shiro过滤器配置,bean的id值须与web中的filter-name的值相同-->
<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="/WEB-INF/page/index.jsp" />
<property name="unauthorizedUrl" value="/login/unauthorized" />
<property name="filterChainDefinitions">
<value>
/login/logout=logout
/login/**=anon
/**=authc,rest
</value>
</property>
</bean>
<!-- 用户授权/认证信息Cache, 采用EhCache 缓存 -->
<bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:conf/ehcache-shiro.xml"/>
</bean>
<!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

3.1 在mybatis配置文件里sqlcofig.xml.添加shiro缓存配置文件

 <?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<diskStore path="java.io.tmpdir"/>
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="900" timeToLiveSeconds="1800"
overflowToDisk="false"
memoryStoreEvictionPolicy="LFU" />
<cache name="testEhcache"
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="900"
timeToLiveSeconds="1800"
memoryStoreEvictionPolicy="LFU" /> </ehcache>

4、添加配置文件中配置的自定义realm,继承AuthorizingRealm

 /**
* 授权
* <p>Title: doGetAuthorizationInfo</p>
* <p>Description: </p>
* @param principals
* @return
* @see org.apache.shiro.realm.AuthorizingRealm#doGetAuthorizationInfo(org.apache.shiro.subject.PrincipalCollection)
*/ @Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();//未进行授权处理
return authorizationInfo;
} /**
* 认证
* <p>Title: doGetAuthenticationInfo</p>
* <p>Description: </p>
* @param token
* @return
* @throws AuthenticationException
* @see org.apache.shiro.realm.AuthenticatingRealm#doGetAuthenticationInfo(org.apache.shiro.authc.AuthenticationToken)
*/ @Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken token) throws AuthenticationException {
UsernamePasswordToken usernamePasswordToke = (UsernamePasswordToken)token;
String account = usernamePasswordToke.getUsername();
String pwd = String.valueOf(usernamePasswordToke.getPassword());
User user = this.userService.getUserByAccount(account);
if( user == null ){
throw new UnknownAccountException();
}
if( !user.getPassword().equals(pwd)){
throw new IncorrectCredentialsException();
}
// if(Boolean.TRUE.equals( user.getLocked())){
// throw new LockedAccountException(); //帐号锁定
// }
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
account,pwd,this.getName()); //此处未进行密码加密处理
return authenticationInfo;
}

5、增加登录jsp及controller进行测试,结果:未登录自动跳到login.jsp,登录成功调到index.jsp

ssm框架整合shiro的更多相关文章

  1. shiro系列三、ssm框架整合shiro实现权限控制

    shiro权限框架是一个非常优秀的框架,前面的几篇文章对shiro进行了非常详细的介绍和原理分析,那么接下来让我们开始在web项目中使用它(javase也能用shiro): 一.数据库表结构设计 二. ...

  2. SSM框架整合项目 :租房管理系统

    使用ssm框架整合,oracle数据库 框架: Spring SpringMVC MyBatis 导包: 1, spring 2, MyBatis 3, mybatis-spring 4, fastj ...

  3. 基于maven的ssm框架整合

    基于maven的ssm框架整合 第一步:通过maven建立一个web项目.                第二步:pom文件导入jar包                              (1 ...

  4. JavaWeb之ssm框架整合,用户角色权限管理

    SSM框架整合 Spring SpringMVC MyBatis 导包: 1, spring 2, MyBatis 3, mybatis-spring 4, fastjson 5, aspectwea ...

  5. SSM框架整合环境构建——基于Spring4和Mybatis3

    目录 环境 配置说明 所需jar包 配置db.properties 配置log4j.properties 配置spring.xml 配置mybatis-spring.xml 配置springmvc.x ...

  6. springmvc(二) ssm框架整合的各种配置

    ssm:springmvc.spring.mybatis这三个框架的整合,有耐心一步步走. --WH 一.SSM框架整合 1.1.整合思路 从底层整合起,也就是先整合mybatis与spring,然后 ...

  7. SSM框架整合的其它方式

    ---------------------siwuxie095                                 SSM 框架整合的其它方式         1.主要是整合 Spring ...

  8. SSM框架整合过程总结

    -----------------------siwuxie095                                 SSM 框架整合过程总结         1.导入相关 jar 包( ...

  9. SSM框架整合思想

    -------------------siwuxie095                                 SSM 框架整合思想         1.SSM 框架,即 SpringMV ...

随机推荐

  1. Win10 取消桌面快捷键图标

    新建文本文档 --- 写入如下内容 --- 改名为 .bat 并运行 @echo off color 2 reg delete HKCR\lnkfile /v IsShortcut /f reg de ...

  2. Kafka controller重设计

    本文主要参考社区0.11版本Controller的重设计方案,试图给大家梳理一下Kafka controller这个组件在设计上的一些重要思考.众所周知,Kafka中有个关键组件叫controller ...

  3. N76E003之串口

    N76E003包含两个具备增强的自动地址识别和帧错误检测功能的全双工串口.由于两个串口的控制位是一样的,为了区分两个串口控制位,串口1的控制位以“_1”结尾(例如SCON_1).下述详例以串口0为例. ...

  4. Cufon在渲染网页字体你不知道的事

    清单 1. 无效的 font-family 字体指定 <style> .introduction { font-family:'Baroque Script';} </style&g ...

  5. linux批量修改文件名

    源文件; [root@test_machine fuzj]# ls fuzj-1.txt  fuzj-2.txt  fuzj-3.txt  fuzj-4.txt  fuzj-5.txt  fuzj-6 ...

  6. django rest framwork教程之 viewsets和routers

    ViewSets 和Routers REST框架包括一个用于抽象处理的ViewSets,允许开发人员集中精力对API的状态和交互进行建模,并根据常见约定自动处理URL构造. Viewset 类和 Vi ...

  7. 【sql进阶】查询每天、每个设备的第一条数据

    需求如下 每个设备(不同DeviceID).每天会向数据库插入多条数据,求每天.每个设备插入的第一条数据. 下面SQL中的 ShareRecommendID 类比不同设备的DeviceID. ROW_ ...

  8. 老徐FrankXuLei 受邀为花旗银行讲授《微软WCF服务分布式开发与SOA架构设计课程》

    老徐FrankXuLei 受邀为花旗银行上海研发中心讲授<微软WCF服务分布式开发与SOA架构设计课程> 受邀为花旗银行上海研发中心讲授<微软WCF服务分布式开发与SOA架构设计课程 ...

  9. QWidget编写的安卓app

    最近为了配套人脸识别的整套设备去检测,特意做了个机关控制app,需要现场修改前端设备和服务器设备的一些参数以便进行检测.qt做一些简单的app还是非常方便的.特意增加了禁用屏保功能.apk文件体验:h ...

  10. commander.js 制作简易的 MINA CLI 脚手架

    出发点并不是小程序本身,是想要做一个脚手架(command-line interface),看过 VUE / REACT 脚手架,觉得很厉害,但是并不太知道里面是怎么做成的,所以最近研究了研究,看能不 ...