apache shiro整合spring(一)
apache shiro整合spring
maven引入jar包
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-all</artifactId>
<version>1.2.3</version>
</dependency>
将shiro配置文件整合到spring体系中
方式一:直接在spring的配置文件中import shiro的配置文件
web.xml中配置spring
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
在spring配置文件中引入shiro配置文件
<import resource="applicationContext-shiro.xml"/>
方式二:直接在web.xml中配置shiro的配置文件
<!-- Spring ApplicationContext 配置文件的路径,可使用通配符,多个路径用,号分隔 此参数用于后面的Spring Context Loader -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext-shiro.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
在web.xml中定义shiro的过滤器
<!-- 这里的filter-name 要和spring 的applicationContext-shiro.xml 里的
org.apache.shiro.spring.web.ShiroFilterFactoryBean 的bean name 相同 -->
<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>
1.shiro中定义的filtername bean是要被wen.xml引用的,所以这里的filtername要和shiro中的filtername相同
2.shiro的filter要定义在Struts2的filter之前,否则action无法创建
shiro.xml配置文件(配置校验的策略,哪些校验,哪些放行)
<!-- 安全管理 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- Single realm app. If you have multiple realms, use the 'realms' property instead. -->
<property name="realm" ref="authRealm"/><!-- 引用自定义的realm -->
<!-- 二级缓存 -->
<property name="cacheManager" ref="shiroEhcacheManager"/>
</bean> <!-- 自定义权限认证 -->
<bean id="authRealm" class="com.fyh.www.shiro.AuthRealm">
<!-- <property name="userService" ref="userService"/> -->
<!-- 自定义密码加密算法 -->
<property name="credentialsMatcher" ref="passwordMatcher"/>
</bean> <!-- 设置密码加密策略 md5hash -->
<bean id="passwordMatcher" class="com.fyh.www.shiro.CustomCredentialsMatcher"/> <!-- 此bean要被web.xml引用,和web.xml中的filtername同名 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="/index.jsp" /> <!-- 没有认证返回地址 -->
<property name="unauthorizedUrl" value="/index2.jsp" /> <!-- 没有授权返回地址 -->
<property name="filterChainDefinitions">
<value> <!-- **代表任意子目录 -->
/login/** = anon
/user/** = authc,roles[admin]
/test/** = authc,perms[测试用的lkkk]
</value>
</property>
</bean> <!-- 用户授权/认证信息Cache, 采用EhCache 缓存 -->
<bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml"/>
</bean> <!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> <!-- 生成代理,通过代理进行控制 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor">
</bean> <!-- 安全管理器 -->
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
shiro缓存(ehcache支持ehcache-shiro.xml)
<ehcache updateCheck="false" name="shiroCache">
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
</ehcache>
java代码实现
自定义密码认证
public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {
public boolean doCredentialsMatch(AuthenticationToken token,
AuthenticationInfo info) {
UsernamePasswordToken usertoken = (UsernamePasswordToken) token;
//注意token.getPassword()拿到的是一个char[],不能直接用toString(),它底层实现不是我们想的直接字符串,只能强转
Object tokenCredentials = Encrypt.md5(String.valueOf(usertoken.getPassword()),usertoken.getUsername());
Object accountCredentials = getCredentials(info);
//将密码加密与系统加密后的密码校验,内容一致就返回true,不一致就返回false
return equals(tokenCredentials, accountCredentials);
}
}
util中定义shiro的MD5加密方法
public class Encrypt {
/*
* 散列算法一般用于生成数据的摘要信息,是一种不可逆的算法,一般适合存储密码之类的数据,
* 常见的散列算法如MD5、SHA等。一般进行散列时最好提供一个salt(盐),比如加密密码“admin”,
* 产生的散列值是“21232f297a57a5a743894a0e4a801fc3”,
* 可以到一些md5解密网站很容易的通过散列值得到密码“admin”,
* 即如果直接对密码进行散列相对来说破解更容易,此时我们可以加一些只有系统知道的干扰数据,
* 如用户名和ID(即盐);这样散列的对象是“密码+用户名+ID”,这样生成的散列值相对来说更难破解。
*/
//高强度加密算法,不可逆
public static String md5(String password, String salt){
return new Md5Hash(password,salt,2).toString();
}
public static void main(String[] args) {
System.out.println(new Md5Hash("123456","tony",2).toString());
}
}
自定义realm AuthRealm
在认证、授权内部实现机制中都有提到,最终处理都将交给Realm进行处理。
因为在Shiro中,最终是通过Realm来获取应用程序中的用户、角色及权限信息的。
通常情况下,在Realm中会直接从我们的数据源中获取Shiro需要的验证信息。可以说,Realm是专用于安全框架的DAO.
public class AuthRealm extends AuthorizingRealm{
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
//授权
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
System.out.println("授权");
//获取当前用户
User user = (User)principals.fromRealm(getName()).iterator().next();
//得到权限字符串
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
Set<Role> roles = user.getRoles();
List<String> list = new ArrayList();
for(Role role :roles){
Set<Module> modules = role.getModules();
for(Module m:modules){
if(m.getCtype()==0){
//说明是主菜单
list.add(m.getCpermission());
}
}
}
info.addStringPermissions(list);
return info;
}
//认证 登录
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken token) throws AuthenticationException {
System.out.println("认证");
UsernamePasswordToken upToken = (UsernamePasswordToken)token;
User user = userService.findUserByName(upToken.getUsername());
if(user==null){
return null;
}else{
AuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), getName());
return info;
}
}
}
修改传统登录为shiro登录
public class LoginAction extends BaseAction {
private static final long serialVersionUID = 1L;
private String username;
private String password;
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
public String login() throws Exception {
/*
* shiro登录方式:根据用户名获取密码,密码为null非法用户;有密码检查是否用户填写的密码
* 登录成功后无需往httpsession中存放当前用户,这样就跟web容器绑定,关联太紧密;它自己创建
* subject对象,实现自己的session。这个跟web容器脱离,实现松耦合。
*/
//调用shiro判断当前用户是否是系统用户
Subject subject = SecurityUtils.getSubject(); //得到当前用户
//shiro是将用户录入的登录名和密码(未加密)封装到token对象中
UsernamePasswordToken token = new UsernamePasswordToken(userName,password);
try{
subject.login(token); //自动调用AuthRealm.doGetAuthenticationInfo
//写seesion,保存当前user对象
User curUser = (User)subject.getPrincipal(); //从shiro中获取当前用户
System.out.println(curUser.getDept().getDeptName()); //让懒加载变成立即加载
Set<Role> roles = curUser.getRoles();
for(Role role :roles){
Set<Module> moduless = role.getModules();
for(Module m :moduless)
System.out.println(m.getName());
}
session.put(SysConstant.CURRENT_USER_INFO, curUser); //Principal 当前用户对象
}catch(Exception ex){
super.put("errorInfo","用户名密码错误,请重新填写!");
ex.printStackTrace();
return "login";
}
return SUCCESS;
}
public String logout(){
session.remove(SysConstant.CURRENT_USER_INFO); //删除session
return "logout";
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
页面使用shiro标签
<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro"%>
<shiro:hasPermission name="sysadmin">
<span id="topmenu" onclick="toModule('sysadmin');">系统管理</span>
</shiro:hasPermission>
apache shiro整合spring(一)的更多相关文章
- apache FtpServer 整合spring部署
我们在项目中可能会出现这样的需求,使用ftp上传很大的文件后对需要对文件进行相应的逻辑处理,这时我们可以使用apache ftpServer来处理这段逻辑,只要我们做相应的部署和编写我们的逻辑代码,这 ...
- 【权限管理】Apache Shiro和Spring Security的对比
一.Shiro简介 Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring Security,可能没有Spring Secu ...
- Shiro整合Spring
首先需要添加shiro的spring整合包. 要想在WEB应用中整合Spring和Shiro的话,首先需要添加一个由spring代理的过滤器如下: <!-- The filter-name ma ...
- Apache Shiro和Spring Security的详细对比
参考资料: 1)Apache Shiro Apache Shiro:http://shiro.apache.org/ 在Web项目中应用 Apache Shiro:http://www.ibm.com ...
- Apache Shiro 集成Spring(二)
1.依赖: <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-cor ...
- 权限框架Apache Shiro 和 Spring Security
Shiro 首先Shiro较之 Spring Security,Shiro在保持强大功能的同时,还在简单性和灵活性方面拥有巨大优势.Shiro是一个强大而灵活的开源安全框架,能够非常清晰的处理认证.授 ...
- apache FtpServer整合spring逻辑处理
上面我们的部署工作完成了,那么文件上传下载后,ftpserver会自动相应我们的上传下载操作,也就是说ftpServer服务器会得到触发,那么我们如果要得到文件的一些信息,比如说文件的路径.大小.类型 ...
- Apache Shiro 快速入门教程,shiro 基础教程
第一部分 什么是Apache Shiro 1.什么是 apache shiro : Apache Shiro是一个功能强大且易于使用的Java安全框架,提供了认证,授权,加密,和会话管理 ...
- Shiro学习总结(2)——Apache Shiro快速入门教程
第一部分 什么是Apache Shiro 1.什么是 apache shiro : Apache Shiro是一个功能强大且易于使用的Java安全框架,提供了认证,授权,加密,和会话管理 如同 spr ...
随机推荐
- HDU 1532 Drainage Ditches(网络流模板题)
题目大意:就是由于下大雨的时候约翰的农场就会被雨水给淹没,无奈下约翰不得不修建水沟,而且是网络水沟,并且聪明的约翰还控制了水的流速, 本题就是让你求出最大流速,无疑要运用到求最大流了.题中m为水沟数, ...
- 2017浙江省赛 B - Problem Preparation ZOJ - 3959
地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3959 题目: It's time to prepare the pr ...
- mysql更新子查询中的内容
UPDATE t_finance_certify_copy c SET c.biz_type=2,c.sub_biz_type=18WHERE c.finance_certify_id IN(SELE ...
- 20145316《Java程序设计》第9周学习总结
20145316<Java程序设计>第9周学习总结 教材学习内容总结 数据库本身是个独立运行的应用程序 撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找 JDBC(Ja ...
- 关于iOS UIWebView 加载网页,点击网页内某些控件导致 Application 'UIKitApplication:xxx.xxx.xxx' was killed by jetsam.
问题:公司用的腾讯问卷系统,内嵌在我们应用或游戏的自定义UIWebView里面展示,发现在iOS 10 以下系统,点击圆形勾选框 会大概率出现闪退. 通过联调发现:报了这样一个警告Applicatio ...
- php中获得数组长度的方法
php中获得数组长度的方法 count统计数组里元素的个数: strlen是统计数组中元素的长度: 你如果想统计数组中所有元素的长度,那就用循环统计吧tqeb 代码: $a = array( ...
- Statement与PreparedStatement
Statement 用于通用查询,能批处理 PreparedStatement(简称PS) 用于执行参数化查询,能批处理 什么是参数化查询? 指在设计与数据库链接并访问数据时,在需要填入数值或数据的地 ...
- 20145313 《Java程序设计》第十周学习总结
网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接收到指定的数据,这个就是狭义的网络编程范畴.在发送和接收数据时,大部分的程序设 ...
- lucene 简介和实践 分享
之前项目做了搜索的改造,使用lucene,公司内做了相关的技术分享,故先整理下ppt内容,后面会再把项目中的具体做法进行介绍 lucene 简介和实践 分享 搜索改造项目
- UVA 1642 Magical GCD(经典gcd)
题意:给你n(n<=100000)个正整数,求一个连续子序列使序列的所有元素的最大公约数与个数乘积最大 题解:我们知道一个原理就是对于n+1个数与n个数的最大公约数要么相等,要么减小并且减小至少 ...