我有一个提供 REST API 的 Spring Boot 应用程序。所有 API 都使用 Spring Security 进行保护。我还使用@PreAuthorize注释添加了方法授权。
对于本地开发,我想通过配置或其他方式完全禁用安全性。我想禁用身份验证和授权,以便我可以轻松调用 API,而不必每次调用 API 时都获取新令牌。
禁用身份验证很容易,我只是将其添加到配置方法中,一切都很好。
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/api/**");
}
Run Code Online (Sandbox Code Playgroud)
但这会导致AuthenticationCredentialsNotFoundException在点击我从身份验证中排除的端点时发生,这是有道理的。仅当我删除@PreAuthorize注释时,此异常才会消失,这显然在我即将进行一些本地开发工作时不想这样做。似乎只是通过对方法进行注释,Spring AOP 会启动并检查 Spring Security Context 中的身份验证对象,并且无法禁用它而不是删除注释。
如何让 Spring完全忽略@PreAuthorize注释?我尝试删除@EnableGlobalMethodSecurity但它对异常没有帮助。
我有从网络表单接收和上传文件的控制器方法。如何从 FilePart 中提取字节数组并将其保存到数据库?
我可以通过使用 FilePart.transferTo() 将 FilePart 保存到文件中来完成此操作,但这看起来又慢又难看。有更好的方法吗?
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.web.bind.annotation.*;
Mono<UploadResult> uploadFile(@RequestParam("files") FilePart file){
byte[] fileAsByteArray = convertFilePartToByteArray(file);
fileService.saveByteArrayToDB(fileAsByteArray);
/* Rest of the method */
}
Run Code Online (Sandbox Code Playgroud)