Skip to content

Commit 7ae9f39

Browse files
lizhen's commit
add the security to validate the user
1 parent 0136723 commit 7ae9f39

File tree

10 files changed

+111
-22
lines changed

10 files changed

+111
-22
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@
6161
<artifactId>spring-boot-starter-thymeleaf</artifactId>
6262
</dependency>
6363

64+
<dependency>
65+
<groupId>org.springframework.boot</groupId>
66+
<artifactId>spring-boot-starter-security</artifactId>
67+
</dependency>
68+
6469
<dependency>
6570
<groupId>org.springframework.boot</groupId>
6671
<artifactId>spring-boot-starter-test</artifactId>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.springboot.demo.config;
2+
3+
import java.io.IOException;
4+
import java.util.List;
5+
6+
import javax.servlet.ServletException;
7+
import javax.servlet.http.HttpServletRequest;
8+
import javax.servlet.http.HttpServletResponse;
9+
import javax.servlet.http.HttpSession;
10+
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.context.annotation.Configuration;
13+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
14+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
15+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
16+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
17+
import org.springframework.security.core.Authentication;
18+
import org.springframework.security.core.AuthenticationException;
19+
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
20+
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
21+
22+
import com.springboot.demo.pojo.primary.FirstUser;
23+
import com.springboot.demo.service.impl.UserService;
24+
25+
@Configuration
26+
@EnableWebSecurity
27+
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
28+
29+
@Autowired
30+
UserService userService;
31+
// 这里设置允许那些路径下的页面需要设置安全验证
32+
@Override
33+
protected void configure(HttpSecurity http) throws Exception {
34+
//如果直接输入登陆网址时,当登陆成功后,默认成功的跳转地址为“/”,现在发现的问题是这个不能将登陆成功后的数据渲染给模板
35+
http
36+
.authorizeRequests().antMatchers("/","/registry").permitAll().anyRequest().authenticated().and()
37+
.formLogin().loginPage("/login").successHandler(new AuthenticationSuccessHandler(){
38+
@Override
39+
public void onAuthenticationSuccess(HttpServletRequest req, HttpServletResponse res,
40+
Authentication auth) throws IOException, ServletException {
41+
HttpSession session = req.getSession();
42+
List<FirstUser> userList = userService.findUserByName(auth.getName());
43+
for(FirstUser user :userList){
44+
//这里将userList中的数据加入到session中,这里加入的只有列表最后一个user
45+
session.setAttribute("user", user);
46+
}
47+
res.sendRedirect("/success");
48+
}
49+
})
50+
.failureHandler(new AuthenticationFailureHandler(){
51+
52+
@Override
53+
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
54+
AuthenticationException exception) throws IOException, ServletException {
55+
// 这里目前登陆失败的的验证我还没有写
56+
57+
}
58+
}).permitAll().and()
59+
.logout().permitAll();
60+
}
61+
62+
@Autowired
63+
protected void configGloable(AuthenticationManagerBuilder auth)throws Exception{
64+
65+
//这里下面我不知道设置的这个用户名和密码好像是固定好的了,理论上应该使用从数据库中查询出来的用户名和密码
66+
//之前设置的一直有问题,加了默认成功页面时,可以跳转!但是每次从系启动应用时,第一次都会出现没有访问权限问题
67+
//这里设置的前提是讲login页面中的username属性的标签的name改为username,这里我猜是这里的user验证时,取出的是前面名字为username的标签
68+
auth.inMemoryAuthentication().withUser("李阵").password("lizhen").roles("USER");
69+
70+
//auth.userDetailsService(this.loginService);
71+
}
72+
}

