fzzj 9 місяці тому
джерело
коміт
a069495548

+ 2
- 1
RuoYi-Vue/pom.xml Переглянути файл

@@ -235,11 +235,12 @@
235 235
 
236 236
     <modules>
237 237
         <module>ruoyi-admin</module>
238
+        <module>ruoyi-common</module>
238 239
         <module>ruoyi-framework</module>
239 240
         <module>ruoyi-system</module>
240 241
         <module>ruoyi-quartz</module>
241 242
         <module>ruoyi-generator</module>
242
-        <module>ruoyi-common</module>
243
+
243 244
 <!--        <module>ruoyi-novel</module>-->
244 245
     </modules>
245 246
     <packaging>pom</packaging>

+ 4
- 0
RuoYi-Vue/ruoyi-framework/pom.xml Переглянути файл

@@ -58,6 +58,10 @@
58 58
             <groupId>com.ruoyi</groupId>
59 59
             <artifactId>ruoyi-system</artifactId>
60 60
         </dependency>
61
+        <dependency>
62
+            <groupId>com.ruoyi</groupId>
63
+            <artifactId>ruoyi-common</artifactId>
64
+        </dependency>
61 65
 
62 66
     </dependencies>
63 67
 

+ 8
- 12
RuoYi-Vue/ruoyi-system/pom.xml Переглянути файл

@@ -74,10 +74,10 @@
74 74
             <artifactId>guava</artifactId>
75 75
             <version>33.4.0-jre</version> <!-- 使用较新版本 -->
76 76
         </dependency>
77
-        <dependency>
78
-            <groupId>org.projectlombok</groupId>
79
-            <artifactId>lombok</artifactId>
80
-        </dependency>
77
+<!--        <dependency>-->
78
+<!--            <groupId>org.projectlombok</groupId>-->
79
+<!--            <artifactId>lombok</artifactId>-->
80
+<!--        </dependency>-->
81 81
         <dependency>
82 82
             <groupId>org.aspectj</groupId>
83 83
             <artifactId>aspectjweaver</artifactId>
@@ -86,20 +86,16 @@
86 86
             <groupId>org.springframework.boot</groupId>
87 87
             <artifactId>spring-boot-starter-data-jpa</artifactId>
88 88
         </dependency>
89
-        <dependency>
90
-            <groupId>org.springframework.boot</groupId>
91
-            <artifactId>spring-boot-starter-data-jpa</artifactId>
92
-        </dependency>
89
+<!--        <dependency>-->
90
+<!--            <groupId>org.springframework.boot</groupId>-->
91
+<!--            <artifactId>spring-boot-starter-data-jpa</artifactId>-->
92
+<!--        </dependency>-->
93 93
         <dependency>
94 94
             <groupId>org.testng</groupId>
95 95
             <artifactId>testng</artifactId>
96 96
             <version>RELEASE</version>
97 97
             <scope>compile</scope>
98 98
         </dependency>
99
-        <dependency>
100
-            <groupId>com.ruoyi</groupId>
101
-            <artifactId>ruoyi-framework</artifactId>
102
-        </dependency>
103 99
     </dependencies>
104 100
 
105 101
 </project>

+ 6
- 0
RuoYi-Vue/ruoyi-system/src/main/java/com/ruoyi/novel/config/JwtFilter.java Переглянути файл

@@ -2,6 +2,7 @@ package com.ruoyi.novel.config;
2 2
 
3 3
 // 新增 JwtFilter.java
4 4
 import org.springframework.security.core.context.SecurityContextHolder;
5
+import org.springframework.util.StringUtils;
5 6
 import org.springframework.web.filter.OncePerRequestFilter;
6 7
 import javax.servlet.FilterChain;
7 8
 import javax.servlet.ServletException;
@@ -33,5 +34,10 @@ public class JwtFilter extends OncePerRequestFilter {
33 34
 
34 35
     private String resolveToken(HttpServletRequest request) {
35 36
         // 从Header提取Token的逻辑
37
+        String bearerToken = request.getHeader("Authorization");
38
+        if (StringUtils.hasText(bearerToken) && bearerToken.startsWith("Bearer ")) {
39
+            return bearerToken.substring(7); // 去掉 "Bearer " 前缀
40
+        }
41
+        return null;
36 42
     }
37 43
 }

