小编Geo*_*rge的帖子

Spring Boot Kafka 监听器与消费者

有什么不同?KafkaConsumer 和 KafkaListener 可以互换使用吗?

apache-kafka kafka-consumer-api spring-kafka

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

Spring 5 Security OAuth2 登录重定向循环

我想使用 Spotify Web API,但我在 Spring Security Configuration 方面遇到了问题。这是我的安全依赖项:

 /* springBootVersion = '2.1.2.RELEASE' */
implementation "org.springframework.security:spring-security-oauth2-client"
implementation 'org.springframework.security:spring-security-oauth2-jose:5.1.6.RELEASE'
implementation "org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.1.7.RELEASE"
Run Code Online (Sandbox Code Playgroud)

这是我application.yml文件中的安全性:

spring:
  security:
    oauth2:
      client:
        registration:
          spotify:
            provider: spotify-provider
            client-id: <client-id>
            client-secret: <client-secret>
            client-authentication-method: basic
            authorization-grant-type: authorization_code
            redirect-uri: http://localhost:8080/
            scope: <comma delimited scopes>
        provider:
          spotify-provider:
            authorization-uri: https://accounts.spotify.com/authorize
            token-uri: https://accounts.spotify.com/api/token
            user-info-uri: https://api.spotify.com/v1/me
Run Code Online (Sandbox Code Playgroud)

我的问题是,在我登录并重定向回我的应用程序后,它卡在 URL 上http://localhost:8080/oauth2/authorization/spotify并显示错误

localhost 将您重定向了太多次。

这是我的 java 安全配置的样子:

@Configuration
public class SpotifySecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .anyRequest()
            .authenticated() …
Run Code Online (Sandbox Code Playgroud)

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

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

Spring Boot 2.0 Hibernate 5 EhCache 3 与 JCache

我正在尝试将 EhCache 设置为 Hibernate 作为二级缓存,但 TTL 不起作用。

这是我的依赖项:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-jcache</artifactId>
</dependency>

<dependency>
  <groupId>org.ehcache</groupId>
  <artifactId>ehcache</artifactId>
</dependency>

<dependency>
  <groupId>javax.cache</groupId>
  <artifactId>cache-api</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

这是我的 YAML 配置:

spring:
  jpa:
    show-sql: true
    properties:
      hibernate:
        dialect: Dialect
        cache:
          use_second_level_cache: true
          region.factory_class: org.hibernate.cache.jcache.JCacheRegionFactory
          use_query_cache: true
  cache:
    jcache:
      config: classpath:ehcache.xml
Run Code Online (Sandbox Code Playgroud)

这是我的实体类的配置方式:

@Entity
@javax.persistence.Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class PersonEntity {
  //
}
Run Code Online (Sandbox Code Playgroud)

以及实体的 JpaRepository:

public interface PersonRepository extends JpaRepository<PersonEntity, Integer> {
  @org.springframework.data.jpa.repository.QueryHints({
      @javax.persistence.QueryHint(name = "org.hibernate.cacheable", value = "true")
  })
  List<PersonEntity> findByName(String name);
}
Run Code Online (Sandbox Code Playgroud)

我已将缓存配置为在 …

java hibernate spring-boot ehcache-3

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

REST与SOAP - SOAP真的比REST更安全吗?

我不是SOAP方面的专家,但据我所知,SOAP只是一个用XML格式化的HTTP请求,以便提供结构化数据.

我需要使用参数列表实现一个简单的API.

我建议使用一个简单的REST接口,但我被告知SOAP更安全.


关于这个主题的任何想法?

rest soap http

5
推荐指数
2
解决办法
2744
查看次数

如何获取JAR文件中资源目录内的所有资源?

我正在使用 Spring Boot,我想获取资源。

\n\n

这是我的目录结构:

\n\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80java\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80...\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80resources\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80files\n\xe2\x94\x82       \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80file1.txt\n\xe2\x94\x82       \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80file2.txt\n\xe2\x94\x82       \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80file3.txt\n\xe2\x94\x82       \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80file4.txt\n
Run Code Online (Sandbox Code Playgroud)\n\n

我正在尝试获取files目录中的资源。这是我为访问这些文件所做的操作:

\n\n
@Autowired\nprivate ResourceLoader resourceLoader;\n\n...\nStream<Path> walk = Files.walk(Paths.get(resourceLoader.getResource("classpath:files/").getURI()));\n
Run Code Online (Sandbox Code Playgroud)\n\n

当文件位于target目录中时,它可以在本地运行,但当我从 JAR 文件运行它时,找不到这些文件。我该如何解决?我已经检查过这些文件确实存在于位于BOOT-INF/classes/files.

\n\n

以下是 maven 如何构建资源并将其复制到 JAR 中(我不希望 .txt 文件被过滤):

\n\n
<resources>\n  <resource>\n    <directory>src/main/resources</directory>\n    <filtering>true</filtering>\n    <excludes>\n      <exclude>**/*.txt</exclude>\n    </excludes>\n  </resource>\n  <resource>\n    <directory>src/main/resources</directory>\n    <filtering>false</filtering>\n    <includes>\n      <include>**/*.txt</include>\n    </includes>\n  </resource>\n</resources>\n
Run Code Online (Sandbox Code Playgroud)\n

java spring java-8

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

配置 EhCache 3 使用默认模板自动创建所需的缓存

我希望 EhCache 使用默认模板创建缓存。这是我的ehcache.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<config
    xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xmlns='http://www.ehcache.org/v3'
    xsi:schemaLocation="
        http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
        http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd">
  <service>
    <jsr107:defaults default-template="default" jsr-107-compliant-atomics="true">
    </jsr107:defaults>
  </service>

  <cache-template name="default">
    <expiry>
      <ttl unit="seconds">5</ttl>
    </expiry>
    <heap>100</heap>
  </cache-template>
