Spring security Thymeleaf安全性:只有经过授权且用户与文章作者不同时,才能编辑内容
我需要一个用户能够作出评论评级,只有当用户是授权的,用户ID是不是作为审查的作者ID相同。如何添加两个安全定义,其中另一个比较用户ID 这是我的html:Spring security Thymeleaf安全性:只有经过授权且用户与文章作者不同时,才能编辑内容,spring-security,thymeleaf,authorize,Spring Security,Thymeleaf,Authorize,我需要一个用户能够作出评论评级,只有当用户是授权的,用户ID是不是作为审查的作者ID相同。如何添加两个安全定义,其中另一个比较用户ID 这是我的html: <td class="rating-tab"> <span th:text="${ratingToUse}"></span> <form sec:authorize="isAuthenticated()" action="#" th:ac
<td class="rating-tab">
<span th:text="${ratingToUse}"></span>
<form sec:authorize="isAuthenticated()"
action="#"
th:action="@{/addrating}"
method="post" th:object="${newRating}" class="mt-5">
<input value="0" type="hidden" class="rating" data-glyphicon="0"
th:field="*{stars}">
<input type="hidden" id="reviewID" name="reviewID" th:value="${oneReview.reviewId}">
<button class="btn btn-info" type="submit" style="margin-top: 10px;">Submit my rating</button>
</form>
</td>
及
一种简单的方法是使用Thymeleaf控制站点的一部分(如表单)是显示还是隐藏。使用
和
可以有多个条件。在这种情况下,这可能足以满足您的需要。但是,您可能希望阻止未经授权的用户甚至能够在第一时间访问该页面。页面导航规则可能更好地处理这一点,而不是Thymeleaf标记。一种简单的方法是使用Thymeleaf控制站点的一部分(如表单)是显示还是隐藏。使用和
可以有多个条件。在这种情况下,这可能足以满足您的需要。但是,您可能希望阻止未经授权的用户甚至能够在第一时间访问该页面。这可能更好地由页面导航规则来处理,而不是由Thymeleaf标记来处理。
@GetMapping(value = "/review/add/{id}")
public ModelAndView getReviewView(@PathVariable String id, HttpServletRequest request) {
ModelAndView modelAndView = new ModelAndView();
ImdbMovieData thisMovie = imdbAPIService.getOneMovieOnly(id);
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
User customUser = (User)authentication.getPrincipal();
int userId = customUser.getId();
String userName = customUser.getUserName();
modelAndView.addObject("review", new Review(userName, userId));
modelAndView.addObject("thisMovie", thisMovie);
modelAndView.setViewName("add-review");
return modelAndView;
}
@PostMapping(value = "/addrating")
public ModelAndView addRating(@Valid Rating ratingToAdd, BindingResult bindingResult, HttpServletRequest request) {
ModelAndView modelAndView = new ModelAndView();
Review thisReview = reviewService.findByReviewID(ratingToAdd.getReviewID());
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
User customUser = (User)authentication.getPrincipal();
int userId = customUser.getId();
if(userId == thisReview.getUserId()){
modelAndView.setViewName("redirect:read-review/" + ratingToAdd.getReviewID());
return modelAndView;
}
if (!bindingResult.hasErrors()) {
ratingService.saveRating(ratingToAdd);
}
Review reviewToUpdate = reviewService.findByReviewID(ratingToAdd.getReviewID());
if (reviewToUpdate.totalRatingCount == null && reviewToUpdate.totalRatingSum == null) {
reviewToUpdate.totalRatingCount = 1;
reviewToUpdate.totalRatingSum = ratingToAdd.getStars();
}
else {
reviewToUpdate.totalRatingCount++;
reviewToUpdate.totalRatingSum += ratingToAdd.getStars();
}
reviewService.saveReview(reviewToUpdate);
modelAndView.setViewName("redirect:read-review/" + ratingToAdd.getReviewID());
return modelAndView;
}