Java Spring security BASC身份验证仅验证第一个请求
我正在使用spring basic身份验证和自定义身份验证提供程序:Java Spring security BASC身份验证仅验证第一个请求,java,spring,spring-security,Java,Spring,Spring Security,我正在使用spring basic身份验证和自定义身份验证提供程序: public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private CustomAuthenticationProvider authProvider; @Override protected void configure( AuthenticationManagerBuilder auth) throw
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private CustomAuthenticationProvider authProvider;
@Override
protected void configure(
AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authProvider);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated()
.and()
.httpBasic();
}
及
为了测试这一点,我使用postman进行以下测试:
无效凭据->401未经授权
正确凭证->200确定
无效凭证->200确定
我的问题是,最后一个请求应该返回401 unauthorized,并且在成功登录后,即使有错误的令牌并且没有令牌,每个后续请求都是200 OK
提前感谢。当您成功登录时,Spring Security将创建一个身份验证对象,并将其放在HTTP会话的SecurityContext中。只要您在服务器上有一个具有有效身份验证对象的有效会话,Spring Security将不会再次对您的请求进行身份验证,并将使用会话中保存的身份验证对象 这是Spring的安全功能,请参见: 检查SecurityContextHolder是否具有经过身份验证的身份验证,并重新使用它。在这种情况下,不要再次调用身份验证管理器 如果你想重新认证,你可以 完全不使用会话 重新验证前注销
在这两种情况下,Spring Security都不会在会话中找到保存的经过身份验证的用户,而是使用新的用户名和密码进行身份验证 您的customauth方法中有哪些内容?您可以尝试查看此示例:如果您有ommit,请尝试与代码进行比较。目前,我看到的唯一一件事是您没有authenticationEntryPoint,但我不知道它是否真的需要,但您可以尝试:.httpBasic.authenticationEntryPointauthenticationEntryPoint;
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String name = authentication.getName();
String password = authentication.getCredentials().toString();
if (customauth()) { // use the credentials
// and authenticate against the third-party system
{
return new UsernamePasswordAuthenticationToken(
name, password, new ArrayList<>());
}
} else {
return null;
}
}
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(
UsernamePasswordAuthenticationToken.class
);
}