Browse Source

数据库验证服务是否允许访问

yufeng0528 4 years ago
parent
commit
73a62b0ff5

+ 16 - 1
spring5-auth/spring5-auth-server/pom.xml

@@ -48,6 +48,21 @@
48 48
 <!--             <groupId>org.apache.commons</groupId> -->
49 49
 <!--             <artifactId>commons-lang3</artifactId> -->
50 50
 <!--         </dependency> -->
51
-		
51
+		      <!--mybatis plus依赖包-->
52
+        <dependency>
53
+            <groupId>com.baomidou</groupId>
54
+            <artifactId>mybatis-plus-boot-starter</artifactId>
55
+            <version>3.1.0</version>
56
+        </dependency>
57
+        <dependency>
58
+            <groupId>org.mybatis</groupId>
59
+            <artifactId>mybatis-spring</artifactId>
60
+            <version>2.0.0</version>
61
+        </dependency>
62
+        <!--数据库-->
63
+        <dependency>
64
+            <groupId>mysql</groupId>
65
+            <artifactId>mysql-connector-java</artifactId>
66
+        </dependency>
52 67
 	</dependencies>
53 68
 </project>

+ 2 - 0
spring5-auth/spring5-auth-server/src/main/java/com/yaozhitech/spring5/Oauth2Application.java

@@ -1,9 +1,11 @@
1 1
 package com.yaozhitech.spring5;
2 2
 
3
+import org.mybatis.spring.annotation.MapperScan;
3 4
 import org.springframework.boot.SpringApplication;
4 5
 import org.springframework.boot.autoconfigure.SpringBootApplication;
5 6
 
6 7
 
8
+@MapperScan("com.yaozhitech.spring5.mapper")
7 9
 @SpringBootApplication
