xiongzhu hace 4 años
padre
commit
0085b1404b

+ 1 - 0
src/main/java/com/izouma/nineth/security/WebSecurityConfig.java

@@ -84,6 +84,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
                 .antMatchers("/collection/all").permitAll()
                 .antMatchers("/collection/all").permitAll()
                 .antMatchers("/collection/get/**").permitAll()
                 .antMatchers("/collection/get/**").permitAll()
                 .antMatchers("/user/all").permitAll()
                 .antMatchers("/user/all").permitAll()
+                .antMatchers("/user/get/*").permitAll()
                 // all other requests need to be authenticated
                 // all other requests need to be authenticated
                 .anyRequest().authenticated().and()
                 .anyRequest().authenticated().and()
                 // make sure we use stateless session; session won't be used to
                 // make sure we use stateless session; session won't be used to

+ 6 - 2
src/main/java/com/izouma/nineth/service/CollectionService.java

@@ -17,6 +17,7 @@ import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageImpl;
 import org.springframework.data.domain.PageImpl;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.List;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
 
 
@@ -49,10 +50,13 @@ public class CollectionService {
     }
     }
 
 
     public List<CollectionDTO> toDTO(List<Collection> collections) {
     public List<CollectionDTO> toDTO(List<Collection> collections) {
+        List<Like> likes = new ArrayList<>();
+        if (SecurityUtils.getAuthenticatedUser() != null) {
+            likes.addAll(likeRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId()));
+        }
         return collections.stream().parallel().map(collection -> {
         return collections.stream().parallel().map(collection -> {
             CollectionDTO dto = toDTO(collection, false);
             CollectionDTO dto = toDTO(collection, false);
-            if (SecurityUtils.getAuthenticatedUser() != null) {
-                List<Like> likes = likeRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId());
+            if (!likes.isEmpty()) {
                 dto.setLiked(likes.stream().anyMatch(l -> l.getCollectionId().equals(collection.getId())));
                 dto.setLiked(likes.stream().anyMatch(l -> l.getCollectionId().equals(collection.getId())));
             }
             }
             return dto;
             return dto;

+ 5 - 2
src/main/java/com/izouma/nineth/service/UserService.java

@@ -275,10 +275,13 @@ public class UserService {
     }
     }
 
 
     public List<UserDTO> toDTO(List<User> users) {
     public List<UserDTO> toDTO(List<User> users) {
+        List<Follow> follows = new ArrayList<>();
+        if (SecurityUtils.getAuthenticatedUser() != null) {
+            follows.addAll(followRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId()));
+        }
         return users.stream().parallel().map(user -> {
         return users.stream().parallel().map(user -> {
             UserDTO dto = toDTO(user, false);
             UserDTO dto = toDTO(user, false);
-            if (SecurityUtils.getAuthenticatedUser() != null) {
-                List<Follow> follows = followRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId());
+            if (!follows.isEmpty()) {
                 dto.setFollow(follows.stream().anyMatch(f -> f.getFollowUserId().equals(user.getId())));
                 dto.setFollow(follows.stream().anyMatch(f -> f.getFollowUserId().equals(user.getId())));
             }
             }
             return dto;
             return dto;

+ 1 - 1
src/main/java/com/izouma/nineth/web/UserController.java

@@ -87,7 +87,7 @@ public class UserController extends BaseController {
         return userService.toDTO(userService.all(pageQuery));
         return userService.toDTO(userService.all(pageQuery));
     }
     }
 
 
-    @PreAuthorize("hasRole('ADMIN')")
+//    @PreAuthorize("hasRole('ADMIN')")
     @GetMapping("/get/{id}")
     @GetMapping("/get/{id}")
     public User get(@PathVariable Long id) {
     public User get(@PathVariable Long id) {
         return userRepo.findById(id).orElseThrow(new BusinessException("无记录"));
         return userRepo.findById(id).orElseThrow(new BusinessException("无记录"));