shiro动态控制url资源
怎么利用shiro权限动态控制每个url资源呢?主要包括jsp(html)页面、action的url访问,而静态资源和登录资源则可直接访问。
所谓动态控制url就是url的权限控制不是手动写死在配置文件中,而是根据数据库的变化而变化。
表结构:
user2:用户表
t_role:角色表
t_user_role:用户角色表
t_privilege:权限资源表
t_role_privilege:角色权限资源表
shiro动态控制url资源:
applicationContext-shiro.xml配置chainDefinitionSectionMetaSource:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <description>apache shiro配置</description> <bean id="chainDefinitionSectionMetaSource" class="com.test.www.web.filter.ChainDefinitionSectionMetaSource">
<property name="filterChainDefinitions">
<value>
<!-- 静态资源允许访问 -->
<!-- /** = anon -->
<!-- /app/** = anon
/assets/** = anon -->
<!-- 登录页允许访问 -->
<!-- /user/login.htm = anon -->
<!-- /login.jsp = anon -->
<!-- /*.jsp = anon -->
<!-- /js/**=anon -->
<!-- 登录页面 -->
/index2.jsp = anon
/js/** = anon
/css/** = anon
/images/** = anon
/assets/** = anon
/test/loginAdmin.html=anon
/logout=logout <!-- 这才是对退出的配置 -->
<!-- /test/add.html = perms["user:add"] -->
<!-- /test/add.html = perms["/test/add.html"] -->
<!-- 其他资源需要认证 -->
<!-- /** = authc -->
<!-- /logout=logout --> <!-- 这才是对退出的配置,不能放在/** = authc的后面 --> </value>
</property>
</bean> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="filters">
<map>
<entry key="logout" value-ref="logoutFilter"/>
</map>
</property>
<property name="securityManager" ref="securityManager"/>
<!-- <property name="loginUrl" value="/index2.jsp"/>
<property name="successUrl" value="/main/main.htm"/>
<property name="unauthorizedUrl" value="/page/401.htm"/> -->
<!-- shiro判断是否登录,没有登录则跳转到登录页面,loginUrl对应登录页面的路径 -->
<property name="loginUrl" value="/index2.jsp"/>
<property name="unauthorizedUrl" value="/page/401.htm"/><!-- /page/401.htm --> <property name="filterChainDefinitionMap" ref="chainDefinitionSectionMetaSource" />
</bean> <!-- 缓存管理器 使用Ehcache实现 -->
<!--
<bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager" p:cacheManagerConfigFile="/WEB-INF/conf/ehcache-shiro.xml">
</bean>
-->
<!-- 会话DAO -->
<bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.MemorySessionDAO"/> <!-- 会话管理器 -->
<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<property name="sessionDAO" ref="sessionDAO"/>
</bean> <!-- 安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realms">
<list>
<ref bean="securityRealm"/>
</list>
</property>
<!-- cacheManager,集合spring缓存工厂 -->
<!-- <property name="cacheManager" ref="shiroEhcacheManager" />
<property name="sessionManager" ref="sessionManager" /> -->
</bean> <!-- Shiro生命周期处理器 -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> </beans>
ChainDefinitionSectionMetaSource.java:
package com.test.www.web.filter; import java.text.MessageFormat;
import java.util.List; import javax.annotation.Resource; import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.config.Ini;
import org.apache.shiro.config.Ini.Section;
import org.springframework.beans.factory.FactoryBean; import com.test.www.web.entity.user.TPrivilege;
import com.test.www.web.service.user.TPrivilegeService; public class ChainDefinitionSectionMetaSource implements FactoryBean<Ini.Section>{ public static final String PREMISSION_STRING="perms[\"{0}\"]"; private String filterChainDefinitions; /*private PrivilegeDao privilegeDao; public PrivilegeDao getPrivilegeDao() {
return privilegeDao;
}*/ @Resource
private TPrivilegeService tPrivilegeService; public String getFilterChainDefinitions() {
return filterChainDefinitions;
} @Resource
public void setFilterChainDefinitions(String filterChainDefinitions) {
String fiter="";//改正后的url配置
/*List<Privilege> list = privilegeDao.getAll();
for (Iterator<Privilege> it = list.iterator(); it.hasNext();) {
Privilege privilege = it.next();
if(!StringUtils.isEmpty(privilege.getUrl())) {
fiter+="/"+privilege.getUrl()+" = authc," +MessageFormat.format(PREMISSION_STRING,privilege.getPerms()) +"\n";
}//追加beans.xml中已经有的过滤
}*/ List<TPrivilege> tPrivilegeList = tPrivilegeService.selectAllPrivileges();
if(tPrivilegeList!=null && tPrivilegeList.size()>0){
for (TPrivilege tPrivilege : tPrivilegeList) {
if(!StringUtils.isEmpty(tPrivilege.getUrl())) {
fiter += tPrivilege.getUrl()+" = authc," +MessageFormat.format(PREMISSION_STRING,tPrivilege.getUrl()) +"\n";
}//追加beans.xml中已经有的过滤
}
} //对url拦截
fiter += "/**"+" = authc" +"\n";
//fiter+="/test/add.html"+" = authc," +MessageFormat.format(PREMISSION_STRING,"/test/add.html") +"\n";
//fiter+="/js/**"+" = authc," +MessageFormat.format(PREMISSION_STRING,"/js/**") +"\n";
//fiter+="/js/**"+" = " +MessageFormat.format(PREMISSION_STRING,"/js/**") +"\n";
System.out.println(filterChainDefinitions+fiter);
this.filterChainDefinitions = filterChainDefinitions+fiter;
} /*@Resource
public void setPrivilegeDao(PrivilegeDao privilegeDao) {
this.privilegeDao = privilegeDao;
}*/ public Section getObject(){
Ini ini = new Ini();//网上好多都是在这里配置URL的。但是发现是错误的。
ini.load(filterChainDefinitions);
Ini.Section section = ini.getSection(Ini.DEFAULT_SECTION_NAME);
return section;
} public Class<?> getObjectType() {
return this.getClass();
} public boolean isSingleton() {
return false;
} }
服务器启动的时候会执行ChainDefinitionSectionMetaSource类,加载系统权限资源表所有的url到filterChainDefinitions中(在filterChainDefinitions后追加),从而实现对所有url资源的权限控制的配置:
这个类就是用来在filterChainDefinitions中追加url资源权限控制的。
注意:
/** = authc应放在最后,否则会影响追加的权限资源的控制。
当访问资源的时候,例如/test/toAddUser.html,就会进入shiro授权方法中进行权限校验,如果没有权限则进入到401未授权,否则正常访问。
SecurityRealm.java:
package com.test.www.web.security; import java.util.List; import javax.annotation.Resource; import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Component; import com.test.www.web.entity.user.TRolePrivilegeKey;
import com.test.www.web.entity.user.TUserRoleKey;
import com.test.www.web.entity.user.User;
import com.test.www.web.service.user.TRolePrivilegeService;
import com.test.www.web.service.user.TUserRoleService;
import com.test.www.web.service.user.UserService; /**
* 用户身份验证,授权 Realm 组件
*
**/
@Component(value = "securityRealm")
public class SecurityRealm extends AuthorizingRealm {
@Resource
private UserService userService;
@Resource
private TUserRoleService tUserRoleService;
@Resource
private TRolePrivilegeService tRolePrivilegeService;
/**
* 权限检查
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
/*SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
String username = String.valueOf(principals.getPrimaryPrincipal()); System.out.println("ssssssss");*/
String username = principals.getPrimaryPrincipal().toString() ;
System.out.println(username); Subject subject = SecurityUtils.getSubject();
User user = (User)subject.getPrincipal(); SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo() ; /*Set<String> roleName = t_userService.findRoles(username) ;
Set<String> permissions = t_userService.findPermissions(username) ;*/
//final List<Role> roleInfos = roleService.selectRolesByUserId(user.getUserId());
/*for (Role role : roleInfos) {
// 添加角色
System.err.println(role);
authorizationInfo.addRole(role.getRoleSign()); final List<Permission> permissions = permissionService.selectPermissionsByRoleId(role.getRoleId());
for (Permission permission : permissions) {
// 添加权限
System.err.println(permission);
authorizationInfo.addStringPermission(permission.getPermissionSign());
}
}*/ List<TUserRoleKey> tUserRoleKeyList = tUserRoleService.selectRolesByUserId(user.getId());
if(tUserRoleKeyList != null && tUserRoleKeyList.size()>0){
for(TUserRoleKey tUserRoleKey : tUserRoleKeyList){
authorizationInfo.addRole(tUserRoleKey.getCode());
List<TRolePrivilegeKey> tRolePrivilegeKeyList = tRolePrivilegeService.selectPrivilegesByRoleId(tUserRoleKey.getRoleId());
for(TRolePrivilegeKey tRolePrivilegeKey : tRolePrivilegeKeyList){
authorizationInfo.addStringPermission(tRolePrivilegeKey.getUrl());
}
}
}
//Set<String> roleName = new HashSet<String>();
//Set<String> permissions = new HashSet<String>();
//查询角色
//roleName.add("admin");
//根据角色查询权限
//permissions.add("/test/add.html1"); //pub:coursecategory user:add
//permissions.add("/js/**");
//permissions.add("/jsp");
//authorizationInfo.setRoles(roleName);
//authorizationInfo.setStringPermissions(permissions);
return authorizationInfo;
} /**
* 登录验证
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
/* String username = String.valueOf(token.getPrincipal());
String password = new String((char[]) token.getCredentials());
System.out.println("aaaaaaa");
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(username, password, getName());*/
//int i = 1/0; //获取用户账号
//验证账号密码
UsernamePasswordToken userToken = (UsernamePasswordToken) token;
System.out.println("1:"+userToken.getUsername());
User user = userService.getUserByUserName(userToken.getUsername());
System.out.println("2");
if (user != null){
//将查询到的用户账号和密码存放到 authenticationInfo用于后面的权限判断。第三个参数传入realmName。
AuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user,user.getPassword(),this.getClass().getSimpleName()) ;
return authenticationInfo ;
}else{
return null ;
}
} }
至此,shiro动态控制url权限资源已完成。
说明:
url资源(action、静态html或动态页面jsp的url)保存在数据库中。
shiro动态控制url资源的更多相关文章
- shiro 静态页面资源不显示 解决方案(转)
最近做一个ssm+shiro的框架整和 不加shiro之前ssm中css和图片显示正常.加上以后无法显示. 解决方案: shiro有静态资源过滤. 配置资源匿名访问即可 <property na ...
- webpack处理url资源的配置
webpack处理url资源的配置 1.安装 npm i url-loader -D 2.修改webpack.config.js const path = require('path'); // 启用 ...
- URL资源跨域访问 跨域使用session信息
SilverLight 出于对安全性的考虑默认情况下对URL的访问进行了严格的限制,只允许访问同一子域下的URL资源. 下表列出了Silverlight 2.0 中 URL 访问规则: WebCl ...
- 项目一:第十二天 1、常见权限控制方式 2、基于shiro提供url拦截方式验证权限 3、在realm中授权 5、总结验证权限方式(四种) 6、用户注销7、基于treegrid实现菜单展示
1 课程计划 1. 常见权限控制方式 2. 基于shiro提供url拦截方式验证权限 3. 在realm中授权 4. 基于shiro提供注解方式验证权限 5. 总结验证权限方式(四种) 6. 用户注销 ...
- [Shiro] - 基于URL配置动态权限
基于shiro进阶 更改了数据库表 之前的PageController是通过@RequiresPermissions和@RequiresRoles进行是否有权限/是否有角色的判定调用@RequestM ...
- 【android】 中文URL资源找不到的问题
在博客园安卓客户端时,遇到过中文资源找不到的问题 背景:在使用PICASSO的时候,遇到过中文路径加载失败.比如 https://images0.cnblogs.com/news_topic/携程.j ...
- 解决springboot 配置文件未映射静态资源文件 导致shiro拦截静态资源的问题
---------------------------------------------------------------------------------------------------- ...
- 不写一行代码,利用常用工具和软件批量下载URL资源
有时候会遇到这种情况:想从某个网站下载一批东西,目标URL是比较规整的,而且结构都一样(仅某些字段不同).但又懒得开IDE专门写个脚本去弄,今天就和大家分享一下,如何利用手边常用的软件和工具,不用写一 ...
- Shiro配置URL过滤
常用过滤器: anon 不需要认证 authc 需要认证 user 验证通过或RememberMe登录的都可以 URL说明: /admin?=authc 表示 ...
随机推荐
- Qt-信号和槽-多对多
前言:介绍1对多,多对1以及多对多的案例. 一.1对多 演示内容:在QLineEdit输入时,同步label,text browser以及调试输出板同步显示. 1.1 新建工程 1.2 添加部件 拖入 ...
- 判断是否是Ajax请求
Request.IsAjaxRequest()判断是否是ajax请求原理:Http协议上有个X-Requested-With:XML HttpRequest属性判断的 mvc后台通过Request可以 ...
- python课程设计笔记(一)开发环境配置
今天开始学python,一个月后交成果?还是希望自己不要浮躁,认真地去学,有所付出也不期望太大回报. 现在还是一脸懵逼的状态,看着教程一点点来吧= = 毕竟我是最棒的最发光的阳光彩虹小白马! 1. 去 ...
- 记一次redis-cluster的切换
# redis-cli -h 10.5.8.18 -c -p 8001 cluster nodes|grep master 6d2f817064a10631648f24f450a37237b3d53f ...
- C++下面关于字符串数组的一些操作
今天在写一个搜索引擎的分词系统,是很简单的那种,但是居然费了我一天的时间还没完成,晚上估计还得弄一会了,但是在这个过程中,遇到了集中关于字符串数组的操作,值得和大家分享一下. 首先是关于统计字符串数组 ...
- Kattis - Eight Queens
Eight Queens In the game of chess, the queen is a powerful piece. It can attack by moving any number ...
- [分享]前端javascript插件(均开源)
记录并分享一些自己使用过的插件,便于以后有相应的功能需要使用可以及时想到. 1:数字插件countUp.js 功能:用于动态显示数字的增加和减少 项目github地址:http://inorganik ...
- js类的使用
brush示例 以d3的一个brush进行叙述,示例见: https://bl.ocks.org/xunhanliu/6f0b46789842e9e19e6cfe9bd0b16806 应用情形: 当页 ...
- 安装idea
1.下载idea https://www.jetbrains.com/idea/download/#section=linux 2.解压 sudo tar -zxvf ideaIC-2018.3.2 ...
- 决策树(Decision Trees)
简介 决策树是一个预测模型,通过坐标数据进行多次分割,找出分界线,绘制决策树. 在机器学习中,决策树学习算法就是根据数据,使用计算机算法自动找出决策边界. 每一次分割代表一次决策,多次决策而形成决策树 ...