Skip to content

Commit d482a44

Browse files
committed
liuyueyi#79 spring security demo
1 parent 01744bd commit d482a44

File tree

11 files changed

+277
-0
lines changed

11 files changed

+277
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<module>spring-boot</module>
1212
<module>spring-case</module>
1313
<module>spring</module>
14+
<module>spring-security</module>
1415
</modules>
1516
<packaging>pom</packaging>
1617

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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>spring-security</artifactId>
7+
<groupId>com.git.hui.boot</groupId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>000-basic-demo</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-security</artifactId>
18+
</dependency>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-web</artifactId>
22+
</dependency>
23+
</dependencies>
24+
25+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.git.hui.boot.security;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* Created by @author yihui in 20:29 19/12/22.
8+
*/
9+
@SpringBootApplication
10+
public class Application {
11+
12+
public static void main(String[] args) {
13+
SpringApplication.run(Application.class, args);
14+
}
15+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.git.hui.boot.security.rest;
2+
3+
import org.springframework.security.core.context.SecurityContextHolder;
4+
import org.springframework.security.core.userdetails.UserDetails;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
import org.springframework.web.context.request.RequestContextHolder;
8+
import org.springframework.web.context.request.ServletRequestAttributes;
9+
10+
/**
11+
* Created by @author yihui in 20:30 19/12/22.
12+
*/
13+
@RestController
14+
public class IndexRest {
15+
16+
public String getUser() {
17+
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getRemoteUser();
18+
}
19+
20+
public String getUserV2() {
21+
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
22+
23+
String userName;
24+
if (principal instanceof UserDetails) {
25+
userName = ((UserDetails) principal).getUsername();
26+
} else {
27+
userName = principal.toString();
28+
}
29+
return userName;
30+
}
31+
32+
@GetMapping(path = {"/", "/index"})
33+
public String index() {
34+
return "hello this is index! welcome " + getUser();
35+
}
36+
37+
@GetMapping(path = "hello")
38+
public String hello(String name) {
39+
return getUserV2() + " welcome " + name;
40+
}
41+
42+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
spring:
2+
security:
3+
user:
4+
name: yihuihui
5+
password: 123456
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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>spring-security</artifactId>
7+
<groupId>com.git.hui.boot</groupId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>001-authentication-mem</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-web</artifactId>
18+
</dependency>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-security</artifactId>
22+
</dependency>
23+
</dependencies>
24+
25+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.git.hui.boot.security;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
6+
7+
/**
8+
* - prePostEnabled :决定Spring Security的前注解是否可用 [@PreAuthorize,@PostAuthorize,..]
9+
* - secureEnabled : 决定是否Spring Security的保障注解 [@Secured] 是否可用。
10+
* - jsr250Enabled :决定 JSR-250 annotations 注解[@RolesAllowed..] 是否可用。
11+
*
12+
* Created by @author yihui in 14:54 19/12/23.
13+
*/
14+
@EnableGlobalMethodSecurity(prePostEnabled=true)
15+
@SpringBootApplication
16+
public class Application {
17+
18+
public static void main(String[] args) {
19+
SpringApplication.run(Application.class, args);
20+
}
21+
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.git.hui.boot.security;
2+
3+
import org.springframework.context.annotation.Configuration;
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+
8+
/**
9+
* Created by @author yihui in 15:41 19/12/23.
10+
*/
11+
@Configuration
12+
@EnableWebSecurity
13+
public class SecurityAdapterConfig extends WebSecurityConfigurerAdapter {
14+
/**
15+
* 指定首页所有人可访问;其他url需要
16+
*
17+
* @param http
18+
* @throws Exception
19+
*/
20+
@Override
21+
protected void configure(HttpSecurity http) throws Exception {
22+
http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/index").permitAll().anyRequest()
23+
.authenticated().and().formLogin().and().httpBasic();
24+
}
25+
26+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.git.hui.boot.security;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.security.core.userdetails.User;
6+
import org.springframework.security.core.userdetails.UserDetailsService;
7+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
8+
import org.springframework.security.crypto.password.PasswordEncoder;
9+
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
10+
11+
/**
12+
* Created by @author yihui in 14:54 19/12/23.
13+
*/
14+
@Configuration
15+
public class SecurityAutoConfig {
16+
17+
@Bean
18+
public PasswordEncoder passwordEncoder() {
19+
return new BCryptPasswordEncoder();
20+
}
21+
22+
/**
23+
* 基于内存的认证方式
24+
*
25+
* @param passwordEncoder
26+
* @return
27+
*/
28+
@Bean
29+
public UserDetailsService userDetailsService(PasswordEncoder passwordEncoder) {
30+
User.UserBuilder users = User.builder().passwordEncoder(passwordEncoder::encode);
31+
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
32+
manager.createUser(users.username("1hui").password("123456").roles("guest").build());
33+
manager.createUser(users.username("2hui").password("666666").roles("manager").build());
34+
manager.createUser(users.username("3hui").password("root").roles("admin").build());
35+
return manager;
36+
}
37+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.git.hui.boot.security.rest;
2+
3+
import org.springframework.security.access.prepost.PreAuthorize;
4+
import org.springframework.security.core.context.SecurityContextHolder;
5+
import org.springframework.security.core.userdetails.UserDetails;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
/**
10+
* Created by @author yihui in 20:30 19/12/22.
11+
*/
12+
@RestController
13+
public class IndexRest {
14+
15+
public String getUserV2() {
16+
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
17+
18+
String userName;
19+
if (principal instanceof UserDetails) {
20+
userName = ((UserDetails) principal).getUsername();
21+
} else {
22+
userName = principal.toString();
23+
}
24+
return userName;
25+
}
26+
27+
/**
28+
* 全部都可以访问
29+
*
30+
* @return
31+
*/
32+
@GetMapping(path = {"/"})
33+
public String index() {
34+
return "hello this is index! welcome " + getUserV2();
35+
}
36+
37+
/**
38+
* 要求登录,且属于三个角色之一的才能访问
39+
*
40+
* @return
41+
*/
42+
@GetMapping(path = "guest")
43+
@PreAuthorize("hasAnyRole('guest', 'admin', 'manager')")
44+
public String guestHello() {
45+
return "guest hello: " + getUserV2();
46+
}
47+
48+
@GetMapping(path = "manager")
49+
@PreAuthorize("hasAnyRole('admin', 'manager')")
50+
public String managerHello() {
51+
return "manager hello: " + getUserV2();
52+
}
53+
54+
@GetMapping(path = "admin")
55+
@PreAuthorize("hasAnyRole('admin')")
56+
public String adminHello() {
57+
return "admin hello: " + getUserV2();
58+
}
59+
}

0 commit comments

Comments
 (0)