DESKTOP-8FESTBI\Administrator 8 månader sedan
förälder
incheckning
7a5861e101

+ 4
- 1
RuoYi-Vue/ruoyi-admin/src/main/resources/application.yml Visa fil

205
     - /druid/**
205
     - /druid/**
206
     - /swagger-ui/**
206
     - /swagger-ui/**
207
     - /v3/api-docs/**
207
     - /v3/api-docs/**
208
-    - /webjars/**
208
+    - /webjars/**
209
+
210
+cors:
211
+  allowed-origins: http://localhost:9090

+ 0
- 19
RuoYi-Vue/ruoyi-framework/src/main/java/com/ruoyi/framework/config/CorsConfig.java Visa fil

1
-package com.ruoyi.framework.config;
2
-
3
-import org.springframework.context.annotation.Configuration;
4
-import org.springframework.web.servlet.config.annotation.CorsRegistry;
5
-import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
6
-
7
-@Configuration
8
-public class CorsConfig implements WebMvcConfigurer {
9
-    @Override
10
-    public void addCorsMappings(CorsRegistry registry) {
11
-        registry.addMapping("/**")
12
-                // 使用 allowedOriginPatterns 替代 allowedOrigins
13
-                .allowedOriginPatterns("http://localhost:9090")
14
-                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
15
-                .allowedHeaders("*")
16
-                .allowCredentials(true)
17
-                .maxAge(3600);
18
-    }
19
-}

+ 18
- 6
RuoYi-Vue/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java Visa fil

1
 package com.ruoyi.framework.config;
1
 package com.ruoyi.framework.config;
2
 
2
 
3
+
3
 import org.springframework.beans.factory.annotation.Autowired;
4
 import org.springframework.beans.factory.annotation.Autowired;
5
+import org.springframework.beans.factory.annotation.Value;
4
 import org.springframework.context.annotation.Bean;
6
 import org.springframework.context.annotation.Bean;
5
 import org.springframework.context.annotation.Configuration;
7
 import org.springframework.context.annotation.Configuration;
6
 import org.springframework.http.HttpMethod;
8
 import org.springframework.http.HttpMethod;
24
 import com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl;
26
 import com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl;
25
 import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl;
27
 import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl;
26
 
28
 
29
+import java.util.List;
30
+
27
 /**
31
 /**
28
  * spring security配置
32
  * spring security配置
29
  *
33
  *
60
     /**
64
     /**
61
      * 跨域过滤器
65
      * 跨域过滤器
62
      */
66
      */
63
-    @Autowired
64
-    private CorsFilter corsFilter;
65
-
67
+    //@Autowired
68
+    //private CorsFilter corsFilter;
69
+// 代码中读取配置
70
+    @Value("${cors.allowed-origins}")
71
+    private List<String> allowedOrigins;
66
     /**
72
     /**
67
      * 允许匿名访问的地址
73
      * 允许匿名访问的地址
68
      */
74
      */
104
         permitAllUrl.getUrls().add("/ad/**");
110
         permitAllUrl.getUrls().add("/ad/**");
105
         permitAllUrl.getUrls().add("/swagger-ui/**");
111
         permitAllUrl.getUrls().add("/swagger-ui/**");
106
         permitAllUrl.getUrls().add("/v3/api-docs/**");
112
         permitAllUrl.getUrls().add("/v3/api-docs/**");
107
-
113
+        // 确保登录相关接口也在白名单
114
+        permitAllUrl.getUrls().add("/login");
115
+        permitAllUrl.getUrls().add("/register");
116
+        permitAllUrl.getUrls().add("/captchaImage");
108
         http
117
         http
109
                 // 禁用CSRF
118
                 // 禁用CSRF
110
                 .csrf(csrf -> csrf.disable())
119
                 .csrf(csrf -> csrf.disable())
147
                 // 添加JWT过滤器
156
                 // 添加JWT过滤器