<!--  <cache alias="test" uses-template="default" />--> <!-- I want to omit this. -->
</config>
Run Code Online (Sandbox Code Playgroud)

这是我的application.yml文件:

spring:
  cache:
    jcache:
      config: classpath:ehcache.xml
Run Code Online (Sandbox Code Playgroud)

我不想ehcache.xml每次添加需要新的缓存名称时都进行更新。如何使用 Spring Boot 配置 EhCache 以使用我的默认模板?

如果我不指定缓存名称配置,则会收到此错误:

java.lang.IllegalArgumentException: Cannot find cache named 'test' for CacheResultOperation
Run Code Online (Sandbox Code Playgroud)

spring-boot ehcache-3

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

嵌入式 MongoDB 尝试下载 Zip

我想使用嵌入式 MongoDB 和 Spring Boot 进行测试。

这是我要导入的依赖项:

testCompile('de.flapdoodle.embed:de.flapdoodle.embed.mongo')
Run Code Online (Sandbox Code Playgroud)

每当我启动时,都会SpringBootTest初始化EmbeddedMongoAutoConfigurationbean MongodExecutable,并且该 bean 开始从 url 下载 zip 文件(版本 3.2.2)。如何配置它才能使用我src/test/resources目录中的 zip 文件?

embedded-database mongodb spring-boot

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

测试之间的嵌入式 Kafka 刷新消息

我使用 Spring Kafka 和注释@EmbeddedKafka来启动嵌入式 kafka 实例。

依赖关系:

<dependency>
  <groupId>org.springframework.kafka</groupId>
  <artifactId>spring-kafka-test</artifactId>
  <scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我已经配置了我的应用程序以连接到它:

spring:
  kafka:
    bootstrap-servers: ${spring.embedded.kafka.brokers}
Run Code Online (Sandbox Code Playgroud)

我成功发送消息并消费消息。

我的问题是这些消息保留在某处,并且可能会出现在正在进行的测试运行中。日志正在打印这些:

Successfully joined group with generation 117
Run Code Online (Sandbox Code Playgroud)

高代数表明 Kafka 在测试重新运行之间保留了我不想要的信息。

如何彻底清理嵌入式 Kafka 并重新开始?

编辑:

我遇到的问题是我错误地使用了 Spring Profiles。我有一个带有嵌入式 Kafka 配置的自定义注释注释。问题是我@ActiveProfiles("kafka")在复合注释中设置,也在@ActiveProfiles("dev")实际的测试类中设置。我已将复合注释更新为:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
// @ActiveProfiles("kafka") REMOVED
@TestPropertySource(properties = "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}")
@EmbeddedKafka(/* config properties */)
public @interface CustomEmbeddedKafka{
}
Run Code Online (Sandbox Code Playgroud)

spring apache-kafka spring-kafka spring-kafka-test

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

从属性文件创建 Spring Boot 动态 Bean

如何根据application.yml文件动态定义bean?

例如,YAML 文件如下所示:

service:
   host: http://localhost:8080/
   account:
     url: /account
     content-type: application/json
   registry:
     url: /registry
     content-type: application/xml
Run Code Online (Sandbox Code Playgroud)

这将动态创建两个HttpHeaders带有Content-Type标头集的。

下面是我现在定义 bean 的方式:

@Bean
public HttpHeaders accountHeaders(
    @Value("${service.account.content-type}") String contentType
) {
    HttpHeaders headers = new HttpHeaders();
    headers.set(HttpHeaders.CONTENT_TYPE, contentType);
    return headers;
}

@Bean
public HttpHeaders registryHeaders(
    @Value("${service.registry.content-type}") String contentType
) {
    HttpHeaders headers = new HttpHeaders();
    headers.set(HttpHeaders.CONTENT_TYPE, contentType);
    return headers;
}
Run Code Online (Sandbox Code Playgroud)

如果我需要添加更多端点,我需要复制和粘贴这些 bean,我想避免这种情况。

注意:这些动态 bean 不需要任何其他 bean。我不确定这是否有所作为。它只需要加载配置。

spring spring-boot

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

AssertJ 将提取的字段转换为 Map

我有一个MessageMessageHeaders字段的对象。本MessageHeaders类实现了Map<String, Object>。我想断言我设置了特定的标头。我很难找到MapAssert方法。

这是我想要完成的:

assertThat(actual)
  .extracting(Message::getHeaders) // This returns AbstractObjectAssert though
  .containsKeys("some key");  // Not available 
Run Code Online (Sandbox Code Playgroud)

这是要明确的MessageandMessageHeaders类:

public class Message {
  private MessageHeaders headers;
  // getter
}


public class MessageHeaders implements Map<String, Object>, Serializable {
  // methods
}
Run Code Online (Sandbox Code Playgroud)

java assertj

3
推荐指数
2
解决办法
2128
查看次数

AssertJ 使用 List 字段验证对象列表不为空

例子:

public class Office {
  List<Employee> employee;
}
Run Code Online (Sandbox Code Playgroud)

我如何断言在我List<Office> offices没有员工的情况下没有?是否可以用一个断言链来断言这一点?

assertj

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

Spring Actuator Metrics 生成日志

我正在尝试将微米度量数据发送到 Splunk。每个指标端点都提供指标的当前值,因此我需要 Splunk 定期向我的应用程序发送 http 请求,或者我可以定期将指标值写入日志文件。

那么如何让我的应用程序将指标值写入日志呢?

spring spring-mvc splunk micrometer spring-micrometer

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