java shiro实例_spring+shiro 整合实例代码详解
一、添加相关依赖
org.apache.shiro
shiro-core
1.2.1
org.apache.shiro
shiro-web
1.2.1
org.apache.shiro
shiro-ehcache
1.2.1
org.apache.shiro
shiro-spring
1.2.1
commons-logging
commons-logging
1.2
二、编写代码
1、自定义realm
public class CommonRealm extends AuthorizingRealm {
@Autowired
private UserLoginService userLoginService;
@Override
public String getName() {
return "CommonRealm";
}
//授权
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
String usernmae = (String) principals.getPrimaryPrincipal();
List permissions = new ArrayList();
if ("admin".equals(usernmae)) {
permissions.add("admin:ee");
}
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
info.addStringPermissions(permissions);
return info;
}
//身份认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String username = (String) token.getPrincipal();
User user = userLoginService.getUser(username);
if (user == null) {
return null;
}
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, user.getPassword(), getName());
return info;
}
}
2、login controller
@Controller
public class UserAction {
@Autowired
private UserLoginService userLoginService;
@RequestMapping("/login.do")
public String userLogin(HttpServletRequest request, String username, String password) throws Exception {
// 如果登陆失败从request中获取异常信息,shiroLoginFailure就是shiro异常类的全限定名
String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");
if (exceptionClassName != null) {
if (UnknownAccountException.class.getName().equals(exceptionClassName)) {
// 最终会抛给异常处理器
throw new XXXException("用户名不存在");
} else if (IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
throw new XXXException("用户名/密码错误");
} else {
throw new Exception();// 最终在异常处理器生成未知错误
}
}
// 如果登录成功的话不走此方法,shiro认证成功会自动跳转到上一个请求路径,配的的successUrl没效果,后边会说
// 登陆失败走此方法,捕获异常,然后 return ~ ,还到login页面
return "login.jsp";
}
}
3、检测权限 controller
//此方法为了验证权限是否生效
@RequestMapping("/findAll.do")
@RequiresPermissions("admin:ee")
public ModelAndView list(HttpServletRequest request){
.......
}
三、常见问题
因为有一些特别常见的问题,需要修改xml配置,所以现在先手问题,把xml配置放在后边,直接就配置完善好的xml
问题一:登陆成功后shiro默认跳到上一次请求,没有上一次请求默认跳到/ ,那我们就想控制调到自己定义的路径咋办呢?
解决方案:
步骤一:继承FormAuthenticationFilter类,重写onLoginSuccess方法,这里可以自定义路径,因为这里自定义了成功跳转的路径,所以配置里的successUrl不用配置,赔了也没效果。。
public class LoginSuccessToFilter extends FormAuthenticationFilter {
@Override
protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) throws Exception {
WebUtils.getAndClearSavedRequest(request);
WebUtils.redirectToSavedRequest(request,response,"/findAll.do");
return false;
}
}
步骤二:
在shiro的xml配置文件中配置
在 shiroFilter配置中引入,完整xml在后边
四、Xml配置
applicationContext-shiro.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
/image/** = anon
/css/** = anon
/js/** = anon
/logout.do = logout
/** = authc
springmvc的配置
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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
以上就是一个大概的整合和遇到的两个问题,博主也是查阅了很多的博客得到的较优答案,整理出来,已备后续参考,遇到一样问题的同学可以看看
java shiro实例_spring+shiro 整合实例代码详解相关推荐
- php java内存占用_PHP内存溢出优化代码详解
相信很多人做大批量数据导出和数据导入的时候,经常会遇到PHP内存溢出的问题,在解决了问题之后,总结了一些经验,整理成文章记录下. 优化点 1.优化SQL语句,避免慢查询,合理的建立索引,查询指定的字段 ...
- Socket编程之TCP实例(附C/C++代码详解)
说明: 主要分步骤给出Windows平台下socket编程的一个TCP实例:使用WINDOWS下网络编程规范Winsock完成网络通信: 对程序各部分细节进行描述. 套接字有三种传输类型SOCK_ST ...
- 【Java】Java中的设计模式的介绍以及代码详解
一.什么是设计模式? 设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案.这些解决方案是众多软件开发人员经过相当长的一段时间的试验和错误 总结出来的. 对问题行之有效地解决方式,是一种设计 ...
- Java GUI编程:swing创建窗体代码详解
package com.zxl;import java.awt.Container; import java.awt.FlowLayout;import javax.swing.JButton; im ...
- java排他_Java排他锁实现的代码详解
这篇文章主要介绍了Java编程实现排他锁的相关内容,叙述了实现此代码锁所需要的功能,以及作者的解决方案,然后向大家分享了设计源码,需要的朋友可以参考下. 一 .前言 某年某月某天,同事说需要一个文件排 ...
- java 文件下载详解_Java 从网上下载文件的几种方式实例代码详解
废话不多说了,直接给大家贴代码了,具体代码如下所示: package com.github.pandafang.tool; import java.io.BufferedOutputStream; i ...
- yii mysql 事务处理_Yii2中事务的使用实例代码详解
前言 一般我们做业务逻辑,都不会仅仅关联一个数据表,所以,会面临事务问题. 数据库事务(Database Transaction) ,是指作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全 ...
- vue build text html,Vue中v-text / v-HTML使用实例代码详解_放手_前端开发者
废话少说,代码如下所述: /p> 显示123 /p> 补充:vuejs {{}},v-text 和 v-html的区别 {{message}} let app = new Vue({ el ...
- vue中 点击事件的写法_vue实现绑定事件的方法实例代码详解
一.前言 vuejs中的事件绑定,使用来完成的,这里函数名是定义在Vue实例中的methods对象中的,Vue实例可以直接访问其中的方法. 二.事件绑定方式 1. 直接在标签中写js方法 执行方法的第 ...
- pos请求 微信小程序_微信小程序蓝牙连接小票打印机实例代码详解
1.连接蓝牙 (第一次发表博客) 第一步打开蓝牙并搜索附近打印机设备// startSearch: function() { var that = this wx.openBluetoothAdapt ...
最新文章
- mail 发送email
- 格式android id,android 获取APP的唯一标识applicationId的实例
- 各大视觉技术看透女神吗
- 虚拟与现实的距离——VR的2016正如移动互联网的2009【下篇】
- JavaIO流(2)--IO流原理、流的分类及节点流文件流操作
- 【课题报告】OpenCV 抠图项目实战(10)PyQt5 使用
- Moody Photoshop Panel for mac(ps专业色彩调色扩展面板)
- Ajax (部分二:prototype.js代码后半部分)自己做的,总结页面向后台传Form值、单个值和后台向前台传一个或是一组值...
- 问答| 为何会采用倒车入库(侧方位停车)方式?
- ThreadLocal总结(jdk1.8源码)
- [转]Ogre:Hardwarebuffer
- matlab幅度归一化,matlab归一化方法
- 华为荣耀3x畅玩版解锁
- Image Enhancement
- Word和Excel齐发力,将多个单元格的文本内容合并到一个单元格
- 决策树(ID3、C4.5、CART、随机森林)
- AX210 PCIE网卡 安装记录(linux)
- Android中Canvas和Paint的常用方法
- 计算机程序创始人阿达洛芙莱斯
- 7-36 调查电视节目受欢迎程度
热门文章
- 用easyx画五角星_【洛谷日报#195】有个东西叫EasyX
- mysql默认密码是多少_路由器192.168.1.1默认登录密码是多少?
- qlv视频转换器免费版_迅捷视频转换器无法转换腾讯视频怎么办?亲测操作快速转换...
- linux备份和还原设置密码,AnyBackup Linux操作系统备份与恢复最佳实践手册(33页)-原创力文档...
- 设计模式:000设计模式与面向对象介绍
- 各种池化操作(包括组合池化)
- Python机器学习:评价分类结果002精准率和召回率
- java rmi 多ip_在rmi连接中客户端指定一个ip地址,但程序执行中却转换成另一个地址...
- mysql jar jdk1.6_搭建非安装版mysql+jdk1.6+tomcat6
- html中js定义的方法无效,javascript中定义函数有几种常用方法?