Skip to content

Commit b4cd29e

Browse files
committed
拦截器登录
1 parent f1424e6 commit b4cd29e

File tree

7 files changed

+160
-30
lines changed

7 files changed

+160
-30
lines changed

core/src/main/java/info/xiaomo/core/untils/RandomUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,6 @@ public static String createSalt() {
139139
public static void main(String[] args) {
140140
String salt = createSalt();
141141
System.out.println(salt);
142-
System.out.println(MD5Util.encode("123456",salt));
142+
System.out.println(MD5Util.encode("xiaomo",salt));
143143
}
144144
}

website/src/main/java/info/xiaomo/website/XiaomoMain.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package info.xiaomo.website;
22

3+
import info.xiaomo.website.interceptor.LoginInterceptor;
34
import org.springframework.boot.SpringApplication;
45
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
56
import org.springframework.boot.autoconfigure.domain.EntityScan;
@@ -9,8 +10,8 @@
910
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
1011
import org.springframework.stereotype.Controller;
1112
import org.springframework.transaction.annotation.EnableTransactionManagement;
12-
import org.springframework.web.bind.annotation.RequestMapping;
13-
import org.springframework.web.bind.annotation.RequestMethod;
13+
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
14+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
1415

1516
/**
1617
* 把今天最好的表现当作明天最新的起点..~
@@ -34,15 +35,18 @@
3435
@EnableJpaRepositories("info.xiaomo.*.dao")
3536
@EnableCaching
3637
@Controller
37-
public class XiaomoMain {
38+
public class XiaomoMain extends WebMvcConfigurerAdapter {
3839

3940
public static void main(String[] args) throws Exception {
4041
SpringApplication.run(XiaomoMain.class, args);
4142
}
4243

43-
@RequestMapping(value = "/web", method = RequestMethod.GET)
44-
public String admin() {
45-
return "web/index";
44+
/**
45+
* 配置拦截器
46+
*/
47+
public void addInterceptors(InterceptorRegistry registry) {
48+
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/", "/web/**");
49+
super.addInterceptors(registry);
4650
}
4751

4852
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package info.xiaomo.website.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RequestMethod;
6+
7+
/**
8+
* 把今天最好的表现当作明天最新的起点..~
9+
* いま 最高の表現 として 明日最新の始発..~
10+
* Today the best performance as tomorrow newest starter!
11+
* Created by IntelliJ IDEA.
12+
*
13+
* @author: xiaomo
14+
* @github: https://github.com/qq83387856
15+
* @email: hupengbest@163.com
16+
* @QQ_NO: 83387856
17+
* @Date: 2016/11/21 10:53
18+
* @Copyright(©) 2015 by xiaomo.
19+
**/
20+
21+
@Controller
22+
public class IndexController {
23+
24+
@RequestMapping(value = "/", method = RequestMethod.GET)
25+
public String admin() {
26+
return "web/index";
27+
}
28+
}

website/src/main/java/info/xiaomo/website/controller/UserController.java

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
import info.xiaomo.core.untils.RandomUtil;
1212
import info.xiaomo.website.model.UserModel;
1313
import info.xiaomo.website.service.UserService;
14+
import info.xiaomo.website.view.UserView;
1415
import org.hibernate.service.spi.ServiceException;
1516
import org.springframework.beans.factory.annotation.Autowired;
1617
import org.springframework.stereotype.Controller;
17-
import org.springframework.web.bind.annotation.PathVariable;
18-
import org.springframework.web.bind.annotation.RequestBody;
19-
import org.springframework.web.bind.annotation.RequestMapping;
20-
import org.springframework.web.bind.annotation.RequestMethod;
18+
import org.springframework.ui.ModelMap;
19+
import org.springframework.web.bind.annotation.*;
2120

21+
import javax.servlet.http.HttpSession;
2222
import java.text.ParseException;
2323
import java.util.List;
2424

@@ -47,6 +47,37 @@ public UserController(UserService service) {
4747
this.service = service;
4848
}
4949

50+
51+
@RequestMapping(value = "toLogin",method = RequestMethod.GET)
52+
public String login() {
53+
return UserView.LOGIN.getName();
54+
}
55+
56+
/**
57+
* 登录
58+
*
59+
* @return result
60+
*/
61+
@RequestMapping(value = "login", method = RequestMethod.POST)
62+
public String login(@RequestParam String email,
63+
@RequestParam String password,
64+
HttpSession session,
65+
ModelMap map) {
66+
UserModel userModel = service.findUserByEmail(email);
67+
//找不到用户
68+
if (userModel == null) {
69+
map.put("errMsg","找不到用户");
70+
return UserView.LOGIN.getName();
71+
}
72+
//密码不正确
73+
if (!MD5Util.encode(password, userModel.getSalt()).equals(userModel.getPassword())) {
74+
map.put("errMsg","密码不正确");
75+
return UserView.LOGIN.getName();
76+
}
77+
session.setAttribute("currentUser", userModel);
78+
return UserView.INDEX.getName();
79+
}
80+
5081
/**
5182
* 根据id 查找用户
5283
*
@@ -97,24 +128,6 @@ public Result register(@PathVariable("email") String email, @PathVariable("passw
97128
}
98129

99130

100-
/**
101-
* 登录
102-
*
103-
* @return result
104-
*/
105-
@RequestMapping(value = "login/{email}/{password}", method = RequestMethod.POST)
106-
public Result login(@PathVariable("email") String email, @PathVariable("password") String password) {
107-
UserModel userModel = service.findUserByEmail(email);
108-
//找不到用户
109-
if (userModel == null) {
110-
return new Result(Err.USER_NOT_FOUND.getCode(), Err.USER_NOT_FOUND.getMessage());
111-
}
112-
//密码不正确
113-
if (!MD5Util.encode(password, userModel.getSalt()).equals(userModel.getPassword())) {
114-
return new Result(Err.AUTH_FAILED.getCode(), Err.AUTH_FAILED.getMessage());
115-
}
116-
return new Result(userModel);
117-
}
118131

119132

120133
/**
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package info.xiaomo.website.interceptor;
2+
3+
import info.xiaomo.website.model.UserModel;
4+
import org.springframework.web.servlet.HandlerInterceptor;
5+
import org.springframework.web.servlet.ModelAndView;
6+
7+
import javax.servlet.http.HttpServletRequest;
8+
import javax.servlet.http.HttpServletResponse;
9+
10+
/**
11+
* 把今天最好的表现当作明天最新的起点..~
12+
* いま 最高の表現 として 明日最新の始発..~
13+
* Today the best performance as tomorrow newest starter!
14+
* Created by IntelliJ IDEA.
15+
*
16+
* @author: xiaomo
17+
* @github: https://github.com/qq83387856
18+
* @email: hupengbest@163.com
19+
* @QQ_NO: 83387856
20+
* @Date: 2016/11/21 10:42
21+
* @Copyright(©) 2015 by xiaomo.
22+
**/
23+
24+
public class LoginInterceptor implements HandlerInterceptor {
25+
@Override
26+
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
27+
UserModel user = (UserModel) httpServletRequest.getSession().getAttribute("currentUser");
28+
if (user == null) {
29+
//用户没有登录
30+
httpServletResponse.sendRedirect(httpServletRequest.getContextPath() + "/user/toLogin");
31+
return false;
32+
}
33+
//用户已经登录
34+
return true;
35+
36+
}
37+
38+
@Override
39+
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
40+
41+
}
42+
43+
@Override
44+
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
45+
46+
}
47+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package info.xiaomo.website.view;
2+
3+
/**
4+
* 把今天最好的表现当作明天最新的起点..~
5+
* いま 最高の表現 として 明日最新の始発..~
6+
* Today the best performance as tomorrow newest starter!
7+
* Created by IntelliJ IDEA.
8+
*
9+
* @author: xiaomo
10+
* @github: https://github.com/qq83387856
11+
* @email: hupengbest@163.com
12+
* @QQ_NO: 83387856
13+
* @Date: 2016/11/21 11:12
14+
* @Copyright(©) 2015 by xiaomo.
15+
**/
16+
17+
public enum UserView {
18+
19+
LOGIN("login"),
20+
INDEX("/web/index");
21+
22+
private String name;
23+
24+
UserView(String name) {
25+
this.name = name;
26+
}
27+
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public void setName(String name) {
33+
this.name = name;
34+
}
35+
}

website/src/main/resources/templates/login.vm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
<title>login</title>
99
</head>
1010
<body>
11-
<form action="/login" method="post">
11+
<form action="/user/login" method="post">
1212
<div><label> 用户名 : <input type="text" name="email"/> </label></div>
1313
<div><label> 密 码 : <input type="password" name="password"/> </label></div>
14+
#if($errMsg)
15+
<h1>${errMsg}</h1>
16+
#end
1417
<div><input type="submit" value="登录"/></div>
1518
</form>
1619
</body>

0 commit comments

Comments
 (0)