+ 87
- 87
RuoYi-Vue/ruoyi-system/src/main/java/com/ruoyi/novel/config/SecurityConfig.java Переглянути файл

@@ -1,87 +1,87 @@
1
-package com.ruoyi.novel.config;
2
-
3
-import com.ruoyi.framework.security.filter.JwtAuthenticationTokenFilter;
4
-import com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl;
5
-import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl;
6
-import com.ruoyi.novel.service.CustomUserDetailsService;
7
-import org.springframework.beans.factory.annotation.Autowired;
8
-import org.springframework.context.annotation.Bean;
9
-import org.springframework.context.annotation.Configuration;
10
-import org.springframework.security.authentication.AuthenticationManager;
11
-import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
12
-import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
13
-import org.springframework.security.config.annotation.web.builders.HttpSecurity;
14
-import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
15
-import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
16
-import org.springframework.security.config.http.SessionCreationPolicy;
17
-import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
18
-import org.springframework.security.crypto.password.PasswordEncoder;
19
-import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
20
-import org.springframework.web.filter.CorsFilter;
21
-
22
-@Configuration
23
-@EnableWebSecurity
24
-@EnableGlobalMethodSecurity(prePostEnabled = true)
25
-public class SecurityConfig extends WebSecurityConfigurerAdapter {
26
-
27
-    @Autowired
28
-    private CustomUserDetailsService customUserDetailsService;
29
-
30
-    @Autowired
31
-    private TokenProvider tokenProvider;
32
-
33
-    @Autowired
34
-    private CorsFilter corsFilter;
35
-
36
-    @Autowired
37
-    private AuthenticationEntryPointImpl unauthorizedHandler;
38
-
39
-    @Autowired
40
-    private LogoutSuccessHandlerImpl logoutSuccessHandler;
41
-
42
-    @Override
43
-    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
44
-        auth.userDetailsService(customUserDetailsService).passwordEncoder(passwordEncoder());
45
-    }
46
-
47
-    @Bean
48
-    public PasswordEncoder passwordEncoder() {
49
-        return new BCryptPasswordEncoder();
50
-    }
51
-
52
-    @Bean
53
-    @Override
54
-    public AuthenticationManager authenticationManagerBean() throws Exception {
55
-        return super.authenticationManagerBean();
56
-    }
57
-
58
-    @Override
59
-    protected void configure(HttpSecurity http) throws Exception {
60
-        http
61
-                .cors().and()
62
-                .csrf().disable()
63
-                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
64
-                .and()
65
-                .exceptionHandling().authenticationEntryPoint(unauthorizedHandler)
66
-                .and()
67
-                .authorizeRequests()
68
-                .antMatchers("/api/auth/**").permitAll()
69
-                //.antrimatchers("/api/novel/**").permitAll()
70
-                .antMatchers("/admin/**").hasRole("ADMIN")
71
-                .anyRequest().authenticated()
72
-                .and()
73
-                .apply(new JwtConfigurer(tokenProvider));
74
-
75
-        http.addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class);
76
-        http.addFilterBefore(authenticationTokenFilter(), UsernamePasswordAuthenticationFilter.class);
77
-
78
-        http.logout()
79
-                .logoutUrl("/api/auth/logout")
80
-                .logoutSuccessHandler(logoutSuccessHandler);
81
-    }
82
-
83
-    @Bean
84
-    public JwtAuthenticationTokenFilter authenticationTokenFilter() {
85
-        return new JwtAuthenticationTokenFilter();
86
-    }
87
-}
1
+//package com.ruoyi.novel.config;
2
+//
3
+//import com.ruoyi.framework.security.filter.JwtAuthenticationTokenFilter;
4
+//import com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl;
5
+//import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl;
6
+//import com.ruoyi.novel.service.CustomUserDetailsService;
7
+//import org.springframework.beans.factory.annotation.Autowired;
8
+//import org.springframework.context.annotation.Bean;
9
+//import org.springframework.context.annotation.Configuration;
10
+//import org.springframework.security.authentication.AuthenticationManager;
11
+//import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
12
+//import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
13
+//import org.springframework.security.config.annotation.web.builders.HttpSecurity;
14
+//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
15
+//import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
16
+//import org.springframework.security.config.http.SessionCreationPolicy;
17
+//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
18
+//import org.springframework.security.crypto.password.PasswordEncoder;
19
+//import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
20
+//import org.springframework.web.filter.CorsFilter;
21
+//
22
+//@Configuration
23
+//@EnableWebSecurity
24
+//@EnableGlobalMethodSecurity(prePostEnabled = true)
25
+//public class SecurityConfig extends WebSecurityConfigurerAdapter {
26
+//
27
+//    @Autowired
28
+//    private CustomUserDetailsService customUserDetailsService;
29
+//
30
+//    @Autowired
31
+//    private TokenProvider tokenProvider;
32
+//
33
+//    @Autowired
34
+//    private CorsFilter corsFilter;
35
+//
36
+//    @Autowired
37
+//    private AuthenticationEntryPointImpl unauthorizedHandler;
38
+//
39
+//    @Autowired
40
+//    private LogoutSuccessHandlerImpl logoutSuccessHandler;
41
+//
42
+//    @Override
43
+//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
44
+//        auth.userDetailsService(customUserDetailsService).passwordEncoder(passwordEncoder());
45
+//    }
46
+//
47
+//    @Bean
48
+//    public PasswordEncoder passwordEncoder() {
49
+//        return new BCryptPasswordEncoder();
50
+//    }
51
+//
52
+//    @Bean
53
+//    @Override
54
+//    public AuthenticationManager authenticationManagerBean() throws Exception {
55
+//        return super.authenticationManagerBean();
56
+//    }
57
+//
58
+//    @Override
59
+//    protected void configure(HttpSecurity http) throws Exception {
60
+//        http
61
+//                .cors().and()
62
+//                .csrf().disable()
63
+//                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
64
+//                .and()
65
+//                .exceptionHandling().authenticationEntryPoint(unauthorizedHandler)
66
+//                .and()
67
+//                .authorizeRequests()
68
+//                .antMatchers("/api/auth/**").permitAll()
69
+//                //.antrimatchers("/api/novel/**").permitAll()
70
+//                .antMatchers("/admin/**").hasRole("ADMIN")
71
+//                .anyRequest().authenticated()
72
+//                .and()
73
+//                .apply(new JwtConfigurer(tokenProvider));
74
+//
75
+//        http.addFilterBefore(corsFilter, JwtAuthenticationTokenFilter.class);
76
+//        http.addFilterBefore(authenticationTokenFilter(), UsernamePasswordAuthenticationFilter.class);
77
+//
78
+//        http.logout()
79
+//                .logoutUrl("/api/auth/logout")
80
+//                .logoutSuccessHandler(logoutSuccessHandler);
81
+//    }
82
+//
83
+//    @Bean
84
+//    public JwtAuthenticationTokenFilter authenticationTokenFilter() {
85
+//        return new JwtAuthenticationTokenFilter();
86
+//    }
87
+//}

