开发工具: intellJ idea
框架版本:spring4.3,hibernate4.3
新建maven项目,项目结构如下图:
文章图片
直接上代码:
User.java:
package com.ssh.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;
/**
* Created by forwor on 2018/8/27.
*/
@Entity
@Table(name="T_USER")
public class User {@Id
@GeneratedValue(generator="id")
@GenericGenerator(name = "id",strategy="identity")
private Integer id;
@Column(name="user_name")
private String userName;
@Column(name="password")
private String password;
@Column(name="age")
private int age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}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;
}public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
==================================================================================================
IUserDao.java:
package com.ssh.entity.dao;
import com.ssh.entity.User;
/**
* Created by forwor on 2018/8/27.
*/
public interface IUserDao {
public User getUserInfoByName(String name);
}
【Java|spring4+hibernate4+springmvc+nginx+redis实现session共享】==================================================================================================
UserDao.java:
package com.ssh.entity.dao.impl;
import com.ssh.entity.User;
import com.ssh.entity.dao.IUserDao;
import com.util.base.BaseDao;
import org.springframework.stereotype.Repository;
import org.hibernate.Query;
import org.springframework.transaction.annotation.Propagation;
/**
* Created by forwor on 2018/8/27.
*/
@Repository("userDao")
public class UserDao extends BaseDao implements IUserDao {
public User getUserInfoByName(String name) {
String hql = "from User where userName = '"+name+"'";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
return (User) query.uniqueResult();
}
}
=================================================================================================
BaseDao.java
package com.util.base;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import java.io.Serializable;
/**
* Created by forwor on 2018/8/27.
*/
public class BaseDao {
@Autowired
protected SessionFactory sessionFactory;
public Session getSession(){
return sessionFactory.openSession();
}
}
================================================================================================
LoginController.java:
package com.ssh.loginmana.action;
import com.ssh.entity.User;
import com.ssh.loginmana.ser.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.UUID;
/**
* Created by forwor on 2018/8/27.
*/
@Controller
public class LoginController {
@Autowired
private ILoginService loginService;
// Log log = LogFactory.getLog(this.getClass());
@RequestMapping(value="https://www.it610.com/login", method= RequestMethod.POST)
public String login(@ModelAttribute User user, Model model, HttpSession session, HttpServletRequest request) {
if(user.getUserName() == null || user.getPassword() == null) {
return "failure";
}
System.out.println("---------------"+user.getUserName());
User loginUser = loginService.getUserInfoByName(user.getUserName());
if (loginUser == null) {
model.addAttribute("error", true);
model.addAttribute("message", "用户不存在。");
return "failure";
}if(!user.getPassword().equals(loginUser.getPassword())) {
model.addAttribute("error", true);
model.addAttribute("message", "用户名或密码错误。");
return "failure";
}//request.getSession().setAttribute("USER", user);
String token = UUID.randomUUID().toString();
System.out.println(token);
session.setAttribute("id", token);
session.setAttribute("login", true);
session.setAttribute("token", token);
session.setAttribute("loginDate",new Date());
session.setAttribute(user.getUserName(), token);
return "login_success";
}@RequestMapping(value="https://www.it610.com/login",method=RequestMethod.GET)
public String login(Model model) throws Exception{
model.addAttribute("", new User());
return "login";
}
}
===============================================================================================
ILoginService.java:
package com.ssh.loginmana.ser;
import com.ssh.entity.User;
/**
* Created by forwor on 2018/8/27.
*/
public interface ILoginService {
public User getUserInfoByName(String name);
}
===============================================================================================
LoginService.java:
package com.ssh.loginmana.ser.impl;
import com.ssh.entity.User;
import com.ssh.entity.dao.IUserDao;
import com.ssh.loginmana.ser.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by forwor on 2018/8/27.
*/
@Service("loginService")
public class LoginService implements ILoginService {
@Autowired
private IUserDao userDao;
public User getUserInfoByName(String name){
return userDao.getUserInfoByName(name);
}
}
==================================================================================================
login.jsp
登陆 - 锐客网
================================================================================================
login-success.jsp
登陆成功 - 锐客网
登陆成功,当前位于Tomcat7页面
SessionID:
SessionIP:
SessionPort:
LoginTime:
=================================================================================================
web.xml:
jsp/login.jsp
org.springframework.web.util.Log4jConfigListener
log4jConfigLocationclasspath:log4j.properties
contextConfigLocationclasspath:applicationContext.xml
spring
org.springframework.web.servlet.DispatcherServlet
contextConfigLocationclasspath:spring-mvc.xml
1 spring
*.fw
org.springframework.web.context.ContextLoaderListener
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encodingUTF-8
forceEncodingtrue
encodingFilter
/*
springSessionRepositoryFilter
org.springframework.web.filter.DelegatingFilterProxy
springSessionRepositoryFilter
/*
Scutiry Filter
com.ssh.sysmana.SecuFilter
Scutiry Filter
*.fw
SpringOpenSessionInViewFilter
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
SpringOpenSessionInViewFilter
/*
================================================================================================
依赖包: pom.xml:
4.0.0 com.forwor
ssh_nginx_redis_session
1.0-SNAPSHOT 4.3.1.RELEASE
4.3.5.Final
1.2
1.1.2
3.0-alpha-1
1.9.8
2.5.0
5.1.38
0.9.1.2
1.2.17
1.3.1
1.16.10
junit
junit
3.8.1
test
org.springframework
spring-core
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-expression
${spring.version}
org.springframework
spring-jms
3.1.1.RELEASE
org.springframework
spring-orm
${spring.version}
org.springframework
spring-test
${spring.version}
org.hibernate
hibernate-core
${hibernateVersion}
org.hibernate
hibernate-ehcache
${hibernateVersion}
jstl
jstl
${jstlVersion}
taglibs
standard
${taglibVersion}
javax.servlet
servlet-api
${servletVersion}
provided
mysql
mysql-connector-java
${mysqlVersion}
c3p0
c3p0
${c3p0Version}
com.mchange
c3p0
0.9.5.2
org.codehaus.jackson
jackson-mapper-asl
${jsonVersion}
jar
compile
com.fasterxml.jackson.core
jackson-core
${jacksonVersion}
com.fasterxml.jackson.core
jackson-annotations
${jacksonVersion}
com.fasterxml.jackson.core
jackson-databind
${jacksonVersion}
log4j
log4j
${log4jVersion}
commons-fileupload
commons-fileupload
${fileuploadVersion}
org.projectlombok
lombok
${lombokVersion}
provided
org.springframework.session
spring-session-data-redis
1.3.1.RELEASE
pom
redis.clients
jedis
2.7.2
log4j
log4j
1.2.17
junit
junit
4.12
demo下载路径:https://download.csdn.net/download/qingyou193301/10631715
推荐阅读
- Java|Java基础——数组
- 人工智能|干货!人体姿态估计与运动预测
- java简介|Java是什么(Java能用来干什么?)
- Java|规范的打印日志
- Linux|109 个实用 shell 脚本
- 程序员|【高级Java架构师系统学习】毕业一年萌新的Java大厂面经,最新整理
- Spring注解驱动第十讲--@Autowired使用
- =======j2ee|spring用注解实现注入的@resource,@autowired,@inject区别
- SqlServer|sql server的UPDLOCK、HOLDLOCK试验
- jvm|【JVM】JVM08(java内存模型解析[JMM])