Browse Source

tj

master
fzzj 9 months ago
parent
commit
a069495548

+ 2
- 1
RuoYi-Vue/pom.xml View File

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

+ 4
- 0
RuoYi-Vue/ruoyi-framework/pom.xml View File

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

+ 8
- 12
RuoYi-Vue/ruoyi-system/pom.xml View File

74
             <artifactId>guava</artifactId>
74
             <artifactId>guava</artifactId>
75
             <version>33.4.0-jre</version> <!-- 使用较新版本 -->
75
             <version>33.4.0-jre</version> <!-- 使用较新版本 -->
76
         </dependency>
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
         <dependency>
81
         <dependency>
82
             <groupId>org.aspectj</groupId>
82
             <groupId>org.aspectj</groupId>
83
             <artifactId>aspectjweaver</artifactId>
83
             <artifactId>aspectjweaver</artifactId>
86
             <groupId>org.springframework.boot</groupId>
86
             <groupId>org.springframework.boot</groupId>
87
             <artifactId>spring-boot-starter-data-jpa</artifactId>
87
             <artifactId>spring-boot-starter-data-jpa</artifactId>
88
         </dependency>
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
         <dependency>
93
         <dependency>
94
             <groupId>org.testng</groupId>
94
             <groupId>org.testng</groupId>
95
             <artifactId>testng</artifactId>
95
             <artifactId>testng</artifactId>
96
             <version>RELEASE</version>
96
             <version>RELEASE</version>
97
             <scope>compile</scope>
97
             <scope>compile</scope>
98
         </dependency>
98
         </dependency>
99
-        <dependency>
100
-            <groupId>com.ruoyi</groupId>
101
-            <artifactId>ruoyi-framework</artifactId>
102
-        </dependency>
103
     </dependencies>
99
     </dependencies>
104
 
100
 
105
 </project>
101
 </project>

+ 6
- 0
RuoYi-Vue/ruoyi-system/src/main/java/com/ruoyi/novel/config/JwtFilter.java View File

2
 
2
 
3
 // 新增 JwtFilter.java
3
 // 新增 JwtFilter.java
4
 import org.springframework.security.core.context.SecurityContextHolder;
4
 import org.springframework.security.core.context.SecurityContextHolder;
5
+import org.springframework.util.StringUtils;
5
 import org.springframework.web.filter.OncePerRequestFilter;
6
 import org.springframework.web.filter.OncePerRequestFilter;
6
 import javax.servlet.FilterChain;
7
 import javax.servlet.FilterChain;
7
 import javax.servlet.ServletException;
8
 import javax.servlet.ServletException;
33
 
34
 
