尝试在空上下文对象上调用方法isAdmin()

时间:2018-08-29 17:56:16

标签: java spring thymeleaf

你好,我遇到了无法解决的问题,我想寻求帮助,这是错误

  

org.attoparser.ParseException:评估SpringEL表达式的异常:“ user.isAdmin()或user.isAuthor(article)”(模板:“ article / details”-第16行,第73行)

     

org.springframework.expression.spel.SpelEvaluationException:EL1011E:方法调用:尝试在空上下文对象上调用方法isAdmin()

实体User.java

    @Transient
    public boolean isAdmin(){
        return this.getRoles()
                .stream()
                .anyMatch(role -> role.getName().equals("ROLE_ADMIN"));
    }

    @Transient
    public boolean isAuthor(Article article) {
        return Objects.equals(this.getId(),
                article.getAuthor().getId());
    }

错误的html文件

       <th:block sec:authorize="isAuthenticated()" th:if="${user.isAdmin() OR user.isAuthor(article)}">
       <a class="btn btn-success btn-xs" th:href="@{/article/edit/{id}(id = ${article.id})}">Edit</a>
       <a class="btn btn-danger btn-xs" th:href="@{/article/delete/{id}(id = ${article.id})}">Delete</a>
       </th:block>

这是我负责文章详细信息的控制器

@GetMapping("/article/{id}")
    public String details(Model model, @PathVariable Integer id){

        if(!this.articleRepository.existsById(id)){
            return "redirect:/";
        }

        if(!(SecurityContextHolder.getContext().getAuthentication()
                instanceof AnonymousAuthenticationToken)){
            UserDetails principal = (UserDetails) SecurityContextHolder.getContext()
                    .getAuthentication().getPrincipal();
            User entityUser = this.userRepository.findByEmail(principal.getUsername());

            model.addAttribute("user", entityUser);
        }

        Article article = this.articleRepository.getOne(id);

        model.addAttribute("article", article);
        model.addAttribute("view", "article/details");


        return "base-layout";
    }

0 个答案:

没有答案