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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 配置安全管理器对象 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- 注入自定义realm域对象 -->
<property name="realm" ref="authRealm"/>
<!-- 注入缓存对象 -->
<property name="cacheManager" ref="shiroEhcacheManager"/>
</bean>
<!-- 自定义realm域对象 -->
<bean id="authRealm" class="cn.action.shiro.AuthRealm">
<property name="userService" ref="userService"/>
<!-- 注入密码比较器对象 -->
<property name="credentialsMatcher" ref="customCredentialsMatcher"/>
</bean>
<!-- 自己编写密码比较器对象 -->
<bean id="customCredentialsMatcher" class="cn.action.shiro.CustomCredentialsMatcher"/>
<!-- Spring整合shiro框架 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 注入安全管理器对象 -->
<property name="securityManager" ref="securityManager"/>
<!--登录页面 -->
<property name="loginUrl" value="/index.jsp"></property>
<!-- 登录成功后,暂时没用到
<property name="successUrl" value="/home.action"></property>
-->
<!-- 如果用户没有权限,提供错误的提示页面 -->
<property name="unauthorizedUrl" value="/demo.jsp"></property>
<property name="filterChainDefinitions">
<!-- /**代表下面的多级目录也过滤 -->
<value>
/index.jsp* = anon
/demo.jsp* = anon
/home* = anon
/sysadmin/login/login.jsp* = anon
/sysadmin/login/logout.jsp* = anon
/login* = anon
/logout* = anon
/components/** = anon
/css/** = anon
/images/** = anon
/js/** = anon
/make/** = anon
/skin/** = anon
/stat/** = anon
/ufiles/** = anon
/validator/** = anon
/resource/** = anon
/sysadmin/deptAction_* = perms["部门管理"]
/** = authc
/*.* = authc
</value>
</property>
</bean>
<!-- 用户授权/认证信息Cache, 采用EhCache 缓存 -->
<bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml"/>
</bean>
</beans>
shiro缓存配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache updateCheck="false" name="shiroCache">
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
</ehcache>
自定义realm类
public class AuthRealm extends AuthorizingRealm {
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection pc) {
User user = (User) pc.fromRealm("abc").iterator().next();
// 获取到用户拥有的角色,对象导航
Set<Role> roles = user.getRoles();
// 存储菜单的名称
List<String> list = new ArrayList<String>();
// 遍历角色
for (Role role : roles) {
// 获取到角色所有的菜单
Set<Module> modules = role.getModules();
for (Module module : modules) {
// 找list集合存储菜单
list.add(module.getName());
}
}
// 把菜单集合封装到AuthorizationInfo对象中,返回。
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
info.addStringPermissions(list);
return info;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
UsernamePasswordToken upToken = (UsernamePasswordToken) token;
String username = upToken.getUsername();
String password = new String(upToken.getPassword());
List<User> list = userService.find("from User where userName=?", User.class, new Object[] { username });
if (null != list && list.size() > 0) {
User user = list.get(0);
AuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), "abc");
return info;
}
return null;
}
}
自定义密码比较器:
public class CustomCredentialsMatcher extends SimpleCredentialsMatcher{
public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
// 把token向下强转成实现类
UsernamePasswordToken upToken = (UsernamePasswordToken) token;
// 获取密码
String inputPwd = new String(upToken.getPassword());
// 对密码加密
String md5Pwd = Encrypt.md5(inputPwd, upToken.getUsername());
// 获取到数据库中的密码
String dbPwd = info.getCredentials().toString();
return super.equals(md5Pwd, dbPwd);
}
}
授权页面:
引入shiro标签<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro"%>
使用
<shiro:hasPermission name="系统首页">
<span id="topmenu" οnclick="toModule('home');">系统首页</span>
<span id="tm_separator"></span>
</shiro:hasPermission>