SSH实现一个简单的权限控制实例(一)
2016-08-11 10:33
531 查看
首先,需要创建数据表,先建一个用户表User,和一个菜单表 Menu.还有用户所属角色,用来批量控制用户权限.
在webRoot下编写一个index.jsp页面:
导入ssh需要的jar包,配置好环境:
web.xml:
beans.xml:
struts.xml:
以上,环境就已经配置好,struts中的index.action用来访问登录页面使用.
编写struts的IndexAction类:
然后,启动服务就可以在浏览器访问了:
CREATE TABLE `tb_menu` ( `menu_id` int(10) NOT NULL AUTO_INCREMENT, `menu_name` varchar(20) DEFAULT NULL, `menu_url` varchar(100) DEFAULT NULL, `parent_id` int(10) DEFAULT NULL, PRIMARY KEY (`menu_id`) ) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8; CREATE TABLE `tb_role` ( `role_id` int(10) NOT NULL AUTO_INCREMENT, `role_name` varchar(20) DEFAULT NULL, `rights` varchar(100) DEFAULT NULL, PRIMARY KEY (`role_id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; CREATE TABLE `tb_user` ( `user_id` int(10) NOT NULL AUTO_INCREMENT, `loginname` varchar(20) DEFAULT NULL, `password` varchar(20) DEFAULT NULL, `username` varchar(20) DEFAULT NULL, `rights` varchar(100) DEFAULT NULL, `status` tinyint(1) DEFAULT NULL, `role_id` int(10) DEFAULT NULL, `last_login` datetime DEFAULT NULL, PRIMARY KEY (`user_id`) ) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8;
在webRoot下编写一个index.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <script type="text/javascript"> document.location = "jsp/login.jsp"; </script> <%-- <jsp:forward page="index.action"/> --%> </body> </html>
导入ssh需要的jar包,配置好环境:
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> <!-- default: /WEB-INF/applicationContext.xml --> </listener> <context-param> <param-name>contextConfigLocation</param-name> <!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> --> <param-value>classpath:beans.xml</param-value> </context-param> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>sessionFactoryBeanName</param-name> <param-value>sf</param-value> </init-param> <init-param> <param-name>singleSession</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>flushMode</param-name> <param-value>AUTO</param-value> </init-param> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping> </web-app>
beans.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="com.auth" /> <!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/spring" /> <property name="username" value="root" /> <property name="password" value="bjsxt" /> </bean> --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="sf" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <value>com.auth.entity</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sf"></property> </bean> <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true" /> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sf" /> </bean> </beans>
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.devMode" value="false" /> <constant name="struts.i18n.encoding" value="utf-8"></constant> <constant name="struts.action.extension" value="html"></constant> <package name="auth" extends="struts-default" namespace="/auth"> <action name="index" class="indexAction"> <result>/jsp/login.jsp</result> </action> </package> </struts>
以上,环境就已经配置好,struts中的index.action用来访问登录页面使用.
编写struts的IndexAction类:
import org.springframework.stereotype.Component; import com.opensymphony.xwork2.ActionSupport; @Component public class IndexAction extends ActionSupport{ public String execute() { return SUCCESS; } }
然后,启动服务就可以在浏览器访问了:
![](http://static.oschina.net/uploads/space/2016/0811/103412_iPZS_2861620.png)
相关文章推荐
- SSH实现一个简单的权限控制实例(二)
- SSH实现一个简单的权限控制实例(三)
- Spring MVC代码实例系列-11:Spring MVC实现简单的权限控制拦截器和请求信息统计拦截器
- 简单客户系统的权限控制实现
- 实现委托的一个简单实例(附多点委托)
- 在家看孩子,10分钟生成一个业务管理系统【能支持多数据库,大数据,流程审批,权限控制,即时提醒】,视频讲解详细实现步骤
- 一个简单实现登陆控制的struts2框架
- 【分享】分享一个基于SSH实现的简单学生选课系统(附源码)
- [WMI实例]实现文件系统管理功能封装。提供共享文件和文件系统访问权限控制能力
- J2ME应用实例——一个简单的计算器实现(附源代码)
- 【分享】分享一个基于SSH实现的简单学生选课系统(附源码)
- CodeIgniter通过hook的方式实现简单的权限控制
- 在VC++实现控制程序只运行唯一一个实例
- ASP.NET 实现简单的权限控制
- 实例说明asp.net中的简单角色权限控制
- ADA程序实例(一个简单的智能指针实现)
- 一个简单的实现不同权限的用户登录后看到不同的菜单设计的数据库表清单
- 远程控制的简单实现之SSH
- redhat6.2通过allow deny实现ssh访问权限控制
- 一个简单的ssh+extjs实例