+ 9
- 0
RuoYi-Vue/ruoyi-system/src/main/java/com/ruoyi/novel/config/TokenProvider.java Переглянути файл

@@ -12,6 +12,7 @@ import io.jsonwebtoken.Jwts;
12 12
 import io.jsonwebtoken.SignatureAlgorithm;
13 13
 import org.springframework.beans.factory.annotation.Autowired;
14 14
 import org.springframework.beans.factory.annotation.Value;
15
+import org.springframework.security.core.Authentication;
15 16
 import org.springframework.stereotype.Component;
16 17
 
17 18
 import javax.servlet.http.HttpServletRequest;
@@ -126,4 +127,12 @@ public class TokenProvider {
126 127
     private String getTokenKey(String uuid) {
127 128
         return CacheConstants.LOGIN_TOKEN_KEY + uuid;
128 129
     }
130
+
131
+    public boolean validateToken(String token) {
132
+        return true;
133
+    }
134
+
135
+    public Authentication getAuthentication(String token) {
136
+        return null;
137
+    }
129 138
 }

+ 1
- 1
RuoYi-Vue/ruoyi-system/src/main/java/com/ruoyi/novel/controller/NovelController.java Переглянути файл

@@ -71,7 +71,7 @@ public class NovelController extends BaseController {
71 71
 // 按分类获取小说
72 72
 @GetMapping("/list")
73 73
 public TableDataInfo getNovelsByCategory(@RequestParam(required = false) Long categoryId) {
74
-    return novelService.getNovelsByCategory(categoryId);
74
+    return (TableDataInfo) novelService.getNovelsByCategory(categoryId);
75 75
 }
76 76
 //    @GetMapping("/list")
77 77
 //    public ResponseEntity<?> getNovelsByCategory(@RequestParam(required = false) Long categoryId) {

+ 4
- 0
RuoYi-Vue/ruoyi-system/src/main/java/com/ruoyi/novel/domain/ChapterUploadDTO.java Переглянути файл

@@ -0,0 +1,4 @@
1
+package com.ruoyi.novel.domain;
2
+
3
+public class ChapterUploadDTO {
4
+}

+ 4
- 0
RuoYi-Vue/ruoyi-system/src/main/java/com/ruoyi/novel/domain/NovelUploadDTO.java Переглянути файл

@@ -0,0 +1,4 @@
1
+package com.ruoyi.novel.domain;
2
+
3
+public class NovelUploadDTO {
4
+}

+ 1
- 1
RuoYi-Vue/ruoyi-system/src/main/java/com/ruoyi/novel/mapper/CategoryMapper.java Переглянути файл

@@ -8,5 +8,5 @@ import java.util.List;
8 8
 // CategoryMapper.java
9 9
 @Mapper
10 10
 public interface CategoryMapper {
11
-    List<Category> selectCategoryList(Category category);
11
+    List<Category> selectCategoryList();
12 12
 }

+ 48
- 48
RuoYi-Vue/ruoyi-system/src/main/java/com/ruoyi/novel/service/CustomUserDetailsService.java Переглянути файл

@@ -1,48 +1,48 @@
1
-package com.ruoyi.novel.service;
2
-
3
-import com.ruoyi.common.core.domain.entity.SysUser;
4
-import com.ruoyi.common.core.domain.model.LoginUser;
5
-import com.ruoyi.common.enums.UserStatus;
6
-import com.ruoyi.common.exception.ServiceException;
7
-import com.ruoyi.common.utils.StringUtils;
8
-import com.ruoyi.framework.web.service.SysPermissionService;
9
-import com.ruoyi.system.service.ISysUserService;
10
-import org.slf4j.Logger;
11
-import org.slf4j.LoggerFactory;
12
-import org.springframework.beans.factory.annotation.Autowired;
13
-import org.springframework.security.core.userdetails.UserDetails;
14
-import org.springframework.security.core.userdetails.UserDetailsService;
15
-import org.springframework.security.core.userdetails.UsernameNotFoundException;
16
-import org.springframework.stereotype.Service;
17
-
18
-@Service
19
-public class CustomUserDetailsService implements UserDetailsService {
20
-    private static final Logger log = LoggerFactory.getLogger(CustomUserDetailsService.class);
21
-
22
-    @Autowired
23
-    private ISysUserService userService;
24
-
25
-    @Autowired
26
-    private SysPermissionService permissionService;
27
-
28
-    @Override
29
-    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
30
-        SysUser user = userService.selectUserByUserName(username);
31
-        if (StringUtils.isNull(user)) {
32
-            log.info("登录用户:{} 不存在.", username);
33
-            throw new ServiceException("登录用户:" + username + " 不存在");
34
-        } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
35
-            log.info("登录用户:{} 已被删除.", username);
36
-            throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
37
-        } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
38
-            log.info("登录用户:{} 已被停用.", username);
39
-            throw new ServiceException("对不起,您的账号:" + username + " 已停用");
40
-        }
41
-
42
-        return createLoginUser(user);
43
-    }
44
-
45
-    public UserDetails createLoginUser(SysUser user) {
46
-        return new LoginUser(user.getUserId(), user.getDeptId(), user, permissionService.getMenuPermission(user));
47
-    }
48
-}
1
+//package com.ruoyi.novel.service;
2
+//
3
+//import com.ruoyi.common.core.domain.entity.SysUser;
4
+//import com.ruoyi.common.core.domain.model.LoginUser;
5
+//import com.ruoyi.common.enums.UserStatus;
6
+//import com.ruoyi.common.exception.ServiceException;
7
+//import com.ruoyi.common.utils.StringUtils;
8
+//import com.ruoyi.framework.web.service.SysPermissionService;
9
+//import com.ruoyi.system.service.ISysUserService;
10
+//import org.slf4j.Logger;
11
+//import org.slf4j.LoggerFactory;
12
+//import org.springframework.beans.factory.annotation.Autowired;
13
+//import org.springframework.security.core.userdetails.UserDetails;
14
+//import org.springframework.security.core.userdetails.UserDetailsService;
15
+//import org.springframework.security.core.userdetails.UsernameNotFoundException;
16
+//import org.springframework.stereotype.Service;
17
+//
18
+//@Service
19
+//public class CustomUserDetailsService implements UserDetailsService {
20
+//    private static final Logger log = LoggerFactory.getLogger(CustomUserDetailsService.class);
21
+//
22
+//    @Autowired
23
+//    private ISysUserService userService;
24
+//
25
+////    @Autowired
26
+////    private SysPermissionService permissionService;
27
+//
28
+//    @Override
29
+//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
30
+//        SysUser user = userService.selectUserByUserName(username);
31
+//        if (StringUtils.isNull(user)) {
32
+//            log.info("登录用户:{} 不存在.", username);
33
+//            throw new ServiceException("登录用户:" + username + " 不存在");
34
+//        } else if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
35
+//            log.info("登录用户:{} 已被删除.", username);
36
+//            throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
37
+//        } else if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
38
+//            log.info("登录用户:{} 已被停用.", username);
39
+//            throw new ServiceException("对不起,您的账号:" + username + " 已停用");
40
+//        }
41
+//
42
+//        return createLoginUser(user);
43
+//    }
44
+//
45
+////    public UserDetails createLoginUser(SysUser user) {
46
+////        return new LoginUser(user.getUserId(), user.getDeptId(), user, permissionService.getMenuPermission(user));
47
+////    }
48
+//}