148
                 .addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class)
157
                 .addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class)
149
                 // 添加CORS过滤器
158
                 // 添加CORS过滤器
150
-                .addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class);
159
+                //.addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class)
160
+                ;
151
 
161
 
152
         return http.build();
162
         return http.build();
153
     }
163
     }
155
     // CORS配置
165
     // CORS配置
156
     private CorsConfigurationSource corsConfigurationSource() {
166
     private CorsConfigurationSource corsConfigurationSource() {
157
         CorsConfiguration configuration = new CorsConfiguration();
167
         CorsConfiguration configuration = new CorsConfiguration();
158
-        configuration.addAllowedOrigin("*"); // 允许所有来源
168
+        configuration.setAllowedOriginPatterns(allowedOrigins);
169
+        //configuration.addAllowedOriginPattern("http://localhost:9090"); // 前端地址
170
+        //configuration.addAllowedOrigin("*"); // 允许所有来源
159
         configuration.addAllowedMethod("*"); // 允许所有方法
171
         configuration.addAllowedMethod("*"); // 允许所有方法
160
         configuration.addAllowedHeader("*"); // 允许所有头部
172
         configuration.addAllowedHeader("*"); // 允许所有头部
161
         configuration.setAllowCredentials(true); // 允许凭证
173
         configuration.setAllowCredentials(true); // 允许凭证

+ 10
- 1
RuoYi-Vue/ruoyi-system/src/main/java/com/ruoyi/novel/controller/NovelAdController.java Visa fil

27
         this.iAdService = iAdService;
27
         this.iAdService = iAdService;
28
         this.redisTemplate = redisTemplate;
28
         this.redisTemplate = redisTemplate;
29
     }
29
     }
30
-
30
+    // 修复广告接口
31
+//    @GetMapping("/position")
32
+//    public AjaxResult getAdByPosition(@RequestParam String code) {
33
+//        Ad ad = iAdService.selectAdByPositionCode(code);
34
+//        if (ad == null) {
35
+//            // 返回空对象而不是404
36
+//            return AjaxResult.success(new Ad());
37
+//        }
38
+//        return AjaxResult.success(ad);
39
+//    }
31
     @PostMapping("/count")
40
     @PostMapping("/count")
32
     public ResponseEntity<?> countAdView(
41
     public ResponseEntity<?> countAdView(
33
             @RequestBody AdCountRequest request,
42
             @RequestBody AdCountRequest request,

+ 5
- 1
RuoYi-Vue/ruoyi-system/src/main/java/com/ruoyi/novel/controller/NovelController.java Visa fil

52
 
52
 
53
     @GetMapping("/hot")
53
     @GetMapping("/hot")
54
     public List<Novel> getHotNovels() {
54
     public List<Novel> getHotNovels() {
55
-        return novelService.getHotNovels();
55
+        List<Novel> hotNovels = novelService.getHotNovels();
56
+        //return novelService.getHotNovels();
57
+        // 返回统一格式 { code, msg, data }
58
+        return (List<Novel>) AjaxResult.success(hotNovels);
56
     }
59
     }
57
 
60
 
58
     // 获取分类列表
61
     // 获取分类列表
81
     public TableDataInfo list(Novel novel) {
84
     public TableDataInfo list(Novel novel) {
82
         startPage();
85
         startPage();
83
         List<Novel> list = novelService.selectNovelOneList(novel);
86
         List<Novel> list = novelService.selectNovelOneList(novel);
87
+        System.out.println(list.size()+"条");
84
         // 关联分类名称
88
         // 关联分类名称
85
         list.forEach(n -> {
89
         list.forEach(n -> {
86
             NovelCategory category = (NovelCategory) novelService.getNovelsByCategory(n.getCategoryId());
90
             NovelCategory category = (NovelCategory) novelService.getNovelsByCategory(n.getCategoryId());

Laddar…
Avbryt
Spara