src/main/java/com/springboot/demo/controller/LoginController.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import org.springframework.ui.Model;
88
import org.springframework.web.bind.annotation.RequestMapping;
99
import org.springframework.web.bind.annotation.RequestMethod;
10-
import org.springframework.web.bind.annotation.ResponseBody;
11-
1210
import com.springboot.demo.pojo.primary.FirstUser;
1311
import com.springboot.demo.service.IUserService;
1412
import com.springboot.demo.service.impl.LoginService;
@@ -23,28 +21,36 @@ public class LoginController {
2321
@Autowired
2422
IUserService userService;
2523

26-
27-
@RequestMapping("/login")
24+
//现在这个数据是经过security模块传过来的
25+
@RequestMapping("/login" )
2826
public String login(){
2927
return"login";
3028
}
3129

30+
@RequestMapping("/success")
31+
public String success(HttpSession session,Model model){
32+
FirstUser user = (FirstUser) session.getAttribute("user");
33+
model.addAttribute("user", user);
34+
return "success";
35+
}
36+
3237
@RequestMapping("/registry")
3338
public String registry(){
3439
return "user/registry";
3540
}
3641

3742

3843
@RequestMapping(value="/registry", method={RequestMethod.POST})
39-
public String registry(Model model,String name, String password, String address, int age){
40-
FirstUser user = new FirstUser(null,name,password,address,age);
44+
public String registry(Model model,String username, String password, String address, int age){
45+
FirstUser user = new FirstUser(null,username,password,address,age);
4146
userService.saveUser(user);
4247
//用户注册后,跳转到登陆成功的界面,这里用户插入后,没有在查询用户操作
4348
model.addAttribute("user", user);
4449
return "success";
4550
}
4651

4752

53+
//使用了权限认证后,好像所有的登陆都不会转发到这里来了
4854
@RequestMapping(value = "/login", method = { RequestMethod.POST })
4955
public String login(Model model, HttpSession session, String name, String password) {
5056
// 这里调用业务层查看是否有用户

src/main/java/com/springboot/demo/controller/UserController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public String update(Model model,Long id){
4848
return "user/update";
4949
}
5050
@RequestMapping(value="/update",method={RequestMethod.POST})
51-
public String update(Model model,Long id,String name, String address, int age){
51+
public String update(Model model,Long id,String username, String address, int age){
5252
FirstUser user = userService.findUserById(id);
53-
user.setName(name);
53+
user.setName(username);
5454
user.setAddress(address);
5555
user.setAge(age);
5656
//执行跟新操作

src/main/java/com/springboot/demo/pojo/primary/FirstUser.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.springboot.demo.pojo.primary;
22

3-
import java.util.Arrays;
4-
import java.util.Collection;
5-
63
import javax.persistence.Entity;
74
import javax.persistence.GeneratedValue;
85
import javax.persistence.Id;

src/main/resources/templates/index.html

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,19 @@
22
<html xmlns:th="http://www.thymeleaf.org">
33
<head>
44
<title>欢迎界面</title>
5+
<style type="text/css">
6+
#index{
7+
text-align:center;
8+
}
9+
10+
#message{
11+
text-align:center;
12+
}
13+
14+
</style>
515
</head>
616
<body>
7-
<h1>欢迎来到用户管理</h1>
8-
<p>点击<a th:href="@{/login}">这里</a>进入我的信息</p>
17+
<h1 id="index">欢迎来到用户管理</h1>
18+
<p id="message">点击<a th:href="@{/success}">这里</a>进入个人信息</p>
919
</body>
1020
</html>

src/main/resources/templates/login.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<table id="table" style='margin:0px auto'>
2323
<tr>
2424
<td>用户名:</td>
25-
<td><input type="text" name="name" /></td>
25+
<td><input type="text" name="username" /></td>
2626
</tr>
2727
<tr>
2828
<td>密 码:</td>

src/main/resources/templates/success.html

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@
1717
</style>
1818

1919
<script type="text/javascript">
20-
21-
function findAllUser(){
22-
location.href = "/findAllUser";
23-
}
20+
function findAllUser() {
21+
location.href = "/findAllUser";
22+
}
2423
</script>
2524
</head>
2625
<body>
27-
2826
<p th:text="'欢迎'+ ${user.name} +'管理员'" th:if="${user.name eq '李阵'}"></p>
2927
<p th:text="'欢迎' + ${user.name}" th:unless="${user.name eq '李阵'}"></p>
30-
<input type="button" value="所有用户" onclick="findAllUser()" th:if="${user.name eq '李阵'}" />
28+
<input type="button" value="所有用户" onclick="findAllUser()"
29+
th:if="${user.name eq '李阵'}" />
3130
<p id="info">您的个人信息</p>
3231
<div>
3332
<table id="table">

src/main/resources/templates/user/registry.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<table id="table" style='margin:0px auto'>
4141
<tr>
4242
<td>用户名:</td>
43-
<td><input type="text" name="name" id="userName"
43+
<td><input type="text" name="username" id="userName"
4444
onchange="validate()"/></td>
4545
<td id="star">*</td>
4646
</tr>

src/main/resources/templates/user/update.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<table id="table">
1111
<tr>
1212
<td>姓名:</td>
13-
<td><input type="text" name="name" th:value="${user.name}" /></td>
13+
<td><input type="text" name="username" th:value="${user.name}" /></td>
1414
</tr>
1515
<tr>
1616
<td>地址:</td>

0 commit comments

Comments
 (0)