我使用 ErrorDecoder 返回正确的异常而不是 500 状态代码。
有没有办法在解码器中检索原始消息。我可以看到它在 FeignException 中,但不在 decode 方法中。我所拥有的只是“状态代码”和一个空的“原因”。
public class CustomErrorDecoder implements ErrorDecoder {
private final ErrorDecoder errorDecoder = new Default();
@Override
public Exception decode(String s, Response response) {
switch (response.status()) {
case 404:
return new FileNotFoundException("File no found");
case 403:
return new ForbiddenAccessException("Forbidden access");
}
return errorDecoder.decode(s, response);
}
}
Run Code Online (Sandbox Code Playgroud)
这里的原始消息:“消息”:“禁止访问文件”
feign.FeignException: status 403 reading ProxyMicroserviceFiles#getUserRoot(); content:
{"timestamp":"2018-11-28T17:34:05.235+0000","status":403,"error":"Forbidden","message":"Access to the file forbidden","path":"/root"}
Run Code Online (Sandbox Code Playgroud)
此外,我像 RestController 一样使用我的 FeignClient 接口,所以我不使用任何其他填充有可以封装方法调用的代理的 Controler。
@RestController
@FeignClient(name = "zuul-server")
@RibbonClient(name = "microservice-files")
public interface …Run Code Online (Sandbox Code Playgroud) 对于 addAll 操作,是否有复杂度为 O(1) 而不是 O(n) 的 Java 集合,还是必须实现我自己的集合?使用有效的链表,Collection1.addAll(Collection2) 操作应该将第二个集合附加到第一个集合,将 collection2 的第一个节点添加到集合 1 的最后一个节点,然后其他的。但这并不是我阅读了似乎使用迭代器的文档,所以我猜复杂度是 O(collection2.size)。
那正确吗 ?
我有一个使用 JDBC 实现的 spring boot oauth2 服务器。它通过@EnableAuthorizationServer 配置为授权服务器。
我想水平扩展该应用程序,但它似乎无法正常工作。
仅当我有一个服务器实例(Pod)时,我才能连接。
我使用来自另一个客户端服务的 autorisation_code_client 授权来获取令牌。因此,首先客户端服务将用户重定向到 oauth2 服务器表单,然后一旦用户通过身份验证,他应该被重定向到客户端服务,并在 url 上附加代码,最后客户端使用该代码来请求 oauth2 服务器再次并获取token。
如果我有多个 oauth2-server 实例,则用户根本不会被重定向。就一个实例来说,效果很好。
当我实时检查两个实例的日志时,我可以看到身份验证在其中一个实例上进行。我没有任何具体错误,用户只是没有被重定向。
有没有办法将 oauth2-server 配置为无状态或其他方式来解决该问题?
这是我的配置,AuthorizationServerConfigurerAdapter 实现。
@Configuration
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource oauthDataSource() {
return DataSourceBuilder.create().build();
}
@Autowired
@Qualifier("authenticationManagerBean")
private AuthenticationManager authenticationManager;
@Bean
public JdbcClientDetailsService clientDetailsSrv() {
return new JdbcClientDetailsService(oauthDataSource());
}
@Bean
public TokenStore tokenStore() {
return new JdbcTokenStore(oauthDataSource());
}
@Bean
public ApprovalStore approvalStore() {
return new JdbcApprovalStore(oauthDataSource());
}
@Bean
public …Run Code Online (Sandbox Code Playgroud) 我将 Async 与使用 Feign 调用远程服务的方法一起使用,我需要将 oauth2 令牌附加到请求中,为此我使用了 RequestInterceptor。
@Bean
public RequestInterceptor requestTokenBearerInterceptor() {
return requestTemplate -> {
Object principal = SecurityContextHolder
.getContext()
.getAuthentication()
.getPrincipal();
if (!principal.equals("anonymousUser")) {
OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails)
SecurityContextHolder.getContext().getAuthentication().getDetails();
requestTemplate.header("Authorization", "bearer " + details.getTokenValue());
}
};
}
Run Code Online (Sandbox Code Playgroud)
但是当 requestInterceptor 在另一个线程中使用时,我无法访问相同的安全上下文,因此 getAuthentication 返回 null。
我尝试在执行程序配置中修复它,我创建了一个 DelegatingSecurityContextExecutor 来包装执行程序和安全上下文。但似乎 bean 是在“主”线程中创建的,并且安全上下文与当时使用的安全上下文不同,当执行 RestController 方法时,因此 getAuthentication() 仍然返回 null。
@Bean(name = "asyncExecutor")
public Executor asyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(3);
executor.setMaxPoolSize(3);
executor.setQueueCapacity(100);
executor.setThreadNamePrefix("AsynchThread-");
executor.initialize();
Executor wrappedExecutor = new DelegatingSecurityContextExecutor(executor, SecurityContextHolder.getContext());
return …Run Code Online (Sandbox Code Playgroud) multithreading asynchronous spring-mvc spring-boot spring-cloud-feign
spring-boot ×2
asynchronous ×1
collections ×1
java ×1
kubernetes ×1
linked-list ×1
spring-mvc ×1