Với 4.x an ninh mùa xuân đã có một lớp cho rằng
org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint
mùa xuân boot cũng bao gồm một
org.springframework.boot.autoconfigure.security.Http401AuthenticationEntryPoint
và cả những lợi ích mà họ yêu cầu các nhà phát triển để sử dụng spec compliant như 401 responses requires that header WWW-Authenticate phải được thiết lập, ví dụ 401 trả lời có thể là:
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="example",
error="invalid_token",
error_description="The access token expired"
Vì vậy, trong cấu hình bảo mật của bạn, bạn xác định và autowire một đậu của lớp
vì vậy, ví dụ với ứng dụng khởi động mùa xuân:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
@Bean
public Http401AuthenticationEntryPoint securityException401EntryPoint(){
return new Http401AuthenticationEntryPoint("Bearer realm=\"webrealm\"");
}
@Autowired
private Http401AuthenticationEntryPoint authEntrypoint;
...
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login").anonymous()
.antMatchers("/").anonymous()
.antMatchers("/api/**").authenticated()
.and()
.csrf()
.disable()
.headers()
.frameOptions().disable()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.logout()
.permitAll()
.exceptionHandling().authenticationEntryPoint(authEntrypoint);
}
dòng tương ứng là:
.exceptionHandling().authenticationEntryPoint(authEntrypoint);
này hoạt động! Cảm ơn bạn. – Mati
Xe tăng, giải pháp này cũng làm việc cho vấn đề của tôi! Tôi muốn làm ngược lại: thay đổi 401 thành 403. xác thực đường nốiEntryPoint đã chuyển sang httpBasic(), tôi đã yêu cầu chỉnh sửa cho điều này. – switch87