gpt4 book ai didi

java - Spring 启动: How to skip basic-auth for particular endpoint

转载 作者:行者123 更新时间:2023-12-02 09:31:30 25 4
gpt4 key购买 nike

我尝试了很多方法,但是当我点击/healthCheck URL 时,它会提示输入用户名和密码。我在 EmailsecurityAdapter.java 类中有以下代码

import com.cellpointmobile.email.services.AuthenticationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity
public class EmailSecurityAdapter extends WebSecurityConfigurerAdapter {

@Autowired
AuthenticationService userDetailsService;

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
auth.authenticationProvider(authenticationProvider());
}


@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/healthCheck**");
}


@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.GET, "**/heathCheck/*").permitAll()
.anyRequest()
.access("isFullyAuthenticated() and hasAnyRole('ROLE_ADMIN','ROLE_USER')")
.and()
.httpBasic();
}

@Bean
public PasswordEncoder passwordEncoder() {
//https://www.browserling.com/tools/bcrypt
return new BCryptPasswordEncoder();
}

@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
authenticationProvider.setUserDetailsService(userDetailsService);
authenticationProvider.setPasswordEncoder(passwordEncoder());
return authenticationProvider;
}
}

我应该在configure()方法中更改什么才能公开访问healthCheck url?

postman 响应 GET http://127.0.0.1:8080/heathCheck

<Map>
<timestamp>2019-09-13</timestamp>
<status>401</status>
<error>Unauthorized</error>
<message>Unauthorized</message>
<path>/heathCheck</path>
</Map>

最佳答案

使用

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.GET, "**/heathCheck/**").permitAll()
.anyRequest()
.access("isFullyAuthenticated() and hasAnyRole('ROLE_ADMIN','ROLE_USER')")
.and()
.httpBasic();
}

或者在configure(WebSecurity web)中使用web.ignoring(),这将忽略指定端点的所有安全过滤器。之后,您可以将其从 configure(HttpSecurity http) 中删除,并将 configure(WebSecurity web) 保留在 configure(HttpSecurity http) 之上。

<强> HttpSecurity vs WebSecurity

 @Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers(HttpMethod.yourMethod, "**/heathCheck/**");
}
<小时/>

更新:

@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers(HttpMethod.yourMethod, "**/heathCheck/**");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers(HttpMethod.GET, "**/heathCheck/*").permitAll()
.anyRequest()
.access("isFullyAuthenticated() and hasAnyRole('ROLE_ADMIN','ROLE_USER')");
}

关于java - Spring 启动: How to skip basic-auth for particular endpoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57927195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com