Apache shiro学习

 

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 在权限管理系统中,基本做两项工作,认证(身份识别) 和 授权(权限判断)。

Apache shiro学习

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)