ssh登录实现
工程目录
配置文件详解
<span ><?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 定义数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<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>
<context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置HibernateSessionFactory工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<!-- 自动扫描注解方式配置的hibernate类文件 -->
<property name="packagesToScan">
<list>
<value>com.tonly.entity</value>
</list>
</property>
</bean> <!-- 配置Hibernate事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!-- 配置事务传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="new*" propagation="REQUIRED" />
<tx:method name="set*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="change*" propagation="REQUIRED" />
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="load*" propagation="REQUIRED" read-only="true" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice> <!-- 配置参与事务的类 -->
<aop:config>
<aop:pointcut id="serviceOperation" expression="execution(* com.tonly.service.*.*(..))" />
<!-- 切面事务归txAdvice处理,把上面我们所配置的事务管理两部分属性整合起来 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
</aop:config> <!-- 自动加载构建JavaBean,直接使用注解的话可以免去配置bean的麻烦,实体类可以被自动扫描 -->
<context:component-scan base-package="com.tonly" /> </beans> </span>
Hibernate.cfg.xml文件配置
<span ><?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration></span>
jdbc.properties文件配置
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_teststudio
jdbc.username=root
jdbc.password=123
struts.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<constant name="struts.action.extension" value="action" />
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<constant name="struts.multipart.maxSize" value="10000000"/> <!-- 大约10M --> <package name="ebuy" namespace="/" extends="struts-default"> <action name="user_*" method="{1}" class="com.tonly.action.UserAction">
<result name="error">login.jsp</result>
<result name="loginSuccess">main.jsp</result>
</action> </package> </struts>
login.jsp文件,主要实现登录(输入用户名、密码)、记住密码功能,比较简单,前台jsp页面使用bootstrap框架,界面效果如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="com.tonly.entity.User"%>
<%
if(request.getAttribute("user") == null){//说明是用户首次登录,而非服务端跳转,这时候再在cookies中进行取值操作
String userName = null;
String password = null;
Cookie[] cookies = request.getCookies();
for(int i=0; cookies!=null && i<cookies.length; i++){
if(cookies[i].getName().equals("user")){
userName = cookies[i].getValue().split("-")[0];
password = cookies[i].getValue().split("-")[1];
}
}
if(userName == null)
userName = "";
if(password == null)
password = "";
pageContext.setAttribute("user", new User(userName, password));//EL表达式取值范围为page request session application
}
%>
<html lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DQA - Test Studio</title>
<link
href="${pageContext.request.contextPath}/bootstrap/css/bootstrap.css"
rel="stylesheet">
<link
href="${pageContext.request.contextPath}/bootstrap/css/bootstrap-responsive.css"
rel="stylesheet">
<script src="${pageContext.request.contextPath}/bootstrap/js/jquery.js"></script>
<script
src="${pageContext.request.contextPath}/bootstrap/js/bootstrap.js"></script>
<style type="text/css">
body {
padding-top: 10px;
padding-bottom: 40px;
background-color: #D6D6D6;
}
.form-signin-heading {
text-align: center;
padding-bottom: 10;
}
.head{
width:100%;
height:200px;
}
.form-signin {
max-width: 390px;
padding: 19px 29px 0px;
margin: 0 auto 20px;
background-color: #fff;
border: 1px solid #e5e5e5;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
-webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, .05);
-moz-box-shadow: 1 1px 2px rgba(0, 0, 0, .05);
box-shadow: 0 1px 2px rgba(0, 0, 0, .05);
} .form-signin .form-signin-heading,.form-signin .checkbox {
margin-bottom: 10px;
} .form-signin input[type="text"],.form-signin input[type="password"] {
font-size: 12px;
height: 13px;
margin-bottom: 15px;
padding: 7px 9px;
width:100%;
}
</style>
<script type="text/javascript"> function resetForm(){
$("#userName").val("");
$("#password").val("");
} function checkForm(){
var userName = $("#userName").val();
var password = $("#password").val();
if(userName == ""){
$("#error").html("Please enter username");
return false;
}
if(password == ""){
$("#error").html("Please enter password");
return false;
}
return true;
} </script>
</head>
<body>
<div id="head" class="head">
<div >
<font size=6 color="#E50914"> </font>
</div>
<div >
<hr size=12 color=#E50914 width=100%>
</div>
</div>
<div class="container">
<form name="myForm" class="form-signin" action="user_login.action" method="post" onsubmit="return checkForm()">
<h2 class="form-signin-heading">Welcome to Test Studio</h2>
<input id="userName" name="user.userName" type="text"
value="${user.userName }" class="input-block-level"
placeholder="Username"> <input id="password" name="user.password"
type="password" value="${user.password }" class="input-block-level"
placeholder="Password"> <label class="checkbox"> <input
id="remember" name="remember" type="checkbox" value="remember-me">Remember me
<font id="error" color="red">${error}</font>
</label>
<div align="center">
<button class="btn btn-default btn-primary" type="submit">Login</button> <button class="btn btn-default btn-primary" type="button" onclick="javascript:resetForm()">Reset</button>
</div>
<br>
</form>
</div>
</body>
</html>
详细代码实现:
package com.tonly.service.impl; import java.util.LinkedList;
import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.tonly.dao.BaseDao;
import com.tonly.entity.PageBean;
import com.tonly.entity.User;
import com.tonly.service.UserService;
import com.tonly.util.StringUtil; @Service("userService")
public class UserServiceImpl implements UserService { @Resource
private BaseDao<User> baseDao; @Override
public User login(User user) {
List<Object> param = new LinkedList<Object>();
StringBuffer hql = new StringBuffer("from User u where u.userName = ? and u.password = ?");
if (user != null) {
param.add(user.getUserName());
param.add(user.getPassword());
return baseDao.get(hql.toString(), param);
}else {
return null;
}
} }
package com.tonly.dao.impl; import java.io.Serializable;
import java.util.List; import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import com.tonly.dao.BaseDao;
import com.tonly.entity.PageBean; @Repository("baseDao")
@SuppressWarnings("all")
public class BaseDaoImpl<T> implements BaseDao<T> { private SessionFactory sessionFactory; public SessionFactory getSessionFactory() {
return sessionFactory;
} @Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
} private Session getCurrentSession() {
return sessionFactory.getCurrentSession();
} public T get(Class<T> c, Serializable id) {
return (T) this.getCurrentSession().get(c, id);
} }
package com.tonly.service.impl; import java.util.LinkedList;
import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.tonly.dao.BaseDao;
import com.tonly.entity.PageBean;
import com.tonly.entity.User;
import com.tonly.service.UserService;
import com.tonly.util.StringUtil; @Service("userService")
public class UserServiceImpl implements UserService { @Resource
private BaseDao<User> baseDao; @Override
public User login(User user) {
List<Object> param = new LinkedList<Object>();
StringBuffer hql = new StringBuffer("from User u where u.userName = ? and u.password = ?");
if (user != null) {
param.add(user.getUserName());
param.add(user.getPassword());
return baseDao.get(hql.toString(), param);
}else {
return null;
}
} }
这里直接通过@Resource注入baseDao并传递泛型参数"User",这样可以直接使用baseDao.get(hql.toString(), param)方法获取对象。
package com.tonly.action; import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionSupport;
import com.tonly.entity.User;
import com.tonly.service.UserService; @Controller
public class UserAction extends ActionSupport implements ServletRequestAware{ /**
*
*/
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
} @Resource
private UserService userService;
private String error;
private User user;
private String remember; public String getRemember() {
return remember;
}
public void setRemember(String remember) {
this.remember = remember;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
} public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
} public String login() throws Exception{
HttpSession session = request.getSession();
User currentUser = userService.login(user);
if (currentUser == null) {
request.setAttribute("user",user);
error = "incorrect username or password";
return ERROR;
}else {
if ("remember-me".equals(remember)) {
remember(currentUser, ServletActionContext.getResponse());
}
session.setAttribute("currentUser", currentUser);
return "loginSuccess";
}
} private void remember(User user, HttpServletResponse response) throws Exception{
Cookie cookie = new Cookie("user", user.getUserName()+"-"+user.getPassword());
cookie.setMaxAge(1*60*60*24*7);//设置有效期一周
response.addCookie(cookie);
} }
实体User类具体代码如下:
package com.tonly.entity; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; @Entity
@Table(name="t_user")
public class User { private int id;
private String userName;
private String password;
private String sex;
private String email;
private String mobile;
private String address;
private int status = 1; //普通用户为1,管理员为2 public User() {
super();
}
public User(String userName, String password) {
super();
this.userName = userName;
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
@Id
@GeneratedValue(generator="_native")
@GenericGenerator(name="_native",strategy="native")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(length=20)
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
} }
整个业务逻辑是:
ssh登录实现的更多相关文章
- ssh登录 The authenticity of host 192.168.0.xxx can't be established. 的问题
scp免密码登录:Linux基础 - scp免密码登陆进行远程文件同步 执行scp一直是OK的,某天在本地生成了公钥私钥后,scp到某个IP报以下错误 The authenticity of host ...
- Azure PowerShell (12) 通过Azure PowerShell创建SSH登录的Linux VM
<Windows Azure Platform 系列文章目录> 本章将介绍如何使用Azure PowerShell,创建SSH登录的Linux VM 前提要求: 1.安装Azure Pow ...
- Linux SSH登录慢案例分析
手头有台Linux服务器ssh登录时超级慢,需要几十秒.其它服务器均没有这个问题.平时登录操作都默默忍了.今天终于忍不住想搞清楚到底什么原因.搜索了一下发现了很多关于ssh登录慢的资料,于是自己也学着 ...
- DAY6 使用ping钥匙临时开启SSH:22端口,实现远程安全SSH登录管理就这么简单
设置防火墙策略时,关于SSH:22访问权限,我们常常会设置服务器只接受某个固定IP(如公司IP)访问,但是当我们出差或在家情况需要登录服务器怎么办呢? 常用两种解决方案:1.通过VPN操作登录主机: ...
- Mac下,使用sshpass让iterm2支持多ssh登录信息保存
windows里有个Xshell非常的方便好使,因为它能保存你所有的ssh登录帐号信息.MAC下并没有xshell,有些也提供这样的功能,但效果都不好.iterm2是很好的终端,但却不能很好的支持多p ...
- ssh 登录
一.ssh登录过程 在实际开发中,经常使用ssh进行远程登录.ssh 登录到远程主机的过程包括: 版本号协商 密钥和算法协商 认证 交互 1.1 版本号协商阶段 (1) 服务端打开22端口(也可以为了 ...
- sudo,linux 新建账号,并开通ssh登录
新建账号需要root账号或sudo权限,sudo配置保存在/etc/sudoers文件. sudoers的配置格式一般为: root ALL=(ALL:ALL) ALL %sudo ALL=(ALL: ...
- 使用ping钥匙临时开启SSH:22端口,实现远程安全SSH登录管理就这么简单
设置防火墙策略时,关于SSH:22访问权限,我们常常会设置服务器只接受某个固定IP(如公司IP)访问,但是当我们出差或在家情况需要登录服务器怎么办呢? 常用两种解决方案:1.通过VPN操作登录主机: ...
- Windows Azure Virtual Machine (25) 使用SSH登录Azure Linux虚拟机
<Windows Azure Platform 系列文章目录> 本文介绍内容适合于Azure Global和Azure China 为什么使用SSH登录Azure Linux虚拟机? 我们 ...
- 终端ssh登录mac用shell打包ipa报错:replacing existing signature
终端ssh登录mac用shell打包ipa报错:replacing existing signature 报错原因:login.keychain被锁定,ssh登录的没有访问权限 解决方法:终端敲入 s ...
随机推荐
- OI图论 简单学习笔记
网络流另开了一个专题,所以在这里就不详细叙述了. 图 一般表示为\(G=(V,E)\),V表示点集,E表示边集 定义图G为简单图,当且仅当图G没有重边和自环. 对于图G=(V,E)和图G2=(V2,E ...
- Three Religions CodeForces - 1149B (字符串,dp)
大意: 给定字符串S, 要求维护三个串, 支持在每个串末尾添加或删除字符, 询问S是否能找到三个不相交的子序列等于三个串. 暴力DP, 若不考虑动态维护的话, 可以直接$O(len^3)$处理出最少需 ...
- 淘宝内部分享:MySQL & MariaDB性能优化 【转】
MySQL· 5.7优化·Metadata Lock子系统的优化 背景 引入MDL锁的目的,最初是为了解决著名的bug#989,在MySQL 5.1及之前的版本,事务执行过程中并不维护涉及到的所有表的 ...
- 百度地图API —— 制作多途经点的线路导航
[百度地图API]如何制作多途经点的线路导航——驾车篇 摘要: 休假结束,酸奶小妹要从重庆驾车去北京.可是途中要去西安奶奶家拿牛奶饼干呢!用百度地图API,能不能帮我实现这个愿望呢? ------ ...
- WeakHashMap源码分析
WeakHashMap是一种弱引用map,内部的key会存储为弱引用, 当jvm gc的时候,如果这些key没有强引用存在的话,会被gc回收掉, 下一次当我们操作map的时候会把对应的Entry整个删 ...
- [CoffeeScript]使用Yield功能
CoffeeScript 1.9 开始提供了类似ES6的yield关键字. 自己结合co和bluebird做了个试验. co -- http://npmjs.org/package/co -- fo ...
- C#集合之位数组
如果需要处理的数字有许多位,就可以使用BitArray类和BitVector32结构.BitArray类位于System.Collection,BitVector32结构位于System.Collec ...
- 使用chart.js時取消懸浮在圖表頂部的'undefined'標識
解決方法:在options中設置legend項中display屬性為false options: { scales: { yAxes: [{ ticks: { beginAtZero: true } ...
- iview2.0 bug之+8 区的 DatePicker
请看以上细节图:工作案例小Demo 用心去做,不留遗憾!
- WorldCount 结对项目
合作者:201631062501,201631062129 代码地址:https://gitee.com/guilinyunya/WorldCount 伙伴博客地址:https://www.cnblo ...