1、取消我们自己写的密码比较器(因为我们继承SimpleCredentialsMatcher
并重写类他的方法public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info))
意思就是不用写这个类也会执行密码比较器 这Shiro已经写好的
但必须在token之前将密码进行加密;
MD5Hash Md5 = new MD5Hash(password,username,2)
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
2、授权
使用shiro标签 必须导入标签库<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro" %>
页面中使用一个shiro标签 shiro:hasPermission 内部会找编写的权限代码
//意思是 是否有abc这个模块的权限
<shiro:hasPermission name="abc"></shiro:hasPermission>
当进入这个标签时
会跳转到我们写的授权和认证的类中的方法
可以在这个方法中查看用户是否有abc这权限
// 授权方法
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
// 1、获取用户信息
User user = (User) arg0.getPrimaryPrincipal();
// 2、获取用户下所有角色
Set<Role> roles = user.getRoles();
for (Role role : roles) {
// 获取角色下的所有模块
Set<Module> modules = role.getModules();
for (Module module : modules) {
// 3、授权
info.addStringPermission(module.getCpermission());
}
}
return info;
}
//这就是在登录时存的密码
new AuthorizationInfo 的实现类 SimpleAuthorizationInfo
调用方法hasPermission ("abc")//现在这个用户具有abc这个模块的权限了
通过参数PrincipalCollection 获取我们的用户信息
(用户信息是在认证结束时存在里面的)SimpleAuthenticationInfo(Object principal, Object credentials, String realmName)
参数的方法:arg0.getPrimaryPrincipal() 获取主要的结束用户信息
得到用户信息就可以查看具体有哪些权限
3、过滤器链加上 perms过滤器 可以指定地方跳转
1、perms是登录后看你有没有这个权限请问别的模块
没有强行从地址栏进入 看具体怎么设置 直接跳转到什么地方看业务逻辑
没有权限跳转到index.jsp
<property name="unauthorizedUrl" value="/index.jsp"/>
2、perms的配置注意事项
不建议使用的配置 因为有时候拦截不了
/XXXAction* = perms["xxxx"]
建议加上路径
/xxxxx/xxxxAction* = perms["xxxx"]
4、有注解方式加入拦截器 拦截到了会抛出异常,要处理异常,并指定跳转页面
注解具体加在哪里你是
注解开发: 加在service 因为加在Action层 会与与Spring注解有冲突
配置文件开发:加在Action
注解具体加在哪里看你的业务逻辑
@RequiresPermissions(value="xxx管理")
5、左侧菜单动态加载说明
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<ul>
<!-- 定义一个变量 赋空值 -->
<c:set var="aaa" value=""/>
<!-- 遍历当前登录用户的角色列表 在session域获取用户角色 遍历 -->
<c:forEach items="${_CURRENT_USER.roles }" var="role">
<!-- 遍历每个角色下的模块 -->
<c:forEach items="${role.modules }" var="module">
<!-- 如果该模块没有输出过,则要进行输出,否则这个模块就不输出 -->
<c:if test="${(moduleName eq module.remark) and module.ctype==1 }">
<!--fn:contains 函数是判断 “aaa” 中是否包含module.cpermission -->
<c:if test="${fn:contains(aaa,module.cpermission) eq false }">
<c:set var="aaa" value="${aaa},${module.cpermission }"/>
<li>
<a href="${ctx}/${module.curl}" onclick="linkHighlighted(this)" target="main" id="aa_1">
${module.cpermission }
</a>
</li>
</c:if>
</c:if>
</c:forEach>
</c:forEach>
</ul>