【问题标题】:How to update a field without authentication?如何在没有身份验证的情况下更新字段?
【发布时间】:2018-04-15 08:53:44
【问题描述】:

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

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

java.lang.ClassCastException:java.lang.String 无法转换为 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
@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);

        ...
    }

    ...
}

【问题讨论】:

  • 您面临的问题是关于身份验证,还是关于投射对象?
  • 当我“.save”时,因为审计员 spring 文档,他们根据登录的用户自动插入用户。在这种情况下,未登录用户,因此,登录用户是字符串“匿名”

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2022-11-30
    • 2020-12-08
    • 2022-01-03
    • 2013-04-21
    • 2018-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    相关资源
    最近更新 更多