Skip to content

Commit 82d9174

Browse files
committed
springboot-19-security(完)
1 parent 8ea4459 commit 82d9174

File tree

18 files changed

+463
-0
lines changed

18 files changed

+463
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# ***SpringBoot-Security***
2+
3+
* **[SpringSecurity官方文档](https://docs.spring.io/spring-security/site/docs/5.1.3.RELEASE/reference/htmlsingle/)**
4+
* **[SpringSecurity官方简单案例](https://docs.spring.io/spring-security/site/docs/current/guides/html5//)**
5+
## **引入Security依赖**
6+
```java
7+
<dependencies>
8+
<!-- Security -->
9+
<dependency>
10+
<groupId>org.springframework.boot</groupId>
11+
<artifactId>spring-boot-starter-security</artifactId>
12+
</dependency>
13+
14+
<!-- thymeleaf-extras-springsecurity5 -->
15+
<dependency>
16+
<groupId>org.thymeleaf.extras</groupId>
17+
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
18+
<version>3.0.4.RELEASE</version>
19+
</dependency>
20+
21+
<!-- 引入thymeleaf依赖 -->
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
25+
</dependency>
26+
27+
<!-- 引入layout组件依赖 -->
28+
<dependency>
29+
<groupId>nz.net.ultraq.thymeleaf</groupId>
30+
<artifactId>thymeleaf-layout-dialect</artifactId>
31+
</dependency>
32+
</dependencies>
33+
```
34+
35+
## **编写SpringSecurity的配置类**
36+
```java
37+
@EnableWebSecurity
38+
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
39+
40+
//控制请求的访问权限
41+
@Override
42+
protected void configure(HttpSecurity http) throws Exception {
43+
44+
//定义授权规则
45+
http.authorizeRequests()
46+
//对根目录下的所有访问放行
47+
.antMatchers("/").permitAll()
48+
//访问level1下的页面需要VIP1级别
49+
.antMatchers("/level1/**").hasRole("VIP1")
50+
.antMatchers("/level2/**").hasRole("VIP2")
51+
.antMatchers("/level3/**").hasRole("VIP3");
52+
53+
/**
54+
* 开启登录功能,如果没有权限就会来到登录页面
55+
* formLogin的功能
56+
* 1. /login来到登录页面
57+
* 2. 重定向到login?error表示登录失败
58+
* 3. 默认post形式的/login代表处理登录
59+
* 4. 一旦定制loginPage,那么loginPage的post请求就是登录
60+
*/
61+
http.formLogin()
62+
.usernameParameter("user")
63+
.passwordParameter("password")
64+
.loginPage("/userlogin");
65+
66+
/**
67+
* 开启自动配置的注销功能,注销成功以后来到welcome.html
68+
* 访问/logout表示用户注销,清空session
69+
*/
70+
http.logout().logoutSuccessUrl("/");
71+
72+
/**
73+
* 开启记住我功能
74+
* 登录成功后,将cookie发给浏览器保存,再次进入网页,只要通过检查cookie就可以免登陆
75+
* 点击注销,删除cookie
76+
*
77+
*/
78+
http.rememberMe().rememberMeParameter("rememberMe");
79+
}
80+
81+
//定义认证规则
82+
@Override
83+
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
84+
//从内存中获取,springSecurity 5.0 的加密方式是{id}………… id为加密方式
85+
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
86+
.withUser("zhang")
87+
.password(new BCryptPasswordEncoder().encode("123456")).roles("VIP1","VIP2")
88+
.and()
89+
.withUser("li")
90+
.password(new BCryptPasswordEncoder().encode("123456")).roles("VIP2","VIP3")
91+
.and()
92+
.withUser("wang")
93+
.password(new BCryptPasswordEncoder().encode("123456")).roles("VIP1","VIP3");
94+
}
95+
}
96+
```

springboot-19-security/pom.xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>SpringBootLearn</artifactId>
7+
<groupId>com.clown</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<artifactId>springboot-19-security</artifactId>
12+
13+
<dependencies>
14+
15+
<!-- Security -->
16+
<dependency>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-security</artifactId>
19+
</dependency>
20+
21+
<!-- thymeleaf-extras-springsecurity5 -->
22+
<dependency>
23+
<groupId>org.thymeleaf.extras</groupId>
24+
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
25+
<version>3.0.4.RELEASE</version>
26+
</dependency>
27+
28+
<!-- 引入thymeleaf依赖 -->
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
32+
</dependency>
33+
34+
<!-- 引入layout组件依赖 -->
35+
<dependency>
36+
<groupId>nz.net.ultraq.thymeleaf</groupId>
37+
<artifactId>thymeleaf-layout-dialect</artifactId>
38+
</dependency>
39+
</dependencies>
40+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.clown.security;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* @author: Richard·Ackerman
8+
* @create: 2019/1/24
9+
**/
10+
@SpringBootApplication
11+
public class SecurityApplication {
12+
public static void main(String[] args){
13+
SpringApplication.run(SecurityApplication.class);
14+
}
15+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.clown.security.config;
2+
3+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
4+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
5+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
6+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
7+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
8+
9+
/**
10+
* @author: Richard·Ackerman
11+
* @create: 2019/1/24
12+
**/
13+
@EnableWebSecurity
14+
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
15+
16+
//控制请求的访问权限
17+
@Override
18+
protected void configure(HttpSecurity http) throws Exception {
19+
20+
//定义授权规则
21+
http.authorizeRequests()
22+
//对根目录下的所有访问放行
23+
.antMatchers("/").permitAll()
24+
//访问level1下的页面需要VIP1级别
25+
.antMatchers("/level1/**").hasRole("VIP1")
26+
.antMatchers("/level2/**").hasRole("VIP2")
27+
.antMatchers("/level3/**").hasRole("VIP3");
28+
29+
/**
30+
* 开启登录功能,如果没有权限就会来到登录页面
31+
* formLogin的功能
32+
* 1. /login来到登录页面
33+
* 2. 重定向到login?error表示登录失败
34+
* 3. 默认post形式的/login代表处理登录
35+
* 4. 一旦定制loginPage,那么loginPage的post请求就是登录
36+
*/
37+
http.formLogin()
38+
.usernameParameter("user")
39+
.passwordParameter("password")
40+
.loginPage("/userlogin");
41+
42+
/**
43+
* 开启自动配置的注销功能,注销成功以后来到welcome.html
44+
* 访问/logout表示用户注销,清空session
45+
*/
46+
http.logout().logoutSuccessUrl("/");
47+
48+
/**
49+
* 开启记住我功能
50+
* 登录成功后,将cookie发给浏览器保存,再次进入网页,只要通过检查cookie就可以免登陆
51+
* 点击注销,删除cookie
52+
*
53+
*/
54+
http.rememberMe().rememberMeParameter("rememberMe");
55+
}
56+
57+
//定义认证规则
58+
@Override
59+
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
60+
//从内存中获取,springSecurity 5.0 的加密方式是{id}………… id为加密方式
61+
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
62+
.withUser("zhang")
63+
.password(new BCryptPasswordEncoder().encode("123456")).roles("VIP1","VIP2")
64+
.and()
65+
.withUser("li")
66+
.password(new BCryptPasswordEncoder().encode("123456")).roles("VIP2","VIP3")
67+
.and()
68+
.withUser("wang")
69+
.password(new BCryptPasswordEncoder().encode("123456")).roles("VIP1","VIP3");
70+
}
71+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.clown.security.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
7+
@Controller
8+
public class KungFuController {
9+
10+
private final String PREFIX = "pages/";
11+
/**
12+
* 欢迎页
13+
* @return
14+
*/
15+
@GetMapping("/")
16+
public String index() {
17+
return "welcome";
18+
}
19+
20+
/**
21+
* 登陆页
22+
* @return
23+
*/
24+
@GetMapping("/userlogin")
25+
public String loginPage() {
26+
return PREFIX+"login";
27+
}
28+
29+
30+
/**
31+
* level1页面映射
32+
* @param path
33+
* @return
34+
*/
35+
@GetMapping("/level1/{path}")
36+
public String level1(@PathVariable("path")String path) {
37+
return PREFIX+"level1/"+path;
38+
}
39+
40+
/**
41+
* level2页面映射
42+
* @param path
43+
* @return
44+
*/
45+
@GetMapping("/level2/{path}")
46+
public String level2(@PathVariable("path")String path) {
47+
return PREFIX+"level2/"+path;
48+
}
49+
50+
/**
51+
* level3页面映射
52+
* @param path
53+
* @return
54+
*/
55+
@GetMapping("/level3/{path}")
56+
public String level3(@PathVariable("path")String path) {
57+
return PREFIX+"level3/"+path;
58+
}
59+
60+
61+
}

springboot-19-security/src/main/resources/application.yml

Whitespace-only changes.
355 KB
Binary file not shown.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5+
<title>Insert title here</title>
6+
</head>
7+
<body>
8+
<a th:href="@{/}">返回</a>
9+
<h1>罗汉拳</h1>
10+
<p>罗汉拳站当央,打起来不要慌</p>
11+
</body>
12+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5+
<title>Insert title here</title>
6+
</head>
7+
<body>
8+
<a th:href="@{/}">返回</a>
9+
<h1>武当长拳</h1>
10+
<p>长一点在长一点</p>
11+
</body>
12+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5+
<title>Insert title here</title>
6+
</head>
7+
<body>
8+
<a th:href="@{/}">返回</a>
9+
<h1>全真剑法</h1>
10+
<p>全都是真的</p>
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)