小编Mat*_*llo的帖子

Spring Security 缓存我的身份验证

我刚刚在我的 Spring Boot 项目类路径中添加了 Spring Security。我没有做 Java 配置或 XML 配置。

问题是,当我向资源localhost:8080/users发送请求时,我的第一个请求通常会得到身份验证(通过基本身份验证),但后续请求不需要任何身份验证标头。即使我重新启动服务器,请求仍在进行身份验证而无需输入任何凭据。

我想关闭这个“缓存”。

我尝试了很多客户。Postman, SOAP-UI, browsers..Already read this , but not works

java spring spring-security

8
推荐指数
1
解决办法
6280
查看次数

在调用保存字符串 ID 之前,必须手动分配此类的 ID

已经阅读了很多关于同一问题的问题,但我仍然无法解决这个问题。

String我的数据库需要有一个主键。

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class MyClass {

    @Id
    private String myId;
    private String name;

    // getters and setters..

}
Run Code Online (Sandbox Code Playgroud)

问题是,如果我在带注释的字段String中使用类型@Id,当我尝试保存对象时,Hibernate 会抛出异常。

ids for this class must be manually assigned before calling 
Run Code Online (Sandbox Code Playgroud)

是的,我正在为该字段设置一个值。

我发现的解决方法:

  1. 将注释添加@GeneratedValue到字段 - 不起作用
  2. 将字段类型更改为Integer- 这对我来说不可行
  3. 添加一个接收myId参数的构造函数public MyClass(String myId){ ... }- 不起作用
  4. 使用 UUID - 我不能,因为这个 id 是由 POST 请求中的字段设置的。

这些解决方法都不适合我。

更新

我正在使用 Spring Boot 和 Spring Data JPA。

我如何插入:

我有一个 …

java hibernate jpa spring-data-jpa spring-boot

7
推荐指数
1
解决办法
2万
查看次数

Spring Boot中处理异常的正确方法

我正在阅读 Spring 文档,发现从创建子类ResponseEntityExceptionHandler是处理异常的好方法。但是,我尝试以不同的方式处理异常,因为我需要BusinessExceptionsTechnicalExceptions.

创建了一个BusinessFault封装异常详细信息的 bean :

业务故障.java

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonInclude(value = Include.NON_NULL)
public class BusinessFault {

    @JsonProperty(value = "category")
    private final String CATEGORY = "Business Failure";
    protected String type;
    protected String code;
    protected String reason;
    protected String description;
    protected String instruction;

    public BusinessFault(String type, String code, String reason) {
        this.type = type;
        this.code = code;
        this.reason = reason;
    }

    public BusinessFault(String type, String code, String reason, String description, …
Run Code Online (Sandbox Code Playgroud)

java spring spring-boot spring-restcontroller spring-rest

6
推荐指数
1
解决办法
8189
查看次数

跨不同微服务的数据验证

我已经阅读了很多有关它的主题,但是仍然没有找到更好的方法。

我有一个User。一个User可能有很多Posts。用户和帖子是不同的微服务。我正在使用Spring Boot。

当前端调用我的Posts微服务向发送POST请求时/posts/user/1,我需要检查给定userId(1)是否存在于Users数据库中。如果否,则抛出异常,告知前端用户不存在。如果是,则将给定的请求正文作为插入Post

问题是:我应该如何在后端检查此信息?我们不想让前端承担此责任,因为javascript是客户端,并且恶意用户可以绕过此检查。

选项:

  • 微服务之间的REST通信。(发布微服务呼叫用户微服务询问给定的id是否在他的身边)
  • 向Posts微服务授予对Users微服务数据库的访问权限

我知道它们之间的通信会产生耦合,但是我不确定让Posts访问Users数据库是否是最佳选择。

随时提出任何选择。

java architecture rest spring-boot microservices

6
推荐指数
2
解决办法
1262
查看次数

Spring Boot-@Service类调用另一个@Service类

有一个带@Service注释的类调用另一个带@Service注释的类是否可以?还是不好的做法?

例如。:

@Service
public class MyService {

    // ...

    @Autowired
    private MyOtherService myOtherService;

    // ...

}
Run Code Online (Sandbox Code Playgroud)

java spring-boot

4
推荐指数
2
解决办法
5699
查看次数

HTTP代码应该用于表示业务失败吗?

本周早些时候,我的团队讨论了HTTP代码是否代表业务失败.

想象一下我们有一个CustomerREST API 的场景.在该API中,我们有许多操作,例如:

  • POST - mydomain.com/customers(接收JSON正文并创建新客户)
  • GET - mydomain.com/customers/{id}(搜索特定客户)
  • PATCH - mydomain.com/customers/{id}(接收JSON正文并修补特定客户)
  • 删除 - mydomain.com;customers/{id}(删除特定客户)

现在,想象一下我正在寻找Customer哪种情况id = 5.

没有Customerid = 5.我应该怎么做HTTP状态代码?未找到客户是业务失败.我应该退回404 - 没有找到?我应该返回200 - OK(使用JSON正文描述不存在ID为5的客户)吗?

我们正在讨论这种行为.

Controller.java(示例)

@GetMapping("/customers/{id}")
public ResponseEntity<?> handleRequestOfRetrieveCustomerById(@PathVariable("id") Integer id) throws CustomerNotFoundException {
    try {
        ResponseEntity.ok(customerService.findCustomerById(id));
    } catch(CustomerNotFoundException e) {
        // log at Controller level and rethrow
        throw e;
    }
}
Run Code Online (Sandbox Code Playgroud)

Handler.java(示例)

@ExceptionHandler(BusinessResourceNotFoundException.class)
@ResponseBody
protected ResponseEntity<Fault> …
Run Code Online (Sandbox Code Playgroud)

java api rest spring http

1
推荐指数
1
解决办法
120
查看次数

Spring Boot - 处理NoHandlerFoundException

阅读Spring参考指南,了解如何处理NoHandlerFoundException,并发现Spring默认设置throwExceptionIfNoHandlerFoundfalse.

知道了,我认为将此参数设置为是个好主意true.

我正在使用Spring Boot.

这样做了:

MyConfig.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

@SpringBootApplication
public class MyConfig {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(MyConfig.class, args);
        context.getBean(DispatcherServlet.class).setThrowExceptionIfNoHandlerFound(true);

        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

好吧,现在throwExceptionIfNoHandlerFound是平等的true.但是,这没有按预期发挥作用.在DispatcherServlet持续不扔的NoHandlerFoundException.这样,我无法处理它.

CustomExceptionHandler.java (这没用)

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@ControllerAdvice
public class CustomExceptionHandler extends ResponseEntityExceptionHandler {

    @Override
    protected ResponseEntity …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-boot

0
推荐指数
1
解决办法
3973
查看次数