Skip to content

Commit

Permalink
validate user login
Browse files Browse the repository at this point in the history
  • Loading branch information
leelance committed Jun 11, 2014
1 parent 3b7c4e3 commit 6aec897
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 16 deletions.
3 changes: 2 additions & 1 deletion spring-boot-samples/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,5 @@ local.properties
# Locally stored "Eclipse launch configurations"
*.launch
.DS_Store
.log
.log
/target
28 changes: 19 additions & 9 deletions spring-boot-samples/src/main/java/com/lance/entity/UserEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
@Table(name="user")
public class UserEntity extends BaseEntity {
//用户名
private String userName;
private String name;

private String email;

//性别 0: 女 1: 男 2:其他
private int sex;
Expand All @@ -26,14 +28,6 @@ public class UserEntity extends BaseEntity {
@OneToMany(mappedBy="user")
private List<AddressEntity> addresses = new LinkedList<AddressEntity>();

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public int getSex() {
return sex;
}
Expand Down Expand Up @@ -64,5 +58,21 @@ public List<AddressEntity> getAddresses() {

public void setAddresses(List<AddressEntity> addresses) {
this.addresses = addresses;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.lance.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.lance.entity.UserEntity;

public interface UserRepository extends JpaRepository<UserEntity, Long>{

/**
* 根据userName查询
* @author lance
* 2014-6-11下午11:30:31
* @param userName
* @return
*/
UserEntity findByEmail(String email);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.lance.service;

import com.lance.entity.UserEntity;

public interface LoginService {

/**
* 用户登录
* @author lance
* 2014-6-11下午11:26:05
* @param user
* @return
*/
UserEntity login(UserEntity user);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.lance.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lance.entity.UserEntity;
import com.lance.repository.UserRepository;
import com.lance.utils.EncryptUtils;
import com.lance.utils.ServiceException;

/**
* 登录信息
* @author lance
*/
@Service
public class LoginServiceImpl implements LoginService {
@Autowired
private UserRepository userRepository;
/**
* 用户登录
* @author lance
* 2014-6-11下午11:26:05
* @param user
* @return
*/
public UserEntity login(UserEntity user) {
if(StringUtils.isBlank(user.getEmail())) {
throw new ServiceException("用户名不能为空");
}

if(StringUtils.isBlank(user.getPassword())) {
throw new ServiceException("密码不能为空");
}

UserEntity userEntity = userRepository.findByEmail(user.getEmail());
if(null == userEntity){
throw new ServiceException("用户名不存在");
}

String password = EncryptUtils.encryptMD5(user.getPassword());
if(!StringUtils.equals(password, userEntity.getPassword())){
throw new ServiceException("密码输入错误");
}

return userEntity;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.lance.utils;

/**
* 处理service抛出运行时异常处理
* @author lance
*/
public class ServiceException extends RuntimeException {
private static final long serialVersionUID = 1389958090308317369L;

public ServiceException() {
super();
}

public ServiceException(String msg, Throwable clause) {
super(msg, clause);
}

public ServiceException(String msg) {
super(msg);
}

public ServiceException(Throwable clause) {
super(clause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lance.entity.UserEntity;
import com.lance.service.LoginService;
import com.lance.utils.ServiceException;

/**
*
Expand All @@ -17,8 +22,11 @@
*/
@Controller
public class IndexController {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private HttpSession session;
@Autowired
private LoginService loginService;

/**
* 跳转登录页面
Expand All @@ -40,7 +48,16 @@ public String login(){
* @return
*/
@RequestMapping(value="login",method=RequestMethod.POST)
public String login(UserEntity user){
public String login(UserEntity user, RedirectAttributes redirect){
try {
user = loginService.login(user);
} catch (ServiceException e) {
logger.debug(e.getMessage());
redirect.addFlashAttribute("err_code", e.getMessage());
redirect.addFlashAttribute("user", user);
return "redirect:/login";
}

session.setAttribute("cur_user", user);
return "redirect:user/home";
}
Expand Down
6 changes: 5 additions & 1 deletion spring-boot-samples/src/main/resources/static/css/login.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@CHARSET "UTF-8";
body {
padding-top: 170px;
padding-top: 150px;
padding-bottom: 40px;
background-color: #eee;
}
Expand Down Expand Up @@ -35,4 +35,8 @@ body {
margin-bottom: 10px;
border-top-left-radius: 0;
border-top-right-radius: 0;
}
.error-code{
color: red;
margin: 5px 0;
}
5 changes: 3 additions & 2 deletions spring-boot-samples/src/main/webapp/WEB-INF/views/login.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
<div class="container">
<form class="form-signin" action="login" method="post">
<h2 class="form-signin-heading">Please sign in</h2>
<input type="text" class="form-control" placeholder="Email address" required autofocus name="userName">
<input type="password" class="form-control" placeholder="Password" required name="password">
<div class="error-code">${err_code}</div>
<input type="text" class="form-control" placeholder="Email address" required autofocus name="email" value="${user.email}">
<input type="password" class="form-control" placeholder="Password" required name="password" value="${user.password}">
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>Insert title here</title>
</head>
<body>

This is a list page.
</body>
</html>
6 changes: 5 additions & 1 deletion spring-boot-samples/target/classes/static/css/login.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@CHARSET "UTF-8";
body {
padding-top: 170px;
padding-top: 150px;
padding-bottom: 40px;
background-color: #eee;
}
Expand Down Expand Up @@ -35,4 +35,8 @@ body {
margin-bottom: 10px;
border-top-left-radius: 0;
border-top-right-radius: 0;
}
.error-code{
color: red;
margin: 5px 0;
}

0 comments on commit 6aec897

Please sign in to comment.