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. SPD

    一般是在内存条上的.1.SPD是SERIAL PRESENCE DETECT的缩写,中文意思是模组存在的串行检测.也即是通过上面讲的IIC串行接口的EEPROM对内存插槽中的模组存在的信息检查.这样的 ...

  2. cocos2d-x 之 CCProgressTimer

    --绕圆心转动的进度动画 local function SpriteProgressToRadial() local leftProgress = CCProgressTimer:create(CCS ...

  3. -[__NSArrayI removeAllObjects]: unrecognized selector sent to instance 0x7fa8dc830110

    问题 今天做项目,遇到了这个问题 -[__NSArrayI removeAllObjects]: unrecognized selector sent to instance 0x7fa8dc8301 ...

  4. vue的路由使用

    1). 安装 vue-router npm install vue-router --save 2). 新建路由配置 安装成功后,在 src 新建 router 文件夹,然后新建 index.js 文 ...

  5. python正则表达式匹配时间和IP地址

    t = '19:16:30' mt = re.match(r'^(0[0-9]|1[0-9]|2[0-3]|[0-9])\:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[ ...

  6. FreeRtos——任务删除,改变任务优先级

    以下转载自安富莱电子: http://forum.armfly.com/forum.php vTaskDelete() API 函数任务可以使用 API 函数 vTaskDelete()删除自己或其它 ...

  7. lua工具库penlight--08额外的库(一)

    额外的库 在这一节中的库不再被认为是Penlight的核心部分,但在需要时,仍提供专门的功能. 简单的输入的模式 Lua 的字符串模式匹配是非常强大,通常您将不需要传统的正则表达式库.即便如此,有时  ...

  8. EmWebAdmin 导航栏分析

    templates/gentelella/base.tpl <!DOCTYPE html> <html lang="en"> <!-- Smarty ...

  9. CentOS 6.5 安装 php7 教程 包很重要使用lnmp1.4里面的包

    ./configure \ --prefix=/usr/local/php-7.0.1 \ --with-mysql=mysqlnd \ --with-pdo-mysql=mysqlnd \ --wi ...

  10. golang :连接数据库闲置断线的问题

    golang在进行数据库操作,一般来说我们使用Open函数创建一个数据库(操作)句柄:func Open(driverName, dataSourceName string) (*DB, error) ...