通过前几天的准备,今天我们要讲spring-security框架和教育办公系统集成到一起.这个说起来容易,可是做不是那么容易.刚刚学习过,有很多 细节记不住.做起来很慢,总是出现错误.不过,在对错误的修正中慢慢的掌握了spring-security的使用和注意事项.
1. 集成登出
1). 配置 spring-security
pring-security 框架的过滤器
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2). 在 struts-menu 的导航中加入登出: 在 EMSNavigationMenuAction 中加入 buildLogoffMenu 方法.
private void buildLogoffMenu(MenuComponent menuComponent, String contextPath){
MenuComponent logoffMenuComponent = new MenuComponent();
logoffMenuComponent.setTitle("登出");
logoffMenuComponent.setLocation(contextPath + "/logout");
logoffMenuComponent.setImage(contextPath + "/common/images/exit.png");
menuComponent.addMenuComponent(logoffMenuComponent);
}
3). 在登出按钮上加 js: 需要使整个页面变为 index 页. 而非仅是 content 页面
2. 在新建用户时, 可以为用户分配角色
1). 弹出所有角色供用户分配: 需要使用 jQuery 的 weebox 插件
<security:authorize ifAnyGranted="ROLE_UPDATE, ROLE_DELETE">
</security:authorize>
2). 为 employee-input.jsp 页面的 <a id="role_a_id" href="">分配角色(必选)</a> 添加 weebox 插件的事件:当点击时, 弹出所有的角色对应的一个多选框
<c:forEach items="${requestScope.CN_ITCAST_EMS_ROLE_REQUEST_ALL}" var="role">
<html:multibox property="roles" value="${role.id}" style="border:none"></html:multibox>${role.name }<br>
</c:forEach>
3). 在提交表单时, 把角色也保存到数据库中
3. 在修改用户时, 可以为用户修改角色: ajax 修改, 类似添加的实现
4. 在显示员工详细信息时, 显示其所具有的权限
5. 使用 spring-security 包含页面: 根据权限来决定相关链接是否显示:
1). 使用 security 标签
<security:authorize ifAnyGranted="ROLE_EMPLOYEE_UPDATE, ROLE_EMPLOYEE_DELETE">
<td>
<security:authorize ifAllGranted="ROLE_EMPLOYEE_DELETE">
<a href="${cp }/employee-delete.do?employeeId = ${emp.employeeid}" class="_delete">删除</a>
</security:authorize>
<security:authorize ifAllGranted="ROLE_EMPLOYEE_UPDATE">
<a href='${cp }/employee-detail.do?employeeid=${emp.employeeid}&type=update'>修 改</a>
</security:authorize>
</td>
</security:authorize>
2). 在 js 中在 '' 中使用 security 标签
6. 首页登录失败时的提示信息.
1). 使用 request.getAttributeNames 和 session.getAttributeNames 查看有哪些信息返回, 筛选出需要的键值
2). 表单数据回显使用 value='${sessionScope.SPRING_SECURITY_LAST_USERNAME }', 因为 spring-security 在发生错误时,使用重定向机制而非转发,所以 ${param.j_username} 不好用.
7. excel 数据上传和下载
1). 操作 Excel: POI
2). 下载 Excel 文档:
①. 读取需要的数据
②. 生成 Workbook 对象
③. 由 Servlet API 进行下载操作.
1. 集成登出
1). 配置 spring-security
pring-security 框架的过滤器
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2). 在 struts-menu 的导航中加入登出: 在 EMSNavigationMenuAction 中加入 buildLogoffMenu 方法.
private void buildLogoffMenu(MenuComponent menuComponent, String contextPath){
MenuComponent logoffMenuComponent = new MenuComponent();
logoffMenuComponent.setTitle("登出");
logoffMenuComponent.setLocation(contextPath + "/logout");
logoffMenuComponent.setImage(contextPath + "/common/images/exit.png");
menuComponent.addMenuComponent(logoffMenuComponent);
}
3). 在登出按钮上加 js: 需要使整个页面变为 index 页. 而非仅是 content 页面
2. 在新建用户时, 可以为用户分配角色
1). 弹出所有角色供用户分配: 需要使用 jQuery 的 weebox 插件
<security:authorize ifAnyGranted="ROLE_UPDATE, ROLE_DELETE">
</security:authorize>
2). 为 employee-input.jsp 页面的 <a id="role_a_id" href="">分配角色(必选)</a> 添加 weebox 插件的事件:当点击时, 弹出所有的角色对应的一个多选框
<c:forEach items="${requestScope.CN_ITCAST_EMS_ROLE_REQUEST_ALL}" var="role">
<html:multibox property="roles" value="${role.id}" style="border:none"></html:multibox>${role.name }<br>
</c:forEach>
3). 在提交表单时, 把角色也保存到数据库中
3. 在修改用户时, 可以为用户修改角色: ajax 修改, 类似添加的实现
4. 在显示员工详细信息时, 显示其所具有的权限
5. 使用 spring-security 包含页面: 根据权限来决定相关链接是否显示:
1). 使用 security 标签
<security:authorize ifAnyGranted="ROLE_EMPLOYEE_UPDATE, ROLE_EMPLOYEE_DELETE">
<td>
<security:authorize ifAllGranted="ROLE_EMPLOYEE_DELETE">
<a href="${cp }/employee-delete.do?employeeId = ${emp.employeeid}" class="_delete">删除</a>
</security:authorize>
<security:authorize ifAllGranted="ROLE_EMPLOYEE_UPDATE">
<a href='${cp }/employee-detail.do?employeeid=${emp.employeeid}&type=update'>修 改</a>
</security:authorize>
</td>
</security:authorize>
2). 在 js 中在 '' 中使用 security 标签
6. 首页登录失败时的提示信息.
1). 使用 request.getAttributeNames 和 session.getAttributeNames 查看有哪些信息返回, 筛选出需要的键值
2). 表单数据回显使用 value='${sessionScope.SPRING_SECURITY_LAST_USERNAME }', 因为 spring-security 在发生错误时,使用重定向机制而非转发,所以 ${param.j_username} 不好用.
7. excel 数据上传和下载
1). 操作 Excel: POI
2). 下载 Excel 文档:
①. 读取需要的数据
②. 生成 Workbook 对象
③. 由 Servlet API 进行下载操作.