8 10
 public class Oauth2Application {
9 11
     public static void main(String[] args) {

+ 2 - 1
spring5-auth/spring5-auth-server/src/main/java/com/yaozhitech/spring5/config/ShiroConfiguration.java

@@ -106,12 +106,13 @@ public class ShiroConfiguration {
106 106
     protected ShiroFilterChainDefinition shiroFilterChainDefinition() {
107 107
         DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
108 108
         chainDefinition.addPathDefinition("/login", "noSessionCreation,anon");  //login不做认证,noSessionCreation的作用是用户在操作session时会抛异常
109
+        chainDefinition.addPathDefinition("/clientAuth/**", "noSessionCreation,anon"); 
109 110
         chainDefinition.addPathDefinition("/logout", "noSessionCreation,authcToken[permissive]"); //做用户认证,permissive参数的作用是当token无效时也允许请求访问,不会返回鉴权未通过的错误
110 111
         chainDefinition.addPathDefinition("/image/**", "anon");
111 112
         chainDefinition.addPathDefinition("/admin/**", "noSessionCreation,authcToken,anyRole[admin,manager]"); //只允许admin或manager角色的用户访问
112 113
         chainDefinition.addPathDefinition("/article/list", "noSessionCreation,authcToken");
113 114
         chainDefinition.addPathDefinition("/article/*", "noSessionCreation,authcToken[permissive]");
114
-        chainDefinition.addPathDefinition("/**", "noSessionCreation,authcToken"); // 默认进行用户鉴权
115
+        chainDefinition.addPathDefinition("/**", "noSessionCreation,anon"); // 默认进行用户鉴权
115 116
         return chainDefinition;
116 117
     }
117 118
 

+ 24 - 0
spring5-auth/spring5-auth-server/src/main/java/com/yaozhitech/spring5/controller/AuthClientController.java

@@ -0,0 +1,24 @@
1
+package com.yaozhitech.spring5.controller;
2
+
3
+import org.springframework.http.ResponseEntity;
4
+import org.springframework.web.bind.annotation.GetMapping;
5
+import org.springframework.web.bind.annotation.RequestMapping;
6
+import org.springframework.web.bind.annotation.RequestParam;
7
+import org.springframework.web.bind.annotation.RestController;
8
+
9
+import com.yaozhitech.spring5.service.ClientAuthService;
10
+
11
+import lombok.AllArgsConstructor;
12
+
13
+@RestController
14
+@RequestMapping("/clientAuth")
15
+@AllArgsConstructor
16
+public class AuthClientController {
17
+	
18
+	private final ClientAuthService authClientService;
19
+
20
+	@GetMapping("/verify")
21
+	public ResponseEntity<Boolean> verify(@RequestParam String service, @RequestParam String allowClient, @RequestParam String secret) {
22
+		return ResponseEntity.ok(authClientService.isAllowed(service, allowClient, secret));
23
+	}
24
+}

+ 28 - 0
spring5-auth/spring5-auth-server/src/main/java/com/yaozhitech/spring5/domain/Client.java

@@ -0,0 +1,28 @@
1
+package com.yaozhitech.spring5.domain;
2
+
3
+import java.io.Serializable;
4
+
5
+import com.baomidou.mybatisplus.annotation.IdType;
6
+import com.baomidou.mybatisplus.annotation.TableId;
7
+import com.baomidou.mybatisplus.annotation.TableName;
8
+
9
+import lombok.Data;
10
+import lombok.EqualsAndHashCode;
11
+import lombok.experimental.Accessors;
12
+
13
+@Data
14
+@EqualsAndHashCode(callSuper = false)
15
+@Accessors(chain = true)
16
+@TableName("sys_client")
17
+public class Client implements Serializable {
18
+
19
+	private static final long serialVersionUID = 1L;
20
+
21
+	@TableId(value = "id", type = IdType.AUTO)
22
+	private Integer id;
23
+
24
+	private String name;
25
+
26
+	private String secret;
27
+
28
+}

+ 28 - 0
spring5-auth/spring5-auth-server/src/main/java/com/yaozhitech/spring5/domain/ClientAuth.java

@@ -0,0 +1,28 @@
1
+package com.yaozhitech.spring5.domain;
2
+
3
+import java.io.Serializable;
4
+
5
+import com.baomidou.mybatisplus.annotation.IdType;
6
+import com.baomidou.mybatisplus.annotation.TableId;
7
+import com.baomidou.mybatisplus.annotation.TableName;
8
+
9
+import lombok.Data;
10
+import lombok.EqualsAndHashCode;
11
+import lombok.experimental.Accessors;
12
+
13
+@Data
14
+@EqualsAndHashCode(callSuper = false)
15
+@Accessors(chain = true)
16
+@TableName("sys_client_auth")
17
+public class ClientAuth implements Serializable {
18
+
19
+	private static final long serialVersionUID = 1L;
20
+
21
+	@TableId(value = "id", type = IdType.AUTO)
22
+	private Integer id;
23
+
24
+	private String service;
25
+
26
+	private String allowClient;
27
+
28
+}

+ 8 - 0
spring5-auth/spring5-auth-server/src/main/java/com/yaozhitech/spring5/mapper/ClientAuthMapper.java

@@ -0,0 +1,8 @@
1
+package com.yaozhitech.spring5.mapper;
2
+
3
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4
+import com.yaozhitech.spring5.domain.ClientAuth;
5
+
6
+public interface ClientAuthMapper extends BaseMapper<ClientAuth>{
7
+ 
8
+}

+ 8 - 0
spring5-auth/spring5-auth-server/src/main/java/com/yaozhitech/spring5/mapper/ClientMapper.java

@@ -0,0 +1,8 @@
1
+package com.yaozhitech.spring5.mapper;
2
+
3
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4
+import com.yaozhitech.spring5.domain.Client;
5
+
6
+public interface ClientMapper extends BaseMapper<Client>{
7
+ 
8
+}

+ 33 - 0
spring5-auth/spring5-auth-server/src/main/java/com/yaozhitech/spring5/service/ClientAuthService.java

@@ -0,0 +1,33 @@
1
+package com.yaozhitech.spring5.service;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.stereotype.Service;
5
+
6
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
7
+import com.yaozhitech.spring5.domain.Client;
8
+import com.yaozhitech.spring5.domain.ClientAuth;
9
+import com.yaozhitech.spring5.mapper.ClientAuthMapper;
10
+import com.yaozhitech.spring5.mapper.ClientMapper;
11
+
12
+@Service
13
+public class ClientAuthService {
14
+
15
+	@Autowired
16
+	private ClientAuthMapper authClientMapper;
17
+	
18
+	@Autowired
19
+	private ClientMapper clientMapper;
20
+	
21
+	public Boolean isAllowed(String service, String allowClient, String secret) {
22
+		Client client = clientMapper.selectOne(new QueryWrapper<Client>().eq("name", allowClient).eq("secret", secret));
23
+		if (client == null) {
24
+			return false;
25
+		}
26
+		
27
+		ClientAuth authClient = authClientMapper.selectOne(new QueryWrapper<ClientAuth>()
28
+				.eq("service", service)
29
+				.eq("allow_client", allowClient));
30
+		
31
+		return authClient != null;
32
+	}
33
+}

+ 24 - 0
spring5-auth/spring5-auth-server/src/main/java/com/yaozhitech/spring5/service/ClientService.java

@@ -0,0 +1,24 @@
1
+package com.yaozhitech.spring5.service;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.stereotype.Service;
5
+
6
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
7
+import com.yaozhitech.spring5.domain.ClientAuth;
8
+import com.yaozhitech.spring5.mapper.ClientAuthMapper;
9
+
10
+@Service
11
+public class ClientService {
12
+
13
+	@Autowired
14
+	private ClientAuthMapper authClientMapper;
15
+	
16
+	public Boolean isAllowed(String service, String secret, String allowClient) {
17
+		ClientAuth authClient = authClientMapper.selectOne(new QueryWrapper<ClientAuth>()
18
+				.eq("service", service)
19
+				.eq("secret", secret)
20
+				.eq("allow_client", allowClient));
21
+		
22
+		return authClient != null;
23
+	}
24
+}

+ 5 - 0
spring5-auth/spring5-auth-server/src/main/resources/application.yml

@@ -16,6 +16,11 @@ spring:
16 16
     port: 6280
17 17
     password: bbztx123456
18 18
     timeout: 5000
19
+  datasource:
20
+    driver-class-name: com.mysql.jdbc.Driver
21
+    url: jdbc:${DATASOURCE_DBTYPE:mysql}://121.41.17.212:${DATASOURCE_PORT:3306}/ceshi?characterEncoding=UTF-8&useUnicode=true&useSSL=false
22
+    username: ${DATASOURCE_USERNAME:root}
23
+    password: huojutech!23
19 24
   
20 25
 password:
21 26
   salt: k12829WhsvnEV$#03b2n          

+ 20 - 0
spring5-common/src/main/java/com/yaozhitech/spring5/common/constant/RestCodeConstants.java

@@ -7,4 +7,24 @@ public class RestCodeConstants {
7 7
 
8 8
     public static final int TOKEN_ERROR_CODE = 40101;
9 9
     public static final int TOKEN_FORBIDDEN_CODE = 40301;
10
+    
11
+    /**
12
+     * const codeMessage = {
13
+  200: '服务器成功返回请求的数据。',
14
+  201: '新建或修改数据成功。',
15
+  202: '一个请求已经进入后台排队(异步任务)。',
16
+  204: '删除数据成功。',
17
+  400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
18
+  401: '用户没有权限(令牌、用户名、密码错误)。',
19
+  403: '用户得到授权,但是访问是被禁止的。',
20
+  404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
21
+  406: '请求的格式不可得。',
22
+  410: '请求的资源被永久删除,且不会再得到的。',
23
+  422: '当创建一个对象时,发生一个验证错误。',
24
+  500: '服务器发生错误,请检查服务器。',
25
+  502: '网关错误。',
26
+  503: '服务不可用,服务器暂时过载或维护。',
27
+  504: '网关超时。',
28
+};
29
+     */
10 30
 }