`
endual
  • 浏览: 3544686 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Apache shiro学习

    博客分类:
  • java
 
阅读更多

 

1)springside的介绍https://github.com/springside/springside4/wiki/ShiroSecurity

1)springside的介绍https://github.com/springside/springside4/wiki/ShiroSecurity

1)springside的介绍https://github.com/springside/springside4/wiki/ShiroSecurity

1)springside的介绍https://github.com/springside/springside4/wiki/ShiroSecurity

 

 

Apache shiro学习

这两天在学习java的权限管理方案。两个选择springsecurtiy和shiro。前者应用比较成熟,但是使用比较复杂,后者国内使用的似乎还不是很广泛,但是使用及学习起来相对简单。

通过学习springside的webmini中shiro使用,对其有了一定了解。

1shiro简单介绍。

1)springside的介绍https://github.com/springside/springside4/wiki/ShiroSecurity

2)十分钟教程 http://shiro.apache.org/10-minute-tutorial.html

3)认证与授权 http://shiro.apache.org/guides.html

2 在权限管理系统中,基本做两项工作,认证(身份识别) 和 授权(权限判断)。

subject是领域对象的概念,也就是一个用户。用户对资源有权限。Realm通过编辑好的权限规则(可以为wildchar,也就是字符串)对subject进行权限判定。

3 spring web怎么使用呢。

1)web.xml中加入shiro.xml配置文件位置,shiro filter

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
   classpath*:/applicationContext.xml
   classpath*:/applicationContext-shiro.xml
  </param-value>
 </context-param>

<!-- Shiro Security filter-->
 <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>

2 在shiro.xml配置文件中,配置Securitymanager、 Realm,shiroFilter过滤链,及其他东西

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
 default-lazy-init="true">

 <description>Shiro Configuration</description>

 <!-- Shiro's main business-tier object for web-enabled applications -->
 <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager" depends-on="userDao,groupDao">
  <property name="realm" ref="shiroDbRealm" />
  <property name="cacheManager" ref="cacheManager" />
 </bean>

 <!-- 項目自定义的Realm -->
 <bean id="shiroDbRealm" class="org.springside.examples.miniweb.service.account.ShiroDbRealm">
  <property name="accountManager" ref="accountManager"/>
 </bean>

 <!-- Shiro Filter -->
 <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
  <property name="securityManager" ref="securityManager" />
  <property name="loginUrl" value="/login" />
  <property name="successUrl" value="/account/user/" />
  <property name="filterChainDefinitions">
   <value>
    /login = authc 这里用的是shiro自带的Filter,下边也是,
    /logout = logout
    /static/** = anon
       /** = user
    </value>
  </property>
 </bean>

 <!-- 用户授权信息Cache -->
 <bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager" />
 
 <!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
 <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
 
 <!-- AOP式方法级权限检查  -->
 <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
  <property name="proxyTargetClass" value="true" />
 </bean>
 
 <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
     <property name="securityManager" ref="securityManager"/>
 </bean>
</beans>

3 编写 Realm

package org.springside.examples.miniweb.service.account;

import java.io.Serializable;

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.cache.Cache;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.examples.miniweb.entity.account.Group;
import org.springside.examples.miniweb.entity.account.User;

/**
 * 自实现用户与权限查询.
 * 演示关系,密码用明文存储,因此使用默认 的SimpleCredentialsMatcher.
 */
public class ShiroDbRealm extends AuthorizingRealm {

 private AccountManager accountManager;

 /**
  * 认证回调函数, 登录时调用.
  */
 @Override
 protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
  UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
  User user = accountManager.findUserByLoginName(token.getUsername());
  if (user != null) {
   return new SimpleAuthenticationInfo(new ShiroUser(user.getLoginName(), user.getName()), user.getPassword(),
     getName());
  } else {
   return null;
  }
 }

 /**
  * 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用.
  */
 @Override
 protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
  ShiroUser shiroUser = (ShiroUser) principals.fromRealm(getName()).iterator().next();
  User user = accountManager.findUserByLoginName(shiroUser.getLoginName());
  if (user != null) {
   SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
   for (Group group : user.getGroupList()) {
    //基于Permission的权限信息
    info.addStringPermissions(group.getPermissionList());
   }
   return info;
  } else {
   return null;
  }
 }

 /**
  * 更新用户授权信息缓存.
  */
 public void clearCachedAuthorizationInfo(String principal) {
  SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName());
  clearCachedAuthorizationInfo(principals);
 }

 /**
  * 清除所有用户授权信息缓存.
  */
 public void clearAllCachedAuthorizationInfo() {
  Cache<Object, AuthorizationInfo> cache = getAuthorizationCache();
  if (cache != null) {
   for (Object key : cache.keys()) {
    cache.remove(key);
   }
  }
 }

 @Autowired
 public void setAccountManager(AccountManager accountManager) {
  this.accountManager = accountManager;
 }

 /**
  * 自定义Authentication对象,使得Subject除了携带用户的登录名外还可以携带更多信息.
  */
 public static class ShiroUser implements Serializable {

  private static final long serialVersionUID = -1748602382963711884L;
  private String loginName;
  private String name;

  public ShiroUser(String loginName, String name) {
   this.loginName = loginName;
   this.name = name;
  }

  public String getLoginName() {
   return loginName;
  }

  /**
   * 本函数输出将作为默认的<shiro:principal/>输出.
   */
  @Override
  public String toString() {
   return loginName;
  }

  public String getName() {
   return name;
  }
 }
}
在使用过程中,对于方法可以采用注释的方法引入权限判断。

@RequiresPermissions("user:edit")
 @RequestMapping(value = "update/{id}")
 public String updateForm(Model model) {
  model.addAttribute("allGroups", accountManager.getAllGroup());
  return "account/userForm";
 }

4基于注释的编程还不是很适应。

分享到:
评论

相关推荐

    Apache Shiro教程

    通过这个Apache Shiro教程,你可以学习到如何构建安全的Java应用,了解Shiro的配置和用法,以及如何在实际项目中实施身份验证、授权、会话管理和加密策略。无论你是初学者还是经验丰富的开发者,Shiro都是一个值得...

    Apache shiro 1.13.0源码

    总之,Apache Shiro 1.13.0 源码提供了深入了解 Java 安全框架的机会,无论是对于学习安全基础知识,还是解决实际项目中的安全问题,都是非常宝贵的资源。通过深入阅读和实践,开发者可以提升自身的安全编程能力,为...

    spring boot+mybatis+thymeleaf+apache shiro开发面向学习型的后台管理系统

    在本项目中,我们利用Spring Boot、MyBatis、Thymeleaf以及Apache Shiro这四个核心技术栈构建了一个面向学习型的后台管理系统。这个系统旨在提供一个高效、易用的平台,帮助用户进行学习资源的管理和分享。接下来,...

    apache shiro文档

    本文档合集包含了多个 PDF 文件,全面覆盖了 Apache Shiro 的核心概念和使用方法,非常适合初学者和进阶开发者学习。 1. **认证**:在 Shiro 中,认证是指验证用户身份的过程。Shiro 提供了简单易用的 API 来处理...

    Spring MVC+Mybatis+Ehcache+Apache Shiro+Bootstrap整合开发java仓库管理系统源码

    这是一个基于Java技术栈的仓库管理系统源码,使用了Spring MVC作为MVC框架,Mybatis作为持久层框架,Ehcache作为缓存管理工具,Apache Shiro进行权限控制,以及Bootstrap作为前端UI框架。下面将详细解析这些技术在...

    apache-shiro教程完整版.7z

    Apache Shiro 是一个强大且易用的...书中的目录将指导你逐个章节深入学习,从基础概念到高级应用,确保你能够全面掌握Apache Shiro 的精髓。通过实践书中给出的例子,相信你将能更好地理解和运用这个强大的安全框架。

    apache shiro 开发文档

    ### Apache Shiro 开发文档详解 #### 一、Apache Shiro 概述 Apache Shiro 是一款强大且灵活的开源安全框架,旨在简洁地解决身份验证(Authentication)、授权(Authorization)、会话管理(Session Management)...

    apache shiro中文教程

    截止到2015年8月1日,Shiro的最新版本为1.2.4,有兴趣的开发者可以访问官方提供的GitBook版本,进行阅读和学习。 Shiro的社区也十分活跃,开发者可以参与到翻译工作中来,贡献自己的力量。如果有任何疑问,可以通过...

    Pairing Apache Shiro and Java EE7

    《配对Apache Shiro和Java EE7》这本书详细介绍了如何将Apache Shiro安全框架与Java EE7企业版进行整合。在学习和实践中,本书以NetBeans集成开发环境(IDE)为基础,提供了丰富的实例教程,这些示例基于边界-控制-...

    apache-shiro 学习笔记

    Apache Shiro 是一个强大且易用的Java安全框架,提供了认证、授权、加密和会话管理功能,可以非常轻松地开发出足够安全的应用程序。它专注于应用程序的安全性,而不是网络安全性,因此它非常适合用于Web应用和桌面...

    Apache Shiro 中文手册

    通过阅读"Apache Shiro 中文手册",你将能够全面了解Shiro的工作原理,学习如何在项目中配置和使用它,从而提高你的应用安全性,并实现高效的身份验证和授权机制。无论是初学者还是有经验的开发者,这都是一个宝贵的...

    Apache_Shiro参考手册中文版_shiro_

    Apache Shiro是一款强大的Java安全框架,它为开发者提供了一种简单易用的API来处理认证、授权、会话...通过《Apache Shiro参考手册中文版》的详细学习,你将能够更好地应对各种安全场景,为你的项目提供稳固的保护。

    Apache Shiro 身份认证例子-源码

    在深入学习 Shiro 的身份认证示例时,你可以关注以下几个方面: - 如何创建自定义 Realm 以连接到实际的数据源(如数据库)并实现 `getPrincipals()` 和 `getCredentials()` 方法。 - 如何配置 Shiro 的 Ini 文件或...

    shiro jar包及源码下载

    Apache Shiro是一个强大且易用的Java安全框架,它提供了认证、授权、加密和会话管理功能,可以简化开发人员处理安全的复杂性。Shiro适用于各种应用,从小型独立应用到大型分布式系统,都能有效地支持。 在"shiro-...

    Apache Shiro 使用分享

    Apache Shiro 是一个强大且易用的Java安全框架,它提供了认证、授权、加密和会话管理功能,简化了企业级应用的安全实现。在本文中,我们将深入探讨Apache Shiro的核心概念、主要功能以及如何在实际项目中进行使用。 ...

    spring boot+mybatis+thymeleaf+apache shiro开发面向学习型的后台管理系统BootDo

    spring boot+mybatis+thymeleaf+apache shiro开发面向学习型的后台管理系统BootDo,参考地址 http://blog.csdn.net/zhaokejin521/article/details/78719722

    基于maven构建 spring mvc + apache shiro + mysql 框架搭建

    本文将深入探讨如何使用Maven作为构建工具,结合Spring MVC作为MVC框架,Apache Shiro作为安全管理组件,以及MySQL作为数据库来搭建这样的系统。这四个技术组件都是Java开发中的热门选择,它们各自在应用程序的不同...

    基于SpringBoot + Thymeleaf + Layui + Apache Shiro + Redis + .zip

    标题 "基于SpringBoot + Thymeleaf + Layui + Apache Shiro + Redis + .zip" 描述了一个使用现代Web开发技术构建的项目。这个项目利用了SpringBoot作为核心框架,Thymeleaf作为模板引擎,Layui作为前端UI框架,...

Global site tag (gtag.js) - Google Analytics