【问题标题】:Spring boot : Create a User with MongoDB [closed]Spring boot:使用 MongoDB 创建用户 [关闭]
【发布时间】:2017-08-13 03:55:45
【问题描述】:

好的,我有一个问题:

如何使用 spring 创建用户?

我学到了一些信息,最好的方法是使用 UserDetails ... 但我找不到要使用良好做法的信息:UserDetails with mongodb。

当我有我的用户详细信息时,如何创建正确的注册/登录休息方法?

我还有一个关于休息安全的问题:

我有 spring 的其余模块,但是如何保护用户的存储库?我的意思是,每个登录的用户只能看到/编辑他的所有者。

使用 rest 模块,您可以使用具有 Spring 安全性的角色保护路径,但您不能将资源分配给用户。 (我的意思是用户详细信息)

我真的需要帮助,因为它是一个框架,我不想重做 spring 所做的,因为它是一个框架......但我是新手!

如果您需要更多详细信息,我可以准确说明您不明白的内容,但我是法国人,所以我的英语并不完美。

【问题讨论】:

    标签: spring mongodb rest spring-boot spring-security


    【解决方案1】:

    我认为this answer 可以帮助您解决问题,即使您不使用 Spring Data MongoDB。

    基本上,您需要实现UserDetailsService 接口并将其注释为@Component@Service bean。

    那你需要自己实现一个UserDetails POJO(例子很多)。

    最后,您需要使用自定义的 UserService 配置 Spring Security,这对于 Spring Boot 来说非常简单。

    因此,如果您还没有使用Spring Boot,我强烈建议您这样做。它使 Spring 开发人员的生活变得如此轻松。

    为了保护您的 REST 端点,您可以创建某种 PermissionService(常规 Spring @Service bean)来检查当前用户是否被允许访问/编辑资源。

    例子:

    @Slf4j
    @Service
    public class PermissionService {
    
        public boolean hasPermission(String userId, String resourceId) {
            log.debug("Checking permission for [{}] on resource [{}]", userId, resourceId);
    
            hasText(userId);
            hasText(resourceId);
    
            return resourceId == 1;
        }
    }
    

    在任何 @RestController 中,您现在可以添加 Spring Security 注释并使用 SPEL(Spring 表达式语言)来评估条件:

    @RestController
    @RequestMapping("/api/resource")
    public class ResourceController {
    
        @GetMapping("/{id}")
        @PreAuthorize("@permissionService.hasPermission(userDetails.userId, resourceId)")
        public ResponseEntity findResource(@PathVariable("id") String resourceId, @MyUser SecUserDetails userDetails) {
            return ResponseEntity.ok(resourceRepository.findById(resourceId));
        }
    }
    

    现在,你一定在想:这个奇怪的@MyUser 注释是什么?嗯,它是一个自定义注解,可以将当前 Spring Security UserDetails 实例注入到控制器方法中。

    你可以这样定义:

    @Slf4j
    @Component
    public class MyUserMethodArgumentResolver implements HandlerMethodArgumentResolver {
    
        private final UserService userService;
    
        @Autowired
        public MyUserMethodArgumentResolver(UserDetailsService userDetailsService) {
            this.userDetailsService = userDetailsService;
        }
    
        @Override
        public boolean supportsParameter(MethodParameter methodParameter) {
            return methodParameter.getParameterAnnotation(MyUser.class) != null
            && methodParameter.getParameterType().equals(SecUserDetails.class);
        }
    
        @Override
        public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
        WebDataBinderFactory binderFactory) throws Exception {
            if (this.supportsParameter(methodParameter)) {
                return this.userService.getCurrentUser();
            } else {
                return WebArgumentResolver.UNRESOLVED;
            }
        }
    }
    

    因此,每当控制器找到此 @MyUser 注释时,它都会调用此 WebArgumentResolver 来检索当前用户详细信息(SecUserDetails 实例)并将其注入控制器方法中。

    然后可以在PermissionService中使用它来检查访问权限。

    【讨论】:

    • 问题是org.springframework.boot:spring-boot-starter-data-restMongoRepository<User, Long> 自动生成所有资源的休息路径,我不知道如何控制它们! :c
    • 你不一定需要spring-boot-starter-data-rest,你可以使用spring-boot-starter-data (Spring Data) 创建你自己的Repository接口并从MongoRepository扩展。
    猜你喜欢
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 2018-10-17
    • 2022-06-23
    • 2021-09-14
    • 1970-01-01
    • 2020-08-02
    • 2016-06-05
    相关资源
    最近更新 更多