Skip to content

Commit 236cb5a

Browse files
committed
使用Spring Security进行安全控制
1 parent 4de9612 commit 236cb5a

File tree

9 files changed

+251
-0
lines changed

9 files changed

+251
-0
lines changed

Chapter4-3-1/pom.xml

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.didispace</groupId>
7+
<artifactId>Chapter4-3-1</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>Chapter4-3-1</name>
12+
<description>Spring Boot with Thymeleaf</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.3.2.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-test</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-web</artifactId>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-starter-security</artifactId>
50+
</dependency>
51+
52+
</dependencies>
53+
54+
<build>
55+
<plugins>
56+
<plugin>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-maven-plugin</artifactId>
59+
<configuration>
60+
<fork>true</fork>
61+
</configuration>
62+
</plugin>
63+
</plugins>
64+
</build>
65+
66+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.didispace;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
*
8+
* @author 程序猿DD
9+
* @version 1.0.0
10+
* @blog http://blog.didispace.com
11+
*
12+
*/
13+
@SpringBootApplication
14+
public class Application {
15+
16+
public static void main(String[] args) {
17+
18+
SpringApplication.run(Application.class, args);
19+
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.didispace;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
6+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
7+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
8+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
9+
10+
@Configuration
11+
@EnableWebSecurity
12+
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
13+
14+
@Override
15+
protected void configure(HttpSecurity http) throws Exception {
16+
http
17+
.authorizeRequests()
18+
.antMatchers("/", "/home").permitAll()
19+
.anyRequest().authenticated()
20+
.and()
21+
.formLogin()
22+
.loginPage("/login")
23+
.permitAll()
24+
.and()
25+
.logout()
26+
.permitAll();
27+
}
28+
29+
@Autowired
30+
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
31+
auth
32+
.inMemoryAuthentication()
33+
.withUser("user").password("password").roles("USER");
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.didispace.web;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.ui.ModelMap;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
8+
/**
9+
*
10+
* @author 程序猿DD
11+
* @version 1.0.0
12+
* @blog http://blog.didispace.com
13+
*
14+
*/
15+
@Controller
16+
public class HelloController {
17+
18+
@RequestMapping("/")
19+
public String index() {
20+
return "index";
21+
}
22+
23+
@RequestMapping("/hello")
24+
public String hello() {
25+
return "hello";
26+
}
27+
28+
@RequestMapping(value = "/login", method = RequestMethod.GET)
29+
public String login() {
30+
return "login";
31+
}
32+
33+
}

Chapter4-3-1/src/main/resources/application.properties

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
3+
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
4+
<head>
5+
<title>Hello World!</title>
6+
</head>
7+
<body>
8+
<h1 th:inline="text">Hello [[${#httpServletRequest.remoteUser}]]!</h1>
9+
<form th:action="@{/logout}" method="post">
10+
<input type="submit" value="注销"/>
11+
</form>
12+
</body>
13+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
3+
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
4+
<head>
5+
<title>Spring Security入门</title>
6+
</head>
7+
<body>
8+
<h1>欢迎使用Spring Security!</h1>
9+
10+
<p>点击 <a th:href="@{/hello}">这里</a> 打个招呼吧</p>
11+
</body>
12+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml"
3+
xmlns:th="http://www.thymeleaf.org"
4+
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
5+
<head>
6+
<title>Spring Security Example </title>
7+
</head>
8+
<body>
9+
<div th:if="${param.error}">
10+
用户名或密码错
11+
</div>
12+
<div th:if="${param.logout}">
13+
您已注销成功
14+
</div>
15+
<form th:action="@{/login}" method="post">
16+
<div><label> 用户名 : <input type="text" name="username"/> </label></div>
17+
<div><label> 密 码 : <input type="password" name="password"/> </label></div>
18+
<div><input type="submit" value="登录"/></div>
19+
</form>
20+
</body>
21+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.didispace;
2+
3+
import com.didispace.web.HelloController;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.boot.test.SpringApplicationConfiguration;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.mock.web.MockServletContext;
10+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
11+
import org.springframework.test.context.web.WebAppConfiguration;
12+
import org.springframework.test.web.servlet.MockMvc;
13+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
14+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
15+
16+
import static org.hamcrest.Matchers.equalTo;
17+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
18+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
19+
20+
21+
/**
22+
*
23+
* @author 程序猿DD
24+
* @version 1.0.0
25+
* @blog http://blog.didispace.com
26+
*
27+
*/
28+
@RunWith(SpringJUnit4ClassRunner.class)
29+
@SpringApplicationConfiguration(classes = MockServletContext.class)
30+
@WebAppConfiguration
31+
public class ApplicationTests {
32+
33+
private MockMvc mvc;
34+
35+
@Before
36+
public void setUp() throws Exception {
37+
mvc = MockMvcBuilders.standaloneSetup(
38+
new HelloController()).build();
39+
}
40+
41+
@Test
42+
public void getHello() throws Exception {
43+
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
44+
.andExpect(status().isOk())
45+
.andExpect(content().string(equalTo("Hello World")));
46+
}
47+
48+
}

0 commit comments

Comments
 (0)