shiro是一个特别简单,易用的框架,在此记录一下shiro的使用配置。

首先,创建四张表:user  role  user_role  permission,分别为用户、角色、用户与角色关系表和权限表。

user表结构:

role表结构:

user_role

permission

当然,表结构如何设计是没有关系的,你可以根据自己偏好设计。

web.xml中加入shiro的过滤器:

<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

然后配置shiro,可以写在spring的配置文件中,也可以另起一个配置文件,配置内容如下:

    <!-- 配置权限管理器 -->
<bean id="myShiro" class="com.itmayong.util.MyShrio"></bean>
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- 我们自定义的realm -->
<property name="realm" ref="myShiro"/>
<!-- 缓存管理器 -->
<property name="cacheManager" ref="cacheManager"/>
</bean> <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="/main.jsp"/>
<!-- 错误页面 -->
<property name="unauthorizedUrl" value="/error.jsp"/>
<!-- 权限配置 -->
<property name="filterChainDefinitions">
<value>
<!-- anon无权限访问请求,此处是登录页面和登录请求 -->
/login.do = anon
/static/**=anon
<!-- 需要权限为add的用户才能访问此请求-->
/user=perms[user:add]
<!-- 需要管理员角色才能访问此页面 -->
/user/add=roles[admin]
<!--拦截非静态资源的所有请求-->
/** = authc
</value>
</property>
</bean> <bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager" />
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />

上面的配置文件我们指定了自定义的realm,内容如下:

public class MyShrio extends AuthorizingRealm{  

    @Autowired
private UserServiceIf userService;
/**
* 权限认证,获取登录用户的权限
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
String loginName=(String) principalCollection.fromRealm(getName()).iterator().next();
//此处连库匹配了登录用户的数据,具体怎么做,需要根据个人需求而定
User user=userService.findByName(loginName);
List<Role> list = user.getRoleList();
if(user!=null){
SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
//获取用户的角色名称
info.setRoles(user.getRolesName());
//获取用户的权限
List<Role> roleList=user.getRoleList();
for (Role role : roleList) {
info.addStringPermissions(role.getPermissionsName());
}
return info;
}
return null;
} /**
* 登录认证,创建用户的登录信息
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken authenticationToken) throws AuthenticationException {
UsernamePasswordToken token=(UsernamePasswordToken) authenticationToken;
//判断用户登录状态
User user=userService.findByName(token.getUsername());
if(user!=null){
//保存用户登录信息到认证中
return new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), getName());
}
return null;
} }

最后,你可以写一个页面和Controller进行测试了,当然也可以细分一下权限和角色,以应用到实际更复杂的场景中。

页面内容可以使用下边的方式对需要角色和权限控制的内容进行包裹,以达到权限控制的目的。

<shiro:hasRole name="admin">需要管理员角色</shiro:hasRole>
<shiro:hasPermission name="add">需要add权限</shiro:hasPermission>

此文为记录文章,防止自己以后又忘记,当然,如果能帮助到想用shiro的人更好。如果错误,请多多包涵

基于spring的shiro配置的更多相关文章

  1. 实战:基于 Spring 的应用配置如何迁移至阿里云应用配置管理 ACM

    最近遇到一些开发者朋友,准备将原有的Java Spring的应用配置迁移到 阿里云应用配置管理 ACM 中.迁移过程中,遇到不少有趣的问题.本文将通过一个简单的样例来还原迁移过程中遇到的问题和相关解决 ...

  2. spring整合shiro配置BUG,Tomcat启动不了:Error during artifact deployment. See server log for details

    现象 spring配置shiro权限控制之后,项目无法启动 [2019-08-09 09:00:35,800] Artifact export_web_manager:war exploded: Er ...

  3. 基于Spring的Appium配置应用

    本文主要是讲述,使用Spring框架,优化Appium的Driver调用,并将写在代码里的大量配置参数定义到配置文件当中,还可灵活的控制调用AndroidDriver还是IOSDriver. Spri ...

  4. spring与shiro配置详解

    1.加入shiro相关依赖的jar包 pom.xml部分内容如下: <dependency> <groupId>org.apache.shiro</groupId> ...

  5. Spring Boot -Shiro配置多Realm

    核心类简介 xxxToken:用户凭证 xxxFilter:生产token,设置登录成功,登录失败处理方法,判断是否登录连接等 xxxRealm:依据配置的支持Token来认证用户信息,授权用户权限 ...

  6. 简单两步快速实现shiro的配置和使用,包含登录验证、角色验证、权限验证以及shiro登录注销流程(基于spring的方式,使用maven构建)

    前言: shiro因为其简单.可靠.实现方便而成为现在最常用的安全框架,那么这篇文章除了会用简洁明了的方式讲一下基于spring的shiro详细配置和登录注销功能使用之外,也会根据惯例在文章最后总结一 ...

  7. Spring与Shiro整合

    Spring整合篇--Shiro 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 什么是Shiro? 链接:https://www.cnblogs.com/StanleyBlogs/ ...

  8. 基于Spring框架的Shiro配置

    一.在web.xml中添加shiro过滤器  <!-- Shiro filter--> <filter> <filter-name>shiroFilter</ ...

  9. 基于Spring框架的Shiro配置(转发:http://kdboy.iteye.com/blog/1103794)

    一.在web.xml中添加shiro过滤器 <!-- Shiro filter--> <filter> <filter-name>shiroFilter</f ...

随机推荐

  1. atitit.seo 发帖关键词以及链接的制作.doc

    atitit.seo 发帖关键词以及链接的制作.doc 1. 关键词的获得(by cate) 1 1.1. 删除统计数量     Cartier(144)  格式 1 1.2. \(\d*\)  替换 ...

  2. bazel-编译一个源文件生成可执行程序

    demo1 使用bazel编译一个源文件生成可执行程序简单示例 demo1目录树 demo1 ├── app │ ├── BUILD │ └── hello_world.cpp ├── README. ...

  3. jsp页面和js代码中使用sessionScope获取session值

    场景:有些实体对象可以放到HttpSession对象中,保正在一个会话期间可以随时获取这个对象的属性,例如可以将登录用户的信息写入session,以保证页面随时可以获取并显示这个用户的状态信息.下面以 ...

  4. python操作word之pywin32的安装

    PyCharm 2016.2 官网中文汉化破解版 注册码 http://idea.lanyus.com/ 首先下载安装win32com,下载32位的,不然安装的时候可能检测不到python https ...

  5. 基于jquery垂直缩略图切换相册

    今天给大家分享一款垂直缩略图切换jQuery相册,这是一款垂直缩略图左右滚动切换响应式jQuery图片相册代码.该 插件适用浏览器:IE8.360.FireFox.Chrome.Safari.Oper ...

  6. at91 uart driver for vxworks

    /* at91UART.c - AT91RM9200 serial driver */ /* Copyright 2003-2004 Coordinate Co., Ltd. */ /* Copyri ...

  7. netifd

    Netifd是OpenWrt中用于进行网络配置的守护进程,基本上所有网络接口设置以及内核的netlink事件都可以由netifd来处理完成. 在启动netifd之前用户需要将所需的配置写入uci配置文 ...

  8. [爬虫]Python爬虫基础

    一.什么是爬虫,爬虫能做什么 爬虫,即网络爬虫,大家可以理解为在网络上爬行的一直蜘蛛,互联网就比作一张大网,而爬虫便是在这张网上爬来爬去的蜘蛛咯,如果它遇到资源,那么它就会抓取下来.比如它在抓取一个网 ...

  9. Golang 中操作 Mongo Update 的方法

    Golang 和 MongoDB 中的 ISODate 时间交互问题 2018年02月27日 11:28:43 独一无二的小个性 阅读数:357 标签: GolangMongoDB时间交互时间转换 更 ...

  10. git提交空文件夹目录结构

    find . -name ".git" | xargs rm -Rf 在git 目录下执行find . -type d -empty -exec touch {}/.gitigno ...