34
     private String resolveToken(HttpServletRequest request) {
35
     private String resolveToken(HttpServletRequest request) {
35
         // 从Header提取Token的逻辑
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 View File

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 View File

12
 import io.jsonwebtoken.SignatureAlgorithm;
12
 import io.jsonwebtoken.SignatureAlgorithm;
13
 import org.springframework.beans.factory.annotation.Autowired;
13
 import org.springframework.beans.factory.annotation.Autowired;
14
 import org.springframework.beans.factory.annotation.Value;
14
 import org.springframework.beans.factory.annotation.Value;
15
+import org.springframework.security.core.Authentication;
15
 import org.springframework.stereotype.Component;
16
 import org.springframework.stereotype.Component;
16
 
17
 
17
 import javax.servlet.http.HttpServletRequest;
18
 import javax.servlet.http.HttpServletRequest;
126
     private String getTokenKey(String uuid) {
127
     private String getTokenKey(String uuid) {
127
         return CacheConstants.LOGIN_TOKEN_KEY + uuid;
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 View File

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

+ 4
- 0
RuoYi-Vue/ruoyi-system/src/main/java/com/ruoyi/novel/domain/ChapterUploadDTO.java View File

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 View File

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 View File

8
 // CategoryMapper.java
8
 // CategoryMapper.java
9
 @Mapper
9
 @Mapper
10
 public interface CategoryMapper {
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 View File

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 View File

6
 import com.ruoyi.novel.domain.Novel;
6
 import com.ruoyi.novel.domain.Novel;
7
 import jdk.jfr.Category;
7
 import jdk.jfr.Category;
8
 import org.springframework.transaction.annotation.Transactional;
8
 import org.springframework.transaction.annotation.Transactional;
9
+import org.springframework.web.multipart.MultipartFile;
9
 
10
 
10
 import java.util.List;
11
 import java.util.List;
11
 
12
 
18
     int deleteNovelByIds(Long[] ids);
19
     int deleteNovelByIds(Long[] ids);
19
     Novel selectNovelById(Long id);
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
     void approveAuthorApplication(Long applicationId);
25
     void approveAuthorApplication(Long applicationId);
25
     void rejectAuthorApplication(Long applicationId);
26
     void rejectAuthorApplication(Long applicationId);
26
 
27
 
29
     List<Novel> getHotNovels();
30
     List<Novel> getHotNovels();
30
     List<Novel> getNovelsByCategory(Long categoryId);
31
     List<Novel> getNovelsByCategory(Long categoryId);
31
     List<Chapter> getChaptersByNovelId(Long novelId);
32
     List<Chapter> getChaptersByNovelId(Long novelId);
32
-    String getChapterContent(Long chapterId);
33
+    Chapter getChapterContent(Long chapterId);
33
     void submitAuthorApplication(AuthorApplicationDTO dto, Long userId);
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 View File

1
 package com.ruoyi.novel.service;
1
 package com.ruoyi.novel.service;
2
 
2
 
3
 import com.ruoyi.common.core.domain.model.LoginUser;
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
 import org.springframework.stereotype.Service;
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
 @Service
18
 @Service
7
 public class TokenService {
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
     public LoginUser getLoginUser(String token) {
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
     public Long parseUserId(String token) {
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 View File

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

+ 13
- 21
RuoYi-Vue/ruoyi-system/src/main/java/com/ruoyi/novel/service/impl/NovelServiceImpl.java View File

8
 import com.ruoyi.common.utils.PageUtils;
8
 import com.ruoyi.common.utils.PageUtils;
9
 import com.ruoyi.common.utils.StringUtils;
9
 import com.ruoyi.common.utils.StringUtils;
10
 import com.ruoyi.novel.config.TableSupport;
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
 import com.ruoyi.novel.mapper.AuthorApplicationMapper;
12
 import com.ruoyi.novel.mapper.AuthorApplicationMapper;
16
 import com.ruoyi.novel.mapper.CategoryMapper;
13
 import com.ruoyi.novel.mapper.CategoryMapper;
17
 import com.ruoyi.novel.mapper.ChapterMapper;
14
 import com.ruoyi.novel.mapper.ChapterMapper;
19
 import com.ruoyi.novel.service.*;
16
 import com.ruoyi.novel.service.*;
20
 import com.ruoyi.system.mapper.SysUserMapper;
17
 import com.ruoyi.system.mapper.SysUserMapper;
21
 import jdk.jfr.Category;
18
 import jdk.jfr.Category;
22
-import org.elasticsearch.client.security.user.User;
23
 import org.slf4j.Logger;
19
 import org.slf4j.Logger;
24
 import org.slf4j.LoggerFactory;
20
 import org.slf4j.LoggerFactory;
25
 import org.springframework.beans.factory.annotation.Autowired;
21
 import org.springframework.beans.factory.annotation.Autowired;
26
-import org.springframework.data.domain.PageRequest;
27
 import org.springframework.stereotype.Service;
22
 import org.springframework.stereotype.Service;
28
 import org.springframework.transaction.annotation.Transactional;
23
 import org.springframework.transaction.annotation.Transactional;
29
 import org.springframework.web.multipart.MultipartFile;
24
 import org.springframework.web.multipart.MultipartFile;
31
 import java.io.BufferedReader;
26
 import java.io.BufferedReader;
32
 import java.io.IOException;
27
 import java.io.IOException;
33
 import java.io.InputStreamReader;
28
 import java.io.InputStreamReader;
34
-import java.nio.charset.StandardCharsets;
35
 import java.util.*;
29
 import java.util.*;
36
 
30
 
37
 // NovelServiceImpl.java
31
 // NovelServiceImpl.java
230
 
224
 
231
     @Override
225
     @Override
232
     public List<Category> getAllCategories() {
226
     public List<Category> getAllCategories() {
233
-        return categoryMapper.selectCategoryList(new Category());
227
+
228
+        return categoryMapper.selectCategoryList();
234
     }
229
     }
235
 
230
 
236
     @Override
231
     @Override
242
     }
237
     }
243
 
238
 
244
     @Override
239
     @Override
245
-    public TableDataInfo getNovelsByCategory(Long categoryId) {
240
+    public List<Novel> getNovelsByCategory(Long categoryId) {
246
         // 使用若依分页工具
241
         // 使用若依分页工具
247
         startPage();
242
         startPage();
248
 
243
 
252
         }
247
         }
253
 
248
 
254
         List<Novel> list = novelMapper.selectNovelList(novel);
249
         List<Novel> list = novelMapper.selectNovelList(novel);
255
-        return getDataTable(list);
250
+        return (List<Novel>) getDataTable(list);
256
     }
251
     }
257
 
252
 
258
     @Override
253
     @Override
343
         return novelMapper.selectById(id);
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
     // 其他CRUD方法...
353
     // 其他CRUD方法...
362
 }
354
 }

Loading…
Cancel
Save