Spring数据REST:自定义方法验证
我正在尝试使用Spring Data REST用@RepositoryRestResource注释的存储库以及实现自定义方法. 有2种情况: 1)我有@RepositoryRestResource注释的REST存储库,该存储库被映射到/users端点.另外,我的@RestController映射到相同的端点. 在@RepositoryRestResource中导致方法(应公开)不可见,并获得405结果. 使用@Valid注释的方法验证正在使用@RestController方法.例如这有效: @ResponseBody @RequestMapping(value = "/users") public ResponseEntity signUp(@RequestBody @Valid final UserSignUpRequest userSignUpRequest) 2)与REST存储库一起工作的控制器是@RepositoryRestController控制器.这样,@Re
4 2023-11-12
编程技术问答社区
如何在春季设置RepositoryRestController的单元测试?
我对A RepositoryRestController(MyController类)的单元测试具有以下配置: @RunWith(SpringRunner.class) @WebMvcTest public class MyTest { @Configuration @EnableSpringDataWebSupport static class TestConfiguration { @Bean MyController myController() { //controller construction } } } 我在网上找不到任何文档,可以设置哪种配置才能正确运行测试.当我在正在测试的控制器方法上注入Pageable实例时,我才偶然发现了EnableSpringDataWebSupport.现在,我的问题是PersistentEntityResourceAsse
2 2023-11-07
编程技术问答社区
如何使用Spring数据REST和HATEOAS公开完整的树结构?
我有一个JPA树结构 @Entity public class Document { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; private String text; @ManyToOne @JoinColumn(name = "parent") Document parent; @OneToMany(mappedBy = "parent", fetch = FetchType.EAGER) Set children; (getters and setters) } 和一个投影 @Projection(name = "all", types = Document.class) public interface AllDocumentsProjection { int getId(
6 2023-10-26
编程技术问答社区
春季数据休息:"无法从@entityScan配置LocalContainerentityManagerFactoryBean"
我有一个REST Service Spring Boot应用程序,分为两个单独的Maven项目: 第一个myapp-data包括弹簧数据JPA +春季数据rest实体和REST存储库类别定义(我已经在单独的Maven项目中隔离了这些supporties,因为除了我的REST服务以外,我还将它们使用它们). SECON myapp-services是一个弹簧启动应用程序,基本上包含主要方法 +配置,以将上述弹簧数据休息存储库视为REST端点. 如果我在本地开发环境(sts eclipse,在我的本地开发机器上启动mvn spring-boot:run),一切似乎都很好,但是当我将其作为一场战争作为一场舞台上的战争(Ubuntu下的Tomcat7)(Ubuntu下的Tomcat7) )我在下面的框中得到了例外.请注意,除此之外,两个部署的差异仅在于application.properties的不同设置(DB连接,日志记录级别等). 在我进行了一些代码更改之前(例如,在以前的
4 2023-10-24
编程技术问答社区
春季启动集成测试忽略安全= false在Autoconfiguremockmvc注释中,获取401
无法使我的@SpringBootTest工作.它说身份验证已经开始,我不想要. 我已经用@AutoConfigureMockMvc(secure = false) 进行了设置 我提交了一些带有JSON的模拟请求,我的集成测试应测试整个堆栈,将其通过SDR通过Web层进行JPA,然后进入内存数据库,因此我可以使用JdbcTemplate进行测试. . 但是响应是401,需要身份验证.为什么@AutoConfigureMockMvc(secure = false)不够?缺少什么? @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = { TestDataSourceConfig.class }) @EnableAutoConfiguration @AutoConfigureMock
6 2023-10-15
编程技术问答社区
春季数据休息端点未在Swagger UI中生成
我已经使用@BasePathAwareController实现了一个控制器,该控制器还利用弹簧数据休息(为了发现露出排序/大小等) 以及一些自定义端点(用于更新等).该应用程序正常工作,端点弹簧 数据休息生成的工作正常正常,我可以看到响应中出现的自我链接,但是,我看不到 这些端点在Swagger UI中.我只能看到我的自定义终点 在我的控制器中定义. 根据此 post 需要与@EnableSwagger2WebMvc 一起使用Swagger 3.0.0-SNAPSHOT 这是我的配置: 我的app.yml: spring: data: rest: basePath: /api/v1 我的POM文件: org.springframework.boot spring-boot-starter-parent
6 2023-10-06
编程技术问答社区
Swagger未检测到使用弹簧数据休息构建的API
我正在使用Swagger在春季启动应用程序中为我的API生成文档,我正在使用Spring Data Rest生成API,但是当我运行应用程序时,我会收到Swagger Message:未定义的操作在规格! 中 这是我的API代码: @Api(tags = "projets") @RepositoryRestResource(collectionResourceRel = "projets", path = "projets") public interface IProjectRepository extends JpaRepository { } 这是我的配置文件: @Configuration @EnableSwagger2 public class QfactoryConfiguration { @Bean public Docket getDocketInstance() { return
4 2023-10-06
编程技术问答社区
处理交易中的Spring-DATA-Rest应用程序事件
当数据更新时,我需要通过JMS将通知事件发布给外部系统.与对象合作到数据库的同一交易中进行的ID以确保完整性. . 弹簧data-rest发出的应用程序插曲事件似乎是实现此逻辑的逻辑场所. @org.springframework.transaction.annotation.Transactional public class TestEventListener extends AbstractRepositoryEventListener { private static final Logger LOG = LoggerFactory.getLogger(TestEventListener.class); @Override protected void onBeforeCreate(Object entity) { LOG.info("XXX before create"); } @Ove
4 2023-09-15
编程技术问答社区
Spring Boot @WebIntegrationTest和TestRestTemplate-是否可以回滚测试事务?
我有一个带有Spring Data Rest的Spring Boot应用程序,我将@WebIntegrationTest与TestRestTemplate一起使用.测试的基类看起来像这样: @RunWith(SpringJUnit4ClassRunner.class) @ActiveProfiles(profiles = "test") @SpringApplicationConfiguration(classes = Application.class) @Transactional @TransactionConfiguration @WebIntegrationTest("server.port: 0") public abstract class IntegrationTest { ... } 我正在通过使用TestRestTemplate对资源执行POST请求来测试实体的创建.问题在于,即使认为我的测试配置为交易,数据库上的实体的交易也没有回滚,因此该实体在
6 2023-09-15
编程技术问答社区
为什么@PreAuthorize与hasPermission(在自定义PermissionEvaluator中)在一个存储库中被触发,而在另一个存储库中没有被触发?
出于某种奇怪的原因,我的@preatuthorize用haspermission表达式的注释是从我的一个存储库中触发的,而不是另一个几乎相同的. 只是为了首先解决这个问题,我使用prePostEnabled = true启用了GlobalMethodSecurity.我还写了一个自定义的许可证. import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler; import org.springframework.security.access.expression.method.MethodSec
4 2023-09-14
编程技术问答社区
同一实体的两个存储库,一个出口,一个不出口
使用SRING数据JPA,Spring Data Rest 2.4.2,Spring Security和Spring Boot 1.3.1.我有一个帐户实体,我想暴露于管理目的: @PreAuthorize("hasRole('ROLE_ADMIN')") //exclusive admin access public interface AccountRepository extends JpaRepository {} 这可以按预期工作,我可以访问具有适当的管理员角色的REST接口. 我的另一个要求是允许非Admin用户通过HTTP进行注册和身份验证.为此,我创建了一个自定义控制器,该自定义控制器揭示了/注册和/登录资源的登录()和登录()功能.问题是,当注册/登录内部逻辑与上述存储库相互作用时,除了匿名的一个. 外,没有用户安全上下文. 为了使事情保持简单,我创建了一个未导出且没有安全要求的第二个存储库: @Reposito
10 2023-09-14
编程技术问答社区
在JpaRepository上的@PreAuthorize
我希望为我的休息服务实施基于角色的安全性.我正在使用Spring-data-rest,并因此配置了JpaRepository: @Repository @RestResource(path = "changesets", rel = "changesets") public interface ChangesetRepository extends JpaRepository { } 我想将@PreAuthorize注释附加到继承的Page findAll(Pageable pageable)方法上,以便get需要特定的角色. 有办法做到这一点吗?我需要提供自定义实施还是缺少明显的东西? 解决方案 您可以为所有存储库添加自己的父级(请参阅如何在文档中进行操作).然后,只需添加所有必要的注释,您的安全限制将用于所有儿童豆. 从架构的角度来看,大多数时候,存储库不是应用您的安全限制的正确位置.您的服务层更合适的位置(因为您
90 2023-09-14
编程技术问答社区
基于用户原则的Spring数据仓库访问控制
我正在尝试实施精细的谷物访问控制,同时仍利用春季数据休息. 我正在努力保护CrudRepository,因此用户只能修改或插入属于它们的数据.我正在利用@PreAuthorize/@PostAuthorize和@PreFilter/@PostFilter锁定访问当前主体. 到目前为止,我的存储库看起来像这样. public interface MyRepository extends CrudRepository { @PreAuthorize("#entity.userId == principal.id") @Override S save(S entity); @PreFilter("filterObject.userId === principal.id") @Override Iterab
6 2023-09-14
编程技术问答社区
Spring Data Rest:返回用户资源
我正在创建一个带有Spring Boot和Spring Data Rest的简单Crud-App(购物清单).我有一个资源:ShoppingItem.是否有一种简单的方法可以返回发送请求的用户的资源? (多个用户支持)因此,用户只能获取自己的购物标题,而不是每个购物者. 还是我必须自己实现一个控制器,我在哪里? 我找到了基于用户的弹簧数据休息滤波数据这种基于用户过滤资源的方法,但这对我的存储库端点没有帮助. 预先感谢 解决方案 如果您使用的是弹簧安全集成,则可以使用ACL(可能是重型)或简单postFilter喜欢以下: public interface ShoppingItemRepository extends CrudRepository { @PostFilter("filterObject.user.getId() == principal.id") @Override Iterable
4 2023-09-14
编程技术问答社区
Spring Data Rest:基于安全的投影
我正在使用 spring data rest ret 和 spring data jpa 并且具有以下实体: public class User { @Id @GeneratedValue private Long id; private String name; private String password; private String email; ...getter/setter methods... } 我也使用Spring Security. 我的用户存储库: @RepositoryRestResource( collectionResourceRel = "user", path = "user", excerptProjection = UserSimpleProjection.class) public interface UserRepository e
2 2023-09-14
编程技术问答社区
@RestController和@repositoryrestcontroller之间的区别
什么是典型的用例代码显示这两个注释之间的区别 - 含义@RestController和@RepositoryRestController - ? 解决方案 根据注释, RepositoryRestController 是提供仍然利用春季数据休息功能的自定义控制器的方法. 春季数据休息参考指南, 15.6.覆盖弹簧数据休息响应处理程序: 有时您可能需要为特定的自定义处理程序编写一个自定义处理程序 资源.要利用Spring Data Rest的设置,请消息 转换器,异常处理等等 @repositoryRestController注释,而不是标准的弹簧MVC @controller或@restcontroller. 最重要的是,RepositoryRestController意识到弹簧数据休息的基本路径,并将在此基本路径下提供.
4 2023-09-14
编程技术问答社区
如何设置spring-data-rest的默认媒体类型?
来自RepositoryRestConfiguration我可以看到设置spring.data.rest.default-media-type=application/json可以更改@RepositoryRestResource>的默认媒体类型. @SuppressWarnings("deprecation") public class RepositoryRestConfiguration { private MediaType defaultMediaType = MediaTypes.HAL_JSON; } 问题:由于此类在deprecation中,设置/覆盖默认类型的正确方法是什么? 解决方案 您可以通过RepositoryRestConfiguration进行此操作,也可以与应用程序中的属性一起使用.请参阅文档 a>. RepositoryRestConfiguration类未弃用.其中有一些弃用的方法.课堂上的@SuppressWarning
10 2023-09-14
编程技术问答社区