Browse Source

token 认证

zjs 1 week ago
parent
commit
afd9a8c8cf

+ 8 - 2
pom.xml

@@ -178,8 +178,14 @@
178 178
 			<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
179 179
 			<version>3.1.0</version>
180 180
 		</dependency>
181
-		
182
-	</dependencies>
181
+        <dependency>
182
+            <groupId>org.testng</groupId>
183
+            <artifactId>testng</artifactId>
184
+            <version>RELEASE</version>
185
+            <scope>compile</scope>
186
+        </dependency>
187
+
188
+    </dependencies>
183 189
 
184 190
 
185 191
 	<dependencyManagement>

+ 2 - 2
src/main/java/com/cn/esermis/config/CorsConfig.java

@@ -9,9 +9,9 @@ public class CorsConfig implements WebMvcConfigurer {
9 9
 
10 10
     @Override
11 11
     public void addCorsMappings(CorsRegistry registry) {
12
-        registry.addMapping("/**") // 允许跨域的路径
12
+        registry.addMapping("/**")// 允许跨域的路径
13 13
                 .allowedOrigins("*") // 允许跨域请求的域名
14
-                .allowedMethods("GET", "POST", "PUT", "DELETE") // 允许的请求方法
14
+                .allowedMethods("GET", "POST", "PUT", "DELETE","OPTIONS") // 允许的请求方法
15 15
                 .allowedHeaders("*") // 允许的请求头
16 16
                 .allowCredentials(true); // 是否允许证书(cookies)
17 17
     }

+ 6 - 7
src/main/java/com/cn/esermis/config/JwtInterceptor.java

@@ -11,25 +11,24 @@ import javax.annotation.Resource;
11 11
 import javax.servlet.http.HttpServletRequest;
12 12
 import javax.servlet.http.HttpServletResponse;
13 13
 
14
-@Component
15
-public class JwtInterceptor implements HandlerInterceptor {
14
+@Component //
15
+public class MyInterceptor  implements HandlerInterceptor  {
16
+
16 17
 
17 18
     private static final String AUTH_HEADER = "Authorization";
18 19
     private static final String TOKEN_PREFIX = "Bearer ";
19
-
20 20
     @Resource
21 21
     DataAcquisitionInstrumentMapper dataAcquisitionInstrumentMapper;
22
-
23 22
     @Override
24
-    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
23
+    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
25 24
         String token = extractToken(request);
26 25
         if (token != null) {
27 26
             BaseUser userInfo = TokenUtils.getUserInfo(token);
28 27
             if (userInfo != null){
29 28
                 // 查询数据库是否有 公司和工厂信息
30 29
                 Boolean verifyCompany = dataAcquisitionInstrumentMapper.verifyCompany(userInfo.getCompanyId());
31
-                Boolean verifydepartmen = dataAcquisitionInstrumentMapper.verifydepartmen(userInfo.getFactoryId());
32
-                if (verifyCompany&&verifydepartmen){
30
+//                Boolean verifydepartmen = dataAcquisitionInstrumentMapper.verifydepartmen(userInfo.getFactoryId());
31
+                if (verifyCompany){
33 32
                     // 将用户信息设置到请求属性中,便于后续使用
34 33
                     request.setAttribute("userInfo", JSON.toJSONString(userInfo));
35 34
                     return true;

+ 7 - 4
src/main/java/com/cn/esermis/config/WebConfig.java

@@ -1,4 +1,5 @@
1 1
 package com.cn.esermis.config;
2
+
2 3
 import org.springframework.beans.factory.annotation.Autowired;
3 4
 import org.springframework.context.annotation.Configuration;
4 5
 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
@@ -8,11 +9,13 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
8 9
 public class WebConfig implements WebMvcConfigurer {
9 10
 
10 11
     @Autowired
11
-    private JwtInterceptor jwtInterceptor;
12
+    private MyInterceptor myInterceptor;
12 13
 
13 14
     @Override
14 15
     public void addInterceptors(InterceptorRegistry registry) {
15
-        registry.addInterceptor(jwtInterceptor)
16
-                .addPathPatterns("/**"); // 拦截所有路径
16
+        // 添加拦截器,并设置拦截规则
17
+        registry.addInterceptor(myInterceptor)
18
+                .addPathPatterns("/**"); // 拦截所有请求
19
+        //.excludePathPatterns("/login", "/error"); // 排除某些请求
17 20
     }
18
-}
21
+}