前言
Apache Shiro是Java的一个安全框架。目前,使用Apache Shiro的人越来越多,因为它相当简单,Shiro可以非常容易的开发出足够好的应用,其不仅可以用在JavaSE环境,也可以用在JavaEE环境。Shiro可以帮助我们完成:认证、授权、加密、会话管理、与Web集成、缓存等。
准备工作
- log4j-1.2.16.jar
- shiro-all-1.3.2.jar
- slf4j-api-1.6.1.jar
- slf4j-log4j12-1.6.1.jar
下面我们来开始配置Shiro
1、在web.xml中配置filter
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
<async-supported>true</async-supported>
<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、配置securityManager
<bean id="securityManager" class="org.apache.shiro.
web.mgt.DefaultWebSecurityManager">
<!-- <property name="cacheManager" ref="cacheManager"/> -->
<property name="realm" ref="jdbcRealm"/>
</bean>
3、配置缓存ehcache(可选)
<!--
需要加入 ehcache-core-2.5.2.jar及ehcache.xml
-->
<!--
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
</bean>
-->
4、配置Realm,同时配置加密器credentialsMatcher(加密器可选)
<!--
自定义Realm,同时配置加密器credentialsMatcher
-->
<bean id="credentialsMatcher"
class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<!-- 采用MD5加密 -->
<property name="hashAlgorithmName" value="MD5" />
<!-- 加密次数 -->
<property name="hashIterations" value="2" />
</bean>
<bean id="jdbcRealm" class="test.jia.com.shiro.ShiroRealm">
<property name="credentialsMatcher" ref="credentialsMatcher" />
</bean>
5、配置lifecycleBeanPostProcessor,可以自动的调用Spring IOC 容器中 Shiro Bean 的生命周期方法
<!--
可以自动的调用Spring IOC 容器中 Shiro Bean 的生命周期方法
-->
<bean id="lifecycleBeanPostProcessor"
class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
6、启用IOC容器中 Shiro 的注解
<!--
启用IOC容器中 Shiro 的注解,但必须配置了lifecycleBeanPostProcessor之后才可使用。
注意:这里需要修改spring-shiro.xml
需要将
<bean class="org.springframework.aop.framework.
autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor"/>
改为
<aop:config proxy-target-class="true"></aop:config>
不然shiro的注解不起作用!
-->
<!--
<bean class="org.springframework.aop.framework.
autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor"/>
-->
<aop:config proxy-target-class="true"></aop:config>
<bean class="org.apache.shiro.spring.security.
interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
7、配置ShiroFilter
注意: id必须和web.xml中的ShiroFilter的name一致
<!--
配置ShiroFilter
注意: id必须和web.xml中的ShiroFilter的name一致
-->
<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="/list.jsp"/>
<!-- 用户访问未对其授权的资源时,所显示的连接 -->
<property name="unauthorizedUrl" value="/unauthorized.jsp" />
<!--
配置哪些页面需要授权
anon : 可以被匿名访问
authc: 必须被认证(即登陆后)才可以被访问
logout:登出
注 :
1、filterChainDefinitions没有被覆盖到的路径可以直接访问到
2、路径采用优先被匹配的原则
例如:/login.jsp = anon
/** = authc
/list.jsp = anon
那么list.jsp的权限应该为authc,而不是anon,
因为/**在/list.jsp的前面,/**优先被匹配
-->
<property name="filterChainDefinitions">
<value>
/login.jsp = anon
/shiro/login = anon
/shiro/logout = logout
/LoginController/testRedis = anon
/** = authc
<!--
/*.js=anon
/test/*=anon
/test/**=anon
-->
</value>
</property>
</bean>
spring-shiro.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
</beans>
8、实现ShiroRealm
/**
* 实现ShiroRealm
*/
public class ShiroRealm extends AuthorizingRealm { //AuthenticatingRealm{
/**
* 授权,需要继承AuthorizingRealm
*
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection
principalCollection) {
System.out.println("doGetAuthorizationInfo");
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
info.addRole("user");
info.addStringPermission("user:delete");
Subject currentUser = SecurityUtils.getSubject();
Session session = currentUser.getSession();
return info;
}
/**
* 登录权限验证 需要继承AuthenticatingRealm
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken token) throws AuthenticationException {
//把AuthenticationToken转换为UsernamePasswordToken
UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
//从UsernamePasswordToken 获取 username
String username = usernamePasswordToken.getUsername();
String password = new String(usernamePasswordToken.getPassword());
//查询数据库
//若用户不存在,抛出异常UnknownAccountException异常
/**
* 根据情况,构建AuthenticationInfo对象,并返回
* AuthenticationInfo是个接口,通常实现类使用的是SimpleAuthenticationInfo
*
* principal:认证的实体信息,可以是username,也可以是数据库该条记录的对象
* credentials:数据库获取的密码
* realmName:调用当前父类里的 getName即可
*/
Object principal = username;
/**
* 此时credentials不能直接引用明文的password了,
* 因为shiro.xml里配置了加密规则,
* 登陆时会将token里的密码加密的返回值与credentials匹配,
* 如果能匹配上,说明密码正确。
*
* 注:在正式环境中不会这样写getMd5Password(password);
* 只需要把数据库取出来的结果赋值给credentials即可,
*/
Object credentials = getMd5Password(password);
//Object credentials = 数据库取出来的密码;
String realmName = getName();
// SimpleAuthenticationInfo info =
// new SimpleAuthenticationInfo(principal, credentials, realmName);
/**
* 与上面的SimpleAuthenticationInfo不同,多了一个credentialsSalt参数
* 由于每个人的密码MD5加密后有可能重复,系统存在安全隐患,
* 所以配置了更加安全的SimpleAuthenticationInfo,
* credentialsSalt参数的作用:将一个字符串与密码一起加密,
* 一般使用的字符串具有唯一性,例如:用户名
* 这样就实现了所有人的密码加密后不会存在重复值。
*/
credentials = getMd5Password2(username,password);
ByteSource credentialsSalt = ByteSource.Util.bytes(username);
SimpleAuthenticationInfo info =
new SimpleAuthenticationInfo(principal,
credentials, credentialsSalt, realmName);
System.out.println(token.hashCode());
return info;
}
/**
* 获取password Md5加密两次的值
* @param password
* @return
*/
public static String getMd5Password(String password){
SimpleHash simpleHash = new SimpleHash("MD5", "123456", null, 2);
String string = simpleHash.toString();
return string;
}
/**
* 获取username与password Md5加密两次的值
* @param username
* @param password
* @return
*/
public static String getMd5Password2(String username,String password){
ByteSource bytes = ByteSource.Util.bytes(username);
SimpleHash simpleHash = new SimpleHash("MD5", "123456", bytes, 2);
String string = simpleHash.toString();
return string;
}
}