小编bar*_*lik的帖子

Jpa审计的测试配置

我正在尝试在 Spring Boot 应用程序中使用 Spring Data、Hibernate Envers 和审计。我已经配置了 AuditorAwareImpl

public class AuditorAwareImpl implements AuditorAware<String> {

    @Override
    public Optional<String> getCurrentAuditor() {
        return Optional.of("Default auditor");
    }
}
Run Code Online (Sandbox Code Playgroud)

和它的配置类。

@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
public class AuditingConfiguration {

    @Bean
    public AuditorAware<String> auditorProvider() {
        return new AuditorAwareImpl();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我想为我的集成测试创建 AuditorAware。我用测试审计员创建了新的配置类

@Configuration
@Profile("test")
@EnableJpaAuditing(auditorAwareRef = "testAuditorProvider")
public class TestAuditingConfiguration {

    @Bean
    @Primary
    public AuditorAware<String> testAuditorProvider() {
        return () -> Optional.of("Test auditor");
    }

}
Run Code Online (Sandbox Code Playgroud)

当我尝试像这样编写集成测试时

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test")
public class AuditingApplicationTests {

    @Autowired
    private AuditorAware<String> auditorAware; …
Run Code Online (Sandbox Code Playgroud)

java spring spring-test spring-data-jpa spring-boot

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

Spring Security 5 populating authorities based on JWT claims

As I see Spring Security OAuth2.x project was moved to Spring Security 5.2.x. I try to implement authorization and resource server in new way. Everythin is working correctly except one thing - @PreAuthorize annotation. When I try to use this with standard @PreAuthorize("hasRole('ROLE_USER')") I always get forbidden. What I see is that the Principal object which is type of org.springframework.security.oauth2.jwt.Jwt is not able to resolve authorities and I have no idea why.

org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken@44915f5f: Principal: org.springframework.security.oauth2.jwt.Jwt@2cfdbd3; Credentials: [PROTECTED]; Authenticated: true; Details: …
Run Code Online (Sandbox Code Playgroud)

java spring oauth spring-security spring-security-oauth2

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

Junit 测试中 LocalDateTime 反序列化的问题

LocalDateTimeJunit测试中遇到反序列化问题。我有简单的REST API返回一些DTO对象。当我打电话给我的端点时,响应没有问题 - 这是正确的。然后我尝试编写单元测试,获取MvcResult并使用ObjectMapper将其转换为我的DTO对象。但我仍然收到:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.time.LocalDateTime` out of START_ARRAY token
 at [Source: (String)"{"name":"Test name","firstDate":[2019,3,11,18,34,43,52217600],"secondDate":[2019,3,11,19,34,43,54219000]}"; line: 1, column: 33] (through reference chain: com.mylocaldatetimeexample.MyDto["firstDate"])
Run Code Online (Sandbox Code Playgroud)

我正在尝试@JsonFormat并添加compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.9.8'到我的build.gradle但我使用Spring Boot 2.1.3.RELEASE所以它参与其中。我不知道如何解决它。我的简单端点和单元测试如下:

@RestController
@RequestMapping("/api/myexample")
public class MyController {

    @GetMapping("{id}")
    public ResponseEntity<MyDto> findById(@PathVariable Long id) {

        MyDto myDto = new MyDto("Test name", LocalDateTime.now(), LocalDateTime.now().plusHours(1));
        return ResponseEntity.ok(myDto); …
Run Code Online (Sandbox Code Playgroud)

java junit jackson spring-boot

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

带有自定义 UserDetails 实现的 java.io.NotSerializableException

我尝试使用 OAuth2 保护我的 Spring Boot 应用程序。在某些方面,一切正常。当我尝试从oauth/token端点获取访问令牌时,首先尝试获取令牌没有问题。当我再次尝试获取它时,我收到自定义UserDetails实现的异常:

Failed to deserialize authentication

java.lang.IllegalArgumentException: java.io.NotSerializableException: Not allowed to deserialize com.example.oauth.user.domain.User
Run Code Online (Sandbox Code Playgroud)

用户实体:

@Data
@EqualsAndHashCode(of = "uuid")
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "app_users")
public class User implements UserDetails {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @Column(name = "uuid")
  private String uuid = UUID.randomUUID().toString();

  @Column(name = "username")
  private String username;

  @Column(name = "password")
  private String password;

  @Column(name = "account_expired")
  private boolean accountExpired;

  @Column(name = "account_locked")
  private boolean accountLocked;

  @Column(name = "credentials_expired") …
Run Code Online (Sandbox Code Playgroud)

java spring spring-security spring-boot spring-security-oauth2

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