+ 19
- 4
RuoYi-Vue/ruoyi-system/src/main/java/com/ruoyi/novel/service/NovelService.java Переглянути файл

@@ -6,6 +6,7 @@ import com.ruoyi.novel.domain.Chapter;
6 6
 import com.ruoyi.novel.domain.Novel;
7 7
 import jdk.jfr.Category;
8 8
 import org.springframework.transaction.annotation.Transactional;
9
+import org.springframework.web.multipart.MultipartFile;
9 10
 
10 11
 import java.util.List;
11 12
 
@@ -18,9 +19,9 @@ public interface NovelService {
18 19
     int deleteNovelByIds(Long[] ids);
19 20
     Novel selectNovelById(Long id);
20 21
 
21
-    void processNovelUpload(NovelUploadDTO dto);
22
-    void processChapterUpload(ChapterUploadDTO dto);
23
-    List<AuthorApplication> getAuthorApplications();
22
+//    void processNovelUpload(NovelUploadDTO dto);
23
+//    void processChapterUpload(ChapterUploadDTO dto);
24
+    List<AuthorApplication> getAuthorApplications(Integer status);
24 25
     void approveAuthorApplication(Long applicationId);
25 26
     void rejectAuthorApplication(Long applicationId);
26 27
 
@@ -29,6 +30,20 @@ public interface NovelService {
29 30
     List<Novel> getHotNovels();
30 31
     List<Novel> getNovelsByCategory(Long categoryId);
31 32
     List<Chapter> getChaptersByNovelId(Long novelId);
32
-    String getChapterContent(Long chapterId);
33
+    Chapter getChapterContent(Long chapterId);
33 34
     void submitAuthorApplication(AuthorApplicationDTO dto, Long userId);
35
+
36
+    Novel processNovelUpload(MultipartFile file, String title, Long authorId);
37
+
38
+    Chapter processChapterUpload(MultipartFile file, Long novelId, Integer chapterOrder);
39
+
40
+//    void processNovelUpload(NovelUploadDTO dto);
41
+//
42
+//    void processChapterUpload(ChapterUploadDTO dto);
43
+
44
+//    List<Novel> getHotNovels();
45
+//    List<Novel> getNovelsByCategory(Long categoryId);
46
+//    List<Chapter> getChaptersByNovelId(Long novelId);
47
+//    String getChapterContent(Long chapterId);
48
+//    void submitAuthorApplication(AuthorApplicationDTO dto, Long userId);
34 49
 }

+ 76
- 4
RuoYi-Vue/ruoyi-system/src/main/java/com/ruoyi/novel/service/TokenService.java Переглянути файл

@@ -1,17 +1,89 @@
1 1
 package com.ruoyi.novel.service;
2 2
 
3 3
 import com.ruoyi.common.core.domain.model.LoginUser;
4
+import io.jsonwebtoken.Claims;
5
+import io.jsonwebtoken.Jwts;
6
+import io.jsonwebtoken.io.Decoders;
7
+import io.jsonwebtoken.security.Keys;
8
+import org.springframework.beans.factory.annotation.Value;
9
+import org.springframework.security.core.Authentication;
10
+import org.springframework.security.core.GrantedAuthority;
11
+import org.springframework.security.core.authority.SimpleGrantedAuthority;
4 12
 import org.springframework.stereotype.Service;
5 13
 
14
+import javax.crypto.SecretKey;
15
+import java.util.*;
16
+import java.util.stream.Collectors;
17
+
6 18
 @Service
7 19
 public class TokenService {
8
-    // 添加缺失方法
20
+
21
+    @Value("${jwt.secret}") // 从配置文件中读取密钥
22
+    private String secret;
23
+
24
+    @Value("${jwt.expiration}") // JWT有效期
25
+    private long expiration;
26
+
27
+    // 获取登录用户信息
9 28
     public LoginUser getLoginUser(String token) {
10
-        // 实际解析逻辑
11
-        return new LoginUser(parseUserId(token), parseUserDetails(token));
29
+        Claims claims = parseToken(token);
30
+        Long userId = claims.get("user_id", Long.class);
31
+        String username = claims.getSubject();
32
+
33
+        // 获取权限列表
34
+        List<String> authorities = claims.get("authorities", List.class);
35
+        Set<GrantedAuthority> grantedAuthorities = authorities.stream()
36
+                .map(SimpleGrantedAuthority::new)
37
+                .collect(Collectors.toSet());
38
+
39
+        return new LoginUser(userId, username, "", grantedAuthorities);
12 40
     }
13 41
 
42
+    // 解析用户ID
14 43
     public Long parseUserId(String token) {
15
-        // 解析用户ID的实现
44
+        Claims claims = parseToken(token);
45
+        return claims.get("user_id", Long.class);
46
+    }
47
+
48
+    // 解析JWT令牌
49
+    private Claims parseToken(String token) {
50
+        byte[] keyBytes = Decoders.BASE64.decode(secret);
51
+        SecretKey key = Keys.hmacShaKeyFor(keyBytes);
52
+
53
+        return Jwts.parserBuilder()
54
+                .setSigningKey(key)
55
+                .build()
56
+                .parseClaimsJws(token)
57
+                .getBody();
58
+    }
59
+
60
+    // 验证令牌有效期
61
+    public boolean validateToken(String token) {
62
+        try {
63
+            Claims claims = parseToken(token);
64
+            Date expirationDate = claims.getExpiration();
65
+            return expirationDate.after(new Date());
66
+        } catch (Exception e) {
67
+            return false;
68
+        }
69
+    }
70
+
71
+    // 创建JWT令牌(如果需要)
72
+    public String createToken(Authentication authentication) {
73
+        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
74
+
75
+        byte[] keyBytes = Decoders.BASE64.decode(secret);
76
+        SecretKey key = Keys.hmacShaKeyFor(keyBytes);
77
+
78
+        return Jwts.builder()
79
+                .setSubject(loginUser.getUsername())
80
+                .claim("user_id", loginUser.getUserId())
81
+                .claim("authorities", loginUser.getAuthorities().stream()
82
+                        .map(GrantedAuthority::getAuthority)
83
+                        .collect(Collectors.toList()))
84
+                .setIssuedAt(new Date())
85
+                .setExpiration(new Date(System.currentTimeMillis() + expiration))
86
+                .signWith(key)
87
+                .compact();
16 88
     }
17 89
 }

+ 8
- 1
RuoYi-Vue/ruoyi-system/src/main/java/com/ruoyi/novel/service/impl/AuthServiceImpl.java Переглянути файл

@@ -16,8 +16,15 @@ public class AuthServiceImpl implements IAuthService {
16 16
 
17 17
     @Autowired
18 18
     private UserRepository userRepository;
19
+    //@Autowired
20
+    //private TokenService tokenService;
21
+
22
+    private final TokenService tokenService;
23
+
19 24
     @Autowired
20
-    private TokenService tokenService;
25
+    public AuthServiceImpl(TokenService tokenService) {
26
+        this.tokenService = tokenService;
27
+    }
21 28
 
22 29
     @Override
23 30
     public LoginUser getLoginUser(String token) {

+ 13
- 21
RuoYi-Vue/ruoyi-system/src/main/java/com/ruoyi/novel/service/impl/NovelServiceImpl.java Переглянути файл

@@ -8,10 +8,7 @@ import com.ruoyi.common.utils.DateUtils;
8 8
 import com.ruoyi.common.utils.PageUtils;
9 9
 import com.ruoyi.common.utils.StringUtils;
10 10
 import com.ruoyi.novel.config.TableSupport;
11
-import com.ruoyi.novel.domain.AuthorApplication;
12
-import com.ruoyi.novel.domain.AuthorApplicationDTO;
13
-import com.ruoyi.novel.domain.Chapter;
14
-import com.ruoyi.novel.domain.Novel;
11
+import com.ruoyi.novel.domain.*;
15 12
 import com.ruoyi.novel.mapper.AuthorApplicationMapper;
16 13
 import com.ruoyi.novel.mapper.CategoryMapper;
17 14
 import com.ruoyi.novel.mapper.ChapterMapper;
@@ -19,11 +16,9 @@ import com.ruoyi.novel.mapper.NovelMapper;
19 16
 import com.ruoyi.novel.service.*;
20 17
 import com.ruoyi.system.mapper.SysUserMapper;
21 18
 import jdk.jfr.Category;
22
-import org.elasticsearch.client.security.user.User;
23 19
 import org.slf4j.Logger;
24 20
 import org.slf4j.LoggerFactory;
25 21
 import org.springframework.beans.factory.annotation.Autowired;
26
-import org.springframework.data.domain.PageRequest;
27 22
 import org.springframework.stereotype.Service;
28 23
 import org.springframework.transaction.annotation.Transactional;
29 24
 import org.springframework.web.multipart.MultipartFile;
@@ -31,7 +26,6 @@ import org.springframework.web.multipart.MultipartFile;
31 26
 import java.io.BufferedReader;
32 27
 import java.io.IOException;
33 28
 import java.io.InputStreamReader;
34
-import java.nio.charset.StandardCharsets;
35 29
 import java.util.*;
36 30
 
37 31
 // NovelServiceImpl.java
@@ -230,7 +224,8 @@ public class NovelServiceImpl implements NovelService {
230 224
 
231 225
     @Override
232 226
     public List<Category> getAllCategories() {
233
-        return categoryMapper.selectCategoryList(new Category());
227
+
228
+        return categoryMapper.selectCategoryList();
234 229
     }
235 230
 
236 231
     @Override
@@ -242,7 +237,7 @@ public class NovelServiceImpl implements NovelService {
242 237
     }
243 238
 
244 239
     @Override
245
-    public TableDataInfo getNovelsByCategory(Long categoryId) {
240
+    public List<Novel> getNovelsByCategory(Long categoryId) {
246 241
         // 使用若依分页工具
247 242
         startPage();
248 243
 
@@ -252,7 +247,7 @@ public class NovelServiceImpl implements NovelService {
252 247
         }
253 248
 
254 249
         List<Novel> list = novelMapper.selectNovelList(novel);
255
-        return getDataTable(list);
250
+        return (List<Novel>) getDataTable(list);
256 251
     }
257 252
 
258 253
     @Override
@@ -343,20 +338,17 @@ public class NovelServiceImpl implements NovelService {
343 338
         return novelMapper.selectById(id);
344 339
     }
345 340
 
346
-    @Override
347
-    public void processNovelUpload(NovelUploadDTO dto) {
348
-
349
-    }
341
+//    @Override
342
+//    public void processNovelUpload(NovelUploadDTO dto) {
343
+//
344
+//    }
350 345
 
351
-    @Override
352
-    public void processChapterUpload(ChapterUploadDTO dto) {
346
+//    @Override
347
+//    public void processChapterUpload(ChapterUploadDTO dto) {
348
+//
349
+//    }
353 350
 
354
-    }
355 351
 
356
-    @Override
357
-    public List<AuthorApplication> getAuthorApplications() {
358
-        return null;
359
-    }
360 352
 
361 353
     // 其他CRUD方法...
362 354
 }

Завантаження…
Відмінити
Зберегти