Tag: 春天启动

如何在基于Spring的强类型语言中正确地使用PATCH – example

据我所知: PUT – 用其整个表示更新对象(替换) 修补程序 – 仅使用给定字段更新对象(更新) 我正在使用Spring来实现一个非常简单的HTTP服务器。 当用户想要更新他的数据时,他需要为某个端点(比如: api/user )创建一个HTTP PATCH 。 他的请求体通过@RequestBody映射到DTO,如下所示: class PatchUserRequest { @Email @Length(min = 5, max = 50) var email: String? = null @Length(max = 100) var name: String? = null … } 然后我使用这个类的一个对象来更新(修补)用户对象: fun patchWithRequest(userRequest: PatchUserRequest) { if (!userRequest.email.isNullOrEmpty()) { email = userRequest.email!! } if (!userRequest.name.isNullOrEmpty()) { name […]

Spring Security,Method Security注解(@Secured)不工作(java config)

我正在尝试使用@Secured(“ADMIN”)(没有任何XML,只有java配置,Spring Boot)来设置方法安全注释。 但通过角色访问不起作用。 安全配置: @Configuration @EnableWebSecurity public class AppSecurityConfiguration extends WebSecurityConfigurerAdapter{ ….. @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/api/**").fullyAuthenticated().and() .addFilterBefore(tokenAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class); } ….. } 我想限制访问控制器的方法: @RestController @RequestMapping("/api/groups") public class GroupController { @Autowired private GroupService groupService; @Secured("ADMIN") @RequestMapping public List<Group> list() { return groupService.findAll(); } } 通过url限制访问正在工作,与: .antMatchers("/api/**").hasAuthority("ADMIN") 也许我忘了指定,我想限制的角色? UPD:按照规则,在控制层或服务层中的哪个层必须是@PreAuthorize("hasRole('ADMIN')") ?

如何禁用RepositoryRestHandlerMapping和EndpointHandlerMapping?

我目前正在使用Spring Boot,Hibernate和Spring-HATEOAS构建一个带有REST接口的应用程序。 我的数据模型被定义为具有@Entity批注的bean,我使用Spring的特性来自动设置一个Hibernate仓库(创建一个扩展PagingAndSortingRepository的界面)。 我的应用程序完全是注解驱动的,也就是说,我没有web.xml但是使用@Configuration @Bean , @Configuration @Bean等Spring注解来配置所有的东西,并且在SpringApplication.run(MyApp.class, args);的帮助下从我的main方法启动应用程序SpringApplication.run(MyApp.class, args); 这工作正常,但通过这种方法,创建了一个RepositoryRestHandlerMapping和EndpointHandlerMapping 。 这些创造了一堆我既不需要也不想要的资源。 我实现我自己的控制器,因为他们需要做比标准的逻辑更多。 我怎样才能防止这种默认行为,并禁用这些映射?

相同的其他端点具有不同的PathVariable

我试图做两个相同的uri,但不同类型的休息端点。 第一个将通过EAN(Int)进行搜索,第二个将通过id(String)进行搜索。 我能否以某种方式超载端点? 我和Kotlin一起使用Spring Boot @GetMapping("/book/{ean}") fun getABookByEan(@PathVariable ean: Int) : ResponseEntity<*> { repository.getByEan(ean)?.let { return ResponseEntity.status(HttpStatus.OK).body(it) } throw ItemNotFoundException() } @GetMapping("/book/{id}") fun getABookById(@PathVariable id: String) : ResponseEntity<*> { repository.getById(id)?.let { return ResponseEntity.status(HttpStatus.OK).body(it) } throw ItemNotFoundException() } 在此之后,我得到了一个异常,多个方法被映射为相同的端点。 NestedServletException:请求处理失败; 嵌套异常是java.lang.IllegalStateException:为HTTP路径映射的模糊处理程序方法…

Kotlin + SpringBoot 2.0.0-M4失败tu加载应用程序上下文(BeanCreationException)

我正在与Kotlin和Spring(通过SpringBoot)合作。 我在2.0.0-M3,一切正常。 我决定跳到M4。 然后,我有一些麻烦。 我得到了以下堆栈跟踪: java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125) at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:107) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:242) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at […]