Java Spring引导安全性禁用特定URL的Web安全性

Java Spring引导安全性禁用特定URL的Web安全性,java,spring,spring-boot,spring-security,basic-authentication,Java,Spring,Spring Boot,Spring Security,Basic Authentication,我正在为包含一组Restful服务的Spring引导应用程序使用Spring安全性。我已经使用基本身份验证启用了Web安全性。 我希望启用基本身份验证,除了以特定模式结尾的特定API URL。(例如,像:/application/\u healthcheck这样的healthcheck API) 代码如下所示: @Configuration @EnableWebSecurity public class ApplicationWebSecurityConfigurer extends WebSe

我正在为包含一组Restful服务的Spring引导应用程序使用Spring安全性。我已经使用基本身份验证启用了Web安全性。 我希望启用基本身份验证,除了以特定模式结尾的特定API URL。(例如,像:/application/\u healthcheck这样的healthcheck API)

代码如下所示:

@Configuration
@EnableWebSecurity
public class ApplicationWebSecurityConfigurer extends WebSecurityConfigurerAdapter {

    @Autowired
    private AuthenticationEntryPoint authEntryPoint;

    @Value("${application.security.authentication.username}")
    private String username;

    @Value("${application.security.authentication.password}")
    private String password;

    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .anyRequest().authenticated()
                .and().httpBasic()
                .authenticationEntryPoint(authEntryPoint);
    }

    @Autowired
    public void configureGlobal(final AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser(username).password(password).roles("USER");
    }

    @Override
    public void configure(final WebSecurity web) throws Exception {
        web.ignoring().antMatchers("*/_healthcheck");
    }
}
然而,每当我调用…/application/_healthcheckURL时,浏览器总是提示我输入凭据

或者,我甚至尝试从Spring boot的application.properties忽略此路径(删除了带有web.ignering().antMatchers(“*/\u healthcheck”)的configure方法),但仍然无法摆脱此端点的身份验证

security.ignored=/_healthcheck,*/_healthcheck

试试这个。这将跳过健康URL的身份验证部分

@Override
protected void configure(final HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("*/_healthcheck").permitAll() // added this line
                .anyRequest().authenticated()
                .and().httpBasic()
                .authenticationEntryPoint(authEntryPoint);
}

上述示例与您的代码的区别在于
web
http
的使用。尝试使用web,看看它是否有效{实际上它应该:)}。

您可以创建权限列表并使用它禁用安全性

List<String> permitAllEndpointList = Arrays.asList(
            AUTHENTICATION_URL,
            REFRESH_TOKEN_URL,
            EXTERNAL_AUTH_URL,
            "/swagger-resources",
            "/swagger-resources/**",
            "/swagger-ui.html"
);

这将解决您的问题。

Thank@dur可能重复,但我只是在web.ignoring().antMatchers(“*/\u healthcheck”)不起作用之后才添加属性配置。删除该配置方法并仅保留属性配置也没有帮助。
web.ignering().antMatchers(*/\u healthcheck”)
是错误的,因为.Thank dur解决了我的问题。Thank Kuldeep的可能副本,但是正如dur在中提到的,我必须将路径模式更新为.antMatchers(“/**/\u healthcheck”)
List<String> permitAllEndpointList = Arrays.asList(
            AUTHENTICATION_URL,
            REFRESH_TOKEN_URL,
            EXTERNAL_AUTH_URL,
            "/swagger-resources",
            "/swagger-resources/**",
            "/swagger-ui.html"
);
.and()
.authorizeRequests()
.antMatchers(permitAllEndpointList.toArray(new String[permitAllEndpointList.size()]))
.permitAll()