我了解在执行Comparator接口的比较方法时,我们需要返回
我的问题是为什么我们都必须在相等时返回0?用例是什么?用在什么地方?如果我们考虑在o2大于o1或o2等于o1时进行排序,则不会更改其位置。谁能来解释这个的实际用例?
Java文档说
比较其两个参数的顺序。当第一个参数小于,等于或大于第二个参数时,返回负整数,零或正整数。
这是否意味着返回-1或返回0具有相同的影响?
零或正整数
@Override
public int compare(Test f1, Test f2) {
if (f1.getId() > f2.getId()) {
return 1;
} else if (f1.getId() < f2.getId()) {
return -1;
} else {
return 0;
}
}
Run Code Online (Sandbox Code Playgroud) 我有使用 spring oAuth2 的完美运行的 Spring Boot 应用程序。当我在主类上使用 @EnableResourceServer 时,它运行良好。但要为我在课下实现的服务启用 CORS
@Configuration
public class CorsConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.cors();
}
@Bean
public CorsConfigurationSource corsConfigurationSource() {
final org.springframework.web.cors.CorsConfiguration configuration = new org.springframework.web.cors.CorsConfiguration();
configuration.setAllowedOrigins(ImmutableList.of("*"));
configuration.setAllowedMethods(ImmutableList.of("HEAD",
"GET", "POST", "PUT", "DELETE", "PATCH"));
configuration.setAllowCredentials(true);
configuration.setAllowedHeaders(ImmutableList.of("Authorization", "Cache-Control", "Content-Type"));
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
}
Run Code Online (Sandbox Code Playgroud)
添加该类后,似乎请求未使用 oAuth 令牌进行验证。即使没有 oAuth 令牌的任何请求也可以访问服务。
我做错了什么?我不能两个都用吗?有什么特别的事情要做吗?有没有更好的方法来启用 CORS。(我试过@CrossOrigin 注释它没有用)