小编vto*_*osh的帖子

Spring Boot - 自动装配DataSource Bean

我有一个基本的Spring Boot应用程序,注释如下:

@SpringBootApplication
public class ApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApiApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的application.properties文件中有以下条目:

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/db
spring.datasource.username=dbuser
spring.datasource.password=dbpassword
Run Code Online (Sandbox Code Playgroud)

根据我的理解,Spring Boot应该能够从这些属性自动自动装配DataSource Bean.

但是,如果我尝试:

@Autowired
DataSource dataSource;
Run Code Online (Sandbox Code Playgroud)

在我的应用程序的任何地方(在@Configuration文件中),我在IntelliJ中收到以下错误:

"无法自动装配.没有找到'DataSource'类型的豆子."

是否有一些显而易见的东西让我无法工作?

我有一个DataSource.

spring autowired spring-boot

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

使用openssl创建.p12信任库

我正在编写Java 8应用程序,并希望使用自签名证书设置一个简单的密钥库和信任库.

通常情况如下:

  1. 使用创建密钥对+证书openssl.
  2. 使用创建.jks密钥库+ .jks信任库 keytool

现在我只想使用openssl和创建.p12密钥库而不是.jks密钥库.

使用以下命令创建.p12密钥库非常有用:

# Create private key and certificate
openssl req -x509 -newkey rsa:"${rsa}" -sha256 \
    -keyout "${key}" \
    -out "${cert}" \
    -days "${days}"

# Create .p12 keystore
openssl pkcs12 -export -in "${cert}" -inkey "${key}" -out "${keystore}"
Run Code Online (Sandbox Code Playgroud)

这个密钥库似乎工作正常,因为在我的Java应用程序中提供相应的.jks信任将获得TLS连接.但是我无法让.p12信任库工作.

我尝试按照此处的建议创建信任库:

# Create .p12 truststore
openssl pkcs12 -export -nokeys -in "${cert}" -out "${truststore}"
Run Code Online (Sandbox Code Playgroud)

然后加载它像这样:

FileInputStream fis = new FileInputStream(new File(trustorePath));
KeyStore trustStore = KeyStore.getInstance("PKCS12");
trustStore.load(fis, truststorePassword.toCharArray());
fis.close();
Run Code Online (Sandbox Code Playgroud)

但我在我的java代码中收到以下异常:

意外错误:java.security.InvalidAlgorithmParameterException:trustAnchors参数必须为非空

我有什么想法我做错了吗? …

java ssl openssl

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

Spring - 从过滤器返回 JSON 格式的错误消息

我正在开发 Spring Boot REST 应用程序。

我注册了一个自定义 AuthenticationEntryPoint,如果用户不提供凭据,它会返回“401 未经授权”错误。

@Component
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
    }
}
Run Code Online (Sandbox Code Playgroud)

这非常有效,并返回 JSON 格式的DefaultErrorAttributes,如下所示:

{
  "timestamp": 1465230610451,
  "status": 401,
  "error": "Unauthorized",
  "exception": "org.springframework.security.authentication.BadCredentialsException",
  "message": "Unauthorized",
  "path": "/webapp/login"
}
Run Code Online (Sandbox Code Playgroud)

Filter现在我已经使用以下覆盖添加到应用程序中doFilter()

@ Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {
    try {
        // Here be some code that fails.
    } catch (Exception e) …
Run Code Online (Sandbox Code Playgroud)

spring json httpresponse servlet-filters spring-boot

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

Spring Boot - 单元测试ApplicationReadyEvent业务逻辑

我有一个简单的服务,我user用默认用户预填充数据库表。该服务如下所示:

@Service
public class BootstrapService
{
    @Autowired
    UserRepository userRepository;

    public void bootstrap()
    {
        User user = new User("admin", "password");
        userRepository.save(user);
    }
}
Run Code Online (Sandbox Code Playgroud)

我在应用程序启动时使用以下命令调用此服务ApplicationListener

@Component
public class ApplicationStartup implements ApplicationListener<ApplicationReadyEvent>
{
    @Autowired
    private BootstrapService bootstrapService;

    @Override
    public void onApplicationEvent(final ApplicationReadyEvent event)
    {
        bootstrapService.bootstrap();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我想编写一个单元测试来BootstrapService检查用户是否确实被添加,如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyApplication.class)
@Transactional
public class BootstrapServiceTests
{
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private BootstrapService bootstrapService;

    @Test
    public void testBootstrap()
    {
        bootstrapService.bootstrap();

        assertEquals(1, userRepository.count());
    }
}
Run Code Online (Sandbox Code Playgroud)

然而,发生的情况是该bootstrapService.bootstrap() …

spring unit-testing spring-boot

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

Spring Cloud Config:客户端不会尝试连接到配置服务器

我正在尝试创建一个简单的 Spring Cloud Config 服务器/客户端设置,并且松散地遵循文档:

https://cloud.spring.io/spring-cloud-config/reference/html/

到目前为止,我已经实现了一个似乎可以正常工作的服务器,即当我调用相应的端点时返回正确的属性值:

GET http://localhost:8888/config-client/development

{
  "name": "config-client",
  "profiles": [
    "development"
  ],
  "label": null,
  "version": null,
  "state": null,
  "propertySources": [
    {
      "name": "classpath:/config/config-client-development.properties",
      "source": {
        "user.role": "Developer"
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

但是,我在让客户端连接到服务器方面没有任何运气。我做了以下工作:

  1. 添加了spring-cloud-starter-config依赖项:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
  1. 添加了一个bootstrap.properties文件:
spring.application.name=config-client
spring.profiles.active=development
spring.cloud.config.uri=http://localhost:8888
Run Code Online (Sandbox Code Playgroud)

但我仍然得到一个

java.lang.IllegalArgumentException: Could not resolve placeholder 'user.role' in value "${user.role}"
Run Code Online (Sandbox Code Playgroud)

尝试运行客户端应用程序时。

应用程序日志中没有任何内容看起来像是客户端正在尝试与配置服务器进行通信。

链接到重现该问题的最小GitHub 存储库:https : //github.com/Bragogirith/spring-cloud-minimal

重现步骤:

  1. 构建并运行config-service应用程序
  2. 构建并运行config-client应用程序

知道我做错了什么吗?

spring-boot spring-cloud spring-cloud-config

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