是否可以在我的休息应用程序中对某些端点使用 OAuth2,并为其他一些端点使用基本身份验证。它应该都适用于 spring 安全版本 2.0.1.RELEASE。我希望有人能进一步帮助我。
我已经激活了弹簧执行器prometheus endpont /actuator/prometheus.通过添加千分尺和执行器的依赖关系并启用prometheus endpont.我如何获得自定义指标?
我的 Spring Rest 应用程序的单元测试有问题。在我的单元测试中,我总是遇到问题,即我收到 401“未经授权”作为响应状态,但我不知道如何解决这个问题。
我的安全配置是这样的:
@Configuration
@EnableWebSecurity
@Order(1)
public class ApiSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private ApiKeyRepository apiKeyRepository;
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
ApiKeyAuthFilter filter = new ApiKeyAuthFilter(Globals.HEADER_APIKEY);
filter.setAuthenticationManager(authentication -> {
String principal = (String) authentication.getPrincipal();
Optional<ApiKey> apiKey = apiKeyRepository.findByApiKey(principal);
if (!apiKey.isPresent()) {
throw new BadCredentialsException("The API key was not found or not the expected value.");
}
authentication.setAuthenticated(true);
return authentication;
});
httpSecurity.
csrf().disable().
addFilter(filter).authorizeRequests().anyRequest().authenticated().and().
regexMatcher("(?i)/api/v1/(?:print|resource)(?:/|$).*").
sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
}
Run Code Online (Sandbox Code Playgroud)
我的单元测试看起来像:
@RunWith(SpringRunner.class)
@WebMvcTest(ResourceController.class)
public class ResourceControllerTestGetByLogicalFileNameOrDate {
@Autowired …Run Code Online (Sandbox Code Playgroud)