Skip to content

Commit e7fb16a

Browse files
committed
更多角色控制的使用方式
1 parent a803871 commit e7fb16a

File tree

2 files changed

+63
-44
lines changed

2 files changed

+63
-44
lines changed

pom.xml

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
<modelVersion>4.0.0</modelVersion>
5-
<parent>
6-
<groupId>org.springframework.boot</groupId>
7-
<artifactId>spring-boot-starter-parent</artifactId>
8-
<version>2.6.0</version>
9-
<relativePath/> <!-- lookup parent from repository -->
10-
</parent>
11-
<groupId>com.pyr</groupId>
12-
<artifactId>security</artifactId>
13-
<version>0.0.1-SNAPSHOT</version>
14-
<name>security</name>
15-
<description>Demo project for Spring Boot</description>
16-
<properties>
17-
<java.version>17</java.version>
18-
</properties>
19-
<dependencies>
20-
<dependency>
21-
<groupId>org.springframework.boot</groupId>
22-
<artifactId>spring-boot-starter-security</artifactId>
23-
</dependency>
24-
<dependency>
25-
<groupId>org.springframework.boot</groupId>
26-
<artifactId>spring-boot-starter-web</artifactId>
27-
</dependency>
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.6.13</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.pyr</groupId>
12+
<artifactId>security</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>security</name>
15+
<description>Demo project for Spring Boot</description>
16+
<properties>
17+
<java.version>17</java.version>
18+
</properties>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-security</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-web</artifactId>
27+
</dependency>
2828

29-
<dependency>
30-
<groupId>org.springframework.boot</groupId>
31-
<artifactId>spring-boot-starter-test</artifactId>
32-
<scope>test</scope>
33-
</dependency>
34-
<dependency>
35-
<groupId>org.springframework.security</groupId>
36-
<artifactId>spring-security-test</artifactId>
37-
<scope>test</scope>
38-
</dependency>
39-
</dependencies>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-test</artifactId>
32+
<scope>test</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.security</groupId>
36+
<artifactId>spring-security-test</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
</dependencies>
4040

41-
<build>
42-
<plugins>
43-
<plugin>
44-
<groupId>org.springframework.boot</groupId>
45-
<artifactId>spring-boot-maven-plugin</artifactId>
46-
</plugin>
47-
</plugins>
48-
</build>
41+
<build>
42+
<plugins>
43+
<plugin>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-maven-plugin</artifactId>
46+
</plugin>
47+
</plugins>
48+
</build>
4949

5050
</project>

src/main/java/com/pyr/security/controller/HelloController.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,23 @@ public String hello() {
2323
public String role() {
2424
return "admin Auth!";
2525
}
26+
27+
28+
/**
29+
* 当前登录的用户,有ADMIN或者MANAGER的角色
30+
*/
31+
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_MANAGER')")
32+
@RequestMapping("/roleAuth2")
33+
public String role2() {
34+
return "admin Auth 222!";
35+
}
36+
37+
/**
38+
* 传过来的id小于10 且 传过来的userName和数据库查出来的username相等
39+
*/
40+
@PreAuthorize("#id< 10 and principal.username.equals(#username)")
41+
@RequestMapping("/roleAuth3")
42+
public String role3(Integer id, String username) {
43+
return "admin Auth 33!";
44+
}
2645
}

0 commit comments

Comments
 (0)