java - 如何在没有身份验证的情况下更新字段?

标签 java spring spring-boot spring-security spring-data

如何在更新计数器时不更新审计员?

在我的数据库中,我有一个名为产品的表,我想实现一个访问计数器。这意味着任何未经身份验证的人都可以访问该产品。基于此,我收到以下错误,因为它正在尝试更新审核员(匿名):

java.lang.ClassCastException: java.lang.String cannot be cast to bla.bla.bla.UserEntity

是否有任何注释告诉 spring 该属性可以在没有身份验证的情况下更新?

一些代码:

@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "Products")
public class ProductEntity extends Auditable<UserEntity> {

...

@NotNull
private Long visit = 0L;

... getters and setters ...
}

在我的 Controller 中:

@Controller
@RequestMapping(value="/product")
public class productController {

    @Autowired
    ProdutoRepository produtoRepository;

    @RequestMapping(value = "/{idProduct}", method=RequestMethod.GET)
    public ModelAndView findProduct(
            @PathVariable(value="idProduct") int produtoRepository,
            RedirectAttributes redirectAttributes,
            Model model){


        ProductEntity p = produtoRepository.findOne(produtoRepository);

        p.setVisit(p.getVisit() + 1);
        produtoRepository.save(p);

        ...
    }

    ...
}

最佳答案

为了回答,我创建了一个新的访问表,没有审计员并且工作完美。

关于java - 如何在没有身份验证的情况下更新字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47081964/

相关文章:

java - 简单但复杂的 HQL/SQL 查询

java - Spring Condition 无法从属性文件中读取值

java - @ControllerAdvice 在处理异常方面比 @ExceptionHandler 或 HandlerExceptionResolver 有什么优势?

java - 在 Spring Boot 应用程序中使用 @Bean 声明 bean

gradle - Spring Boot Vaadin静态内容

java - 如何在weblogic.xml中设置cookie Samesite属性?

java - 从代码创建 JAR 文件并调用函数

java - 使用java将数据写入顺序文本文件

java - 由于未提供多部分配置,无法处理部分

rest - 无法从另一个 docker 容器访问 docker 容器中的